01Why a money app has to work offline
An expense tracker has one moment that matters: the few seconds after someone pays. If the app shows a spinner, an error or "check your connection" at that moment, the expense is not recorded, and a month later the totals no longer match reality. Once the numbers are wrong, people stop trusting the app and stop using it.
That moment usually happens in a shop, a market, a rickshaw or a basement car park — exactly the places with the weakest signal. So for Spendora, offline support was not a nice-to-have feature. It was the requirement that shaped the whole data layer.
If adding an expense can fail, users learn to add expenses "later". Later usually means never.
02Save on the phone first, always
Every expense create, edit and delete in Spendora is written to an outbox table in SQLite on the phone before anything touches the network. The screen updates from that local write immediately, so the user sees their expense in the list with a small "Saved on this phone" label until it reaches the server.
The network request is then just a background job that drains the outbox. With a connection, that happens within a second and the label disappears. Without one, nothing about the user's flow changes — they keep adding expenses and the queue grows.
- The outbox is the source of truth for pending changes, not component state.
- Screens read from a per-account cache, so they open instantly even offline.
- A slim banner only appears when there is something to say: offline with N unsynced, syncing, or items needing attention.
03Idempotent uploads with phone-generated IDs
The classic offline bug is the duplicate. The phone sends a new expense, the server saves it, the response is lost when the signal drops, and the phone — never having seen a success — sends it again. Now the user has two identical lunches.
Spendora avoids this by giving every new expense a UUID on the phone, called client_id, before it is ever sent. The database has a unique constraint on (user, client_id). When a retry arrives, the server recognises the client_id, does not create a second row, and simply returns the expense that already exists. Retrying becomes completely safe, so the sync engine can retry as aggressively as it likes.
Make retries harmless and you no longer have to be clever about when to retry.
04One sync endpoint, one answer per action
Rather than replaying individual REST calls, the phone sends up to 50 queued actions to a single POST /api/sync/ endpoint. Each action carries its operation (create, update or delete), its client_id, the server ID if it has one, and the time the change was made on the phone.
The server processes each action in its own database transaction and returns a result per action: ok, rejected, gone or stale. One bad action never rolls back the other 49. The phone removes the actions that succeeded and keeps the rest.
- ok — applied; the phone replaces its local copy with the server's version.
- stale — a newer change already exists on the server, so this one is dropped.
- gone — the expense was deleted elsewhere; the local copy is removed.
- rejected — the server cannot accept it (for example, its income source was deleted on another phone). It is listed under "Needs attention" with the reason, to retry or discard.
05Conflicts: last write wins, honestly
If the same expense is edited on two phones, Spendora keeps the most recent change, using the time the change was made on the phone. That is the right model for personal data where one person owns every record, and it is far easier to explain to users than a merge dialog.
Phone clocks cannot be trusted, though. A phone set a day ahead would win every conflict forever. So the server caps the client time at its own clock before comparing. A future timestamp is treated as "now", which keeps last-write-wins meaningful without letting one misconfigured device dominate.
06Keeping the queue small
A naive outbox grows with every keystroke-level change. Spendora collapses the queue as changes are made. Editing an expense that has not been uploaded yet merges the edit into the pending create. Deleting an expense that never reached the server just removes it from the queue — the server never needs to hear about it.
Sync runs on sign-in, when the network comes back, when the app returns to the foreground and after every change. While the server is unreachable, it backs off exponentially from 2 seconds to 5 minutes, so a phone with no signal is not burning battery on doomed requests.
07What the server has to guarantee
Offline-first is mostly a server discipline. The phone can only be relaxed about retries because the API guarantees idempotency, per-action transactions and strict per-user isolation — a client_id from one account can never touch another account's data.
Those guarantees are covered by automated tests: duplicate uploads, partial batch failures, ordering, deleted-versus-edited, phones with future clocks, and isolation between users. Spendora's API has 74 tests in total, and the sync suite is the part I would least want to change without them.
08What stays online-only, and why
Not everything needs to work offline. In Spendora, expenses are offline-first because that is the high-frequency action at an inconvenient moment. Income, savings goals, budgets, recurring schedules, shared bills and search stay online-only for now: they are changed rarely, usually at home, and keeping them online avoids a lot of conflict surface for little user benefit.
That is the general rule I apply on client apps too: make the action people take in the field bulletproof, and keep the rest simple until real usage shows otherwise.
