Backend
Making game server writes safe to retry
Fixing player progress and XP being counted twice when a CubeCraft game server retried a write, with write IDs recorded atomically in Redis.
The problem
Game servers send a steady stream of writes to Rocket, like progress and XP. When one times out, the server does the sensible thing and sends it again. But a timeout doesn’t mean the first write failed. Sometimes it landed and only the response got lost, so the retry counted it a second time. Players got XP they didn’t earn, or progress jumped ahead.
What I built
- Every write carries an ID the game server picks once and reuses on every retry
- A Redis Lua script checks for that ID, applies the write, and records the ID with a TTL in one atomic step, so two copies arriving at once can’t both slip through
- A repeat of a write it has already seen gets the original result back instead of being applied again
- Game serverwrite with ID w-123
- Rocketforwards to the script
- Redis Luaseen w-123?
- First timeapply, record ID with TTL
- Seen beforereturn the stored result
The idea in pseudocode
-- runs inside Redis, atomically
function apply_once(write_id, key, delta, ttl):
existing = GET "write:" .. write_id
if existing then -- a missing key comes back as false
return existing -- a retry: same answer, no change
end
new_value = INCRBY key delta
SET "write:" .. write_id new_value EX ttl
return new_value
// the game server side
id = new_write_id()
retry(max = 3, on = Timeout):
rocket.add_xp(player, amount, write_id = id) // same id every attempt
Why it matters
Retrying is the right call on a flaky network, and now it’s safe. Inside the dedupe window, game servers can retry a timed-out write without counting it twice, and nobody has to guess whether it went through.