Cry Croc had local saves and nothing else. That's fine until a player changes phones and loses forty in-game days. The obvious answer is Firebase or Google Play's save system. I didn't use either. I built cloud save on the backend I already run — the same NestJS/Postgres host the analytics pipeline talks to — with one hard constraint: the APK gains zero new dependencies. No Firebase SDK, no extra Play Services, no new service to learn. This is how I got there without inventing a fragile sync protocol.
1. Don't build an account system you already have one
The game already signs the player into Google Play Games to light up achievements. That sign-in hands me a stable playerId. So that id is the save identity. A save is keyed on (game, google_player_id). There is no email/password to build, no "link your account" flow, no migration. A device UUID is sent along too, but it is stored only as an attribute for auditing and the analytics join — never as the key, because a phone reinstall rotates the UUID and a player's progress should not.
The honest consequence: cloud save requires Play sign-in. Never signed in ⇒ the game is local-only, forever. That's not a broken half-state; it's a clean, explicit mode. And most players sign in anyway.
2. Whole-envelope pushes, on safe beats only
A first instinct is to sync the field that changed — just the coin count, just the day. I do the opposite: every push carries the entire save as one versioned, gzipped envelope. Counterintuitive, but the smaller diff is the worse choice here:
- The save is tens of KB. A partial update saves almost no bytes and risks half-applied state. A whole snapshot is one validated blob; it cannot tear.
- The server never merges fields. It stores and returns the blob. That keeps the protocol trivially correct.
- I push rarely — debounced, at most about every thirty seconds, and only on meaningful beats: surviving a day, starting a new run, a purchase, claiming an achievement, or on app pause/close. Never per-frame, never per-rabbit, never on a background timer.
Settings are the other bucket. Music volume, control scheme, graphics quality are device preferences — they belong to the hardware, not the player. They never sync, on purpose, so your tablet doesn't inherit your phone's audio levels.
3. A clock is not an ordering
The tempting way to resolve two devices is if (clientTime > storedTime) overwrite. Phones lie about time — wrong clocks, manual changes, timezone games. So I never order writes by client_ts. It is stored for observability and nothing else.
Instead every save row carries a server-assigned rev, a strictly increasing integer per (game, save_owner). The client never gets to pick its own rev. A push is a compare-and-swap in one statement:
UPDATE saves
SET rev = rev + 1, payload = @blob, schema_ver = @v, ...
WHERE game_id = @g AND save_owner = @o AND rev = @base_rev;
-- rows affected == 0 -> our base_rev was stale, the write is not accepted
If the stored row's rev is no longer the base_rev the client based its push on, zero rows update and the server replies 409 with the current rev. The client re-pulls, rebases, and re-pushes. That is ordinary optimistic concurrency, and it holds regardless of when the two sides last spoke — which is exactly what I need for an offline-first game.
4. Retrying a network push must not double-apply it
Mobile networks eat responses. A push can succeed on the server while the client never gets the 200, so the client retries — and now I need the second attempt to be a no-op, not a double write. Each logical push carries a client nonce, idem_key. On a retry the server sees the same key (or a payload whose payload_sha256 matches what it already stored) and returns the current rev with dup: true, incrementing nothing:
// push with idem_key already applied, or identical payload_sha256:
200 { "rev": 7, "dup": true } // accepted once, replayed harmlessly
Idempotency here isn't a bonus; without it a single flaky connection silently bumps the save twice.
5. Two brand-new devices, no shared history: rank, don't guess
There's one case OCC can't settle on its own. A player gets a new phone and plays a while offline, so the new device has no shared rev with the cloud (base_rev = 0 against an existing row). Now there's no lineage to compare and the clock can't help. So the server computes a progress_rank — a single integer derived from the decoded payload's own numbers: how far they got (best day reached, then lifetime counters, then wallet, then cosmetics owned). The client never supplies it; the server computes it the same way on every push.
Because a blank save scores zero on every component, a fresh device always ranks lowest and a real save always beats it. The decision is deterministic and human-sensible:
- Stored save ranks higher (the common case: new phone, blank local) → return the cloud save so the client imports it. The player's forty days come back. This is the normal path.
- Incoming ranks higher (they genuinely played more on the new phone) → accept the push.
- Ranks equal → adopt the existing
revas a shared baseline, don't store a duplicate.
After either side wins, both devices share a rev and everything from then on is plain OCC. The rank exists only to bootstrap that first contact and for light cheat-observability — the server still does not simulate a game day or run anti-cheat. v1 trusts gameplay values and defends integrity, not fairness.
6. A failed pull never blocks the game
On launch the client loads the local save and is playable immediately. Only after identity is known does it pull, once. If that pull fails, the failure is silent and non-fatal: a pending-pull flag is set and the retry happens in the background with jittered exponential backoff on reconnect and on the next safe beat — never in a blocking loop, never on the render thread.
When the pull eventually succeeds and the cloud rev is newer, I import on the next clean launch boundary, never mid-run, so the world never changes underneath a player who's already moving through it. A pull that turns out older than local is a no-op; I'll just push the local up later. A cheap known_rev → 304 poll for near-real-time multi-device is designed in but switched off — single-player on one active device has nothing to continuously reconcile.
The boring decisions are the ones that make it safe
A bad-shaped blob is rejected with 400 and never stored, so the previous good save survives. A malformed history can't poison the row. The route gets its own tight 48kb body cap enforced before parse, and both per-IP and per-owner rate limits, because a single game key can get hammered. The raw IP is never stored — only a derived country via the geo service already in the stack, and the player id is masked in logs. And a DELETE /v1/saves/{game}/{owner} erases the row and its history, so "delete my data" actually means it.
None of that is clever. But "trust the network, trust the clock, trust the client" is exactly how cloud saves lose people their progress. A whole encrypted envelope, a server-owned rev, a content-derived rank for the cold start, and idempotent pushes: that's the whole trick — and it ships in an APK that didn't grow a single dependency to get it.