Skip to content

Writing · The Archive

I broke my own server on purpose. Here's what it taught me

A chaos test with 1,000 bots, a restart, a crash and a database outage. The server came back in 31 seconds. After four small fixes, it comes back in 0.6.

· 4 min read

This site runs its real-time world on one small server. I knew that was a single point of failure when I chose it, and I said the design handles it: the server is disposable, the state lives in DynamoDB, and clients reconnect on their own.

Saying it is easy. So before launch I wrote a test that does the breaking for me.

The test

One script starts the real server against its own DynamoDB table, connects 1,000 bots (most walking the island ten times a second, sixty painting the Pixel Wall), and then does four things to it.

  1. 01

    Load

    1,000 bots walking and painting at once.

  2. 02

    Restart

    A graceful drain and a fresh process, mid-game.

  3. 03

    Database outage

    DynamoDB stops for 20 s while everyone keeps playing.

  4. 04

    Restart in the outage

    The worst case: a new process while the database is still down.

After each step it checks what a visitor would notice: is everyone back, under the same name, and is the Pixel Wall exactly as it was, pixel for pixel? There are 26 checks in all.

Most of them passed on the first run. The last step didn't.

1. 31 seconds of nothing

When the server restarted during the outage, nobody could get in for 31 seconds. Then the server gave up on the database and started the arcade with an empty Pixel Wall, which it would happily have saved over the real one later.

The cause was one line in startup: the server waited for the arcade's saved state before it opened the door. But pages, chat, presence and the 3D world don't need storage at all. One feature was holding the whole server hostage.

The fix was to degrade per feature, not per server:

  • Listen at once. The server accepts visitors immediately.
  • The arcade waits for its storage. Joins are queued and seated by themselves once the saved state loads. Until then nothing is written, so an empty board can never overwrite a saved one.
  • Retry in the background, forever. Backoff from 2 s up to 30 s. No restart needed when the database comes back.

Now the server is back 0.6 seconds after a restart in an outage, and the arcade loads by itself 5.6 seconds after DynamoDB returns, with the wall intact.

For games I chose saved state over availability on purpose. A visitor seeing "the arcade is waiting" for a few seconds is fine. A visitor wiping a week of everyone's pixels is not.

2. "I couldn't read it" is not "there's nothing there"

The same run found a quieter bug. When a visitor's achievements couldn't be read, the server treated them as having none. Their next lost parcel would have saved a list with one item, over the real one.

The rule I took from it: never act on data you failed to read. Now nothing is awarded or saved for that visitor until a read succeeds. An achievement earned during an outage can simply be earned again.

3. 1,000 log lines for one problem

During the outage every reconnecting bot logged the same error. That's 1,000 lines saying one thing, and the one line that mattered would be buried in them.

Logs are for a human at 2 a.m. Each message is now throttled: the first one at once, then at most one a minute, with a count of the ones skipped. The whole chaos run now writes 18 lines.

4. Tell clients when to come back

The API (Lambda, not the real-time server) answered a storage outage with a generic 500. A 500 says "something broke". It now answers 503 with a Retry-After header, which says "I'm temporarily unavailable, come back in a moment": the honest answer, and one any client or cache knows how to handle.

What I took away

  • Degrade per feature

    • One feature's dependency should never block the rest
    • Gate the feature, open the door
  • Unknown is not empty

    • A failed read is not a zero
    • Never write on top of what you could not read
  • Logs are for humans

    • Say it once, then count
    • The important line must stay findable
  • Be honest with clients

    • 503 + Retry-After, not 500
    • Reconnect with the same identity

After the fixes all 26 checks pass at 1,000 bots: everyone is back within about 4 seconds under the same name, and the wall survives a drain, a crash and an outage. None of these bugs would have shown up in a unit test. They only show up when you break things on purpose, and it's much better when I'm the one doing the breaking.

architecture · resilience · aws · dynamodb

Enjoyed this? Leave a reaction.

All writing