Saving games to a database without slowing play
By Swapnil Shah · Updated 2026-11-24
Players like seeing their game history and a leaderboard. But storing every game must never slow a move or cost much to run. Here is how I added a database to a game that started life entirely in memory.
The requirement
Moves must stay instant, so the game itself never waits for the database. The database is also optional: if it is missing or asleep, the game keeps working. And because it is a hobby project, I wanted it to cost nothing when nobody is playing.
A database that sleeps
I run PostgreSQL in a container that is started on demand. A small systemd socket listens for connections, and the first connection wakes the database, which takes about two and a half seconds. After three minutes with no connections it shuts itself down and uses no memory at all. When idle it costs nothing, and when awake it uses a few tens of megabytes.
A background queue
When a game finishes, the result is put on a queue and saved in the background. If the database is still waking up, the save retries after one, three, ten and thirty seconds. The player never notices, because the game does not wait for the result.
Short connections
A database that never sleeps because a connection pool holds it open defeats the whole idea. The pool keeps no idle connections and closes unused ones after about a minute, so the database can go back to sleep. Statistics and leaderboard responses are cached for five minutes, so most page views do not wake it either.
Privacy by design
Anonymous players are identified only by a random id in their browser. A nightly job removes games older than a day where nobody was signed in, and anonymises guests in mixed games, replacing their names with "Guest". Signed-in players can edit their name, make their profile private or delete their account.
What I learned
Treating the database as an optional add-on kept the game fast and robust. If I had started by making every move depend on it, a sleeping database would have turned into laggy games. Design the critical path to work without it, then add it on the side.
What is stored
For each finished game the database stores the board size, the players, the result, how many kills each player made and how the game ended. It does not store chat, and it does not keep exact timings of individual clicks. Signed-in players can see their own history, and a public leaderboard shows only display names.
Lessons
- Make storage a side effect, never part of the move path.
- Make the database optional so a failure never stops a game.
- Let it sleep when nobody is playing, and cache anything that would wake it.
- Decide how long you keep data on day one, and automate the deletion.
Frequently asked questions
Do I need an account for my games to be saved?
Guest games are saved anonymously for a short time and then removed or anonymised. Sign in if you want a lasting history.
Can I delete my data?
Yes. You can delete your account and its game history from the My games page.
Does saving games slow the game?
No. Results are saved in the background after the game ends.