Devlog | M15

Saving without stopping

Terminal output of stopping a server with a player on it

Saving used to be simple. The server wrote a file, and it took no time worth talking about. Saving to a database over a network is different. It can take a moment, it can fail, and the server has 30 ticks a second to get through, whatever the database is doing.

So saving doesn't happen on the game's clock any more. The server hands a save to a worker and carries on. The worker sends it to the backend, and if that fails, it keeps trying. Only the newest save of a character waits in line, so a backend that's down for a minute doesn't build up a pile, and when it's back, the latest state goes in. A save stays in line until the backend has confirmed it.

Loading got the same treatment. When you join, your request now waits for the database, while everyone else keeps playing.

The most important line in this update is about what happens when a load fails. "I couldn't read your character" and "you have no character" look alike to code that isn't careful. If the game mixed those up, it would offer you the character screen, and a new character would land on top of the old one. So a failed load closes the connection, and that's all it does.

There's a small trap around reconnecting too. If you leave and come straight back, your last save may still be on its way, and the backend would hand you something older. So the server checks its own line first.

Making the backend slow on purpose showed something I hadn't thought of. Unity quietly runs the rest of a waiting task on the main thread, which is the one thing this work was meant to avoid. Loads now stay on a worker the whole way.

Then the real thing. A Linux server in Docker loaded a character from PostgreSQL, a client joined, and I stopped the server with it still in the world. Before this change, nothing would have been saved at that point. This time the last save landed as the server stopped, two seconds later.