Part of the guide: Software architecture
Engineering10 min read

Building an offline-first expense tracker with React Native and Django

People record a purchase at the counter, not at their desk with good Wi-Fi. Here is the outbox, sync API and conflict model that let Spendora save every expense offline without ever duplicating one.

Short answer

Write every change to a local outbox first, give each new record a phone-generated ID so retries are idempotent, and make the server answer each queued action individually. That combination means no lost expenses and no duplicates, whatever the network does.

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.

TopicsOffline-firstReact NativeExpoDjango REST FrameworkMobile app developmentData sync

FAQ

Quick answers

It means the app saves changes on the device first and treats the server as something to sync with later. The user can keep working with no connection, and changes upload automatically when the connection returns.

Give each new record a unique ID generated on the phone, and enforce a unique constraint on it per user on the server. A retried upload is then recognised and returns the existing record instead of creating a second one.

No. Spendora uses SQLite on the phone for the outbox and screen cache, and a normal Django REST Framework API with PostgreSQL on the server. The reliability comes from the sync protocol, not from a special product.

Spendora uses last-write-wins on the time the change was made on the phone, capped at the server's clock so a device with a wrong future clock cannot win every conflict. Changes the server cannot accept are shown to the user instead of being silently dropped.

Keep reading

All articles

Projects behind this article

The production systems where these decisions were made.

All case studies
  • Spendora Expense Tracker App — Mobile app project preview
    Mobile appPersonal finance

    Spendora Expense Tracker App

    Spendora is an Android expense tracker and budget app built with Expo React Native and Django: income, expen…

    Read case studyLive
  • FE CRM — CRM project preview
    CRMLead tracking

    FE CRM

    A multi-tenant CRM for lead generation, sheet uploads, centre-wise tracking, monthly filters, policies, char…

    Read case study
  • Keynou Flow — SaaS project preview
    SaaSTask management

    Keynou Flow

    A SaaS task and workflow platform with role-based access, subscriptions, todos and structured data managemen…

    Read case studyLive
  • CallLoom Dashboard — Call tracking SaaS project preview
    Call tracking SaaSDialer system

    CallLoom Dashboard

    An inbound and outbound call tracking SaaS with dialers, recordings, AI transcription, routing, Stripe billi…

    Read case studyLive

Related services

If this describes a problem you have right now, these are the ways I can help.

All services
  • Mobile App Development

    A mobile app is only as good as the API behind it. I bring the same backend discipline from my CRM and SaaS work — c…

    Explore service
  • Django & Backend

    The backend is where a product either scales or quietly rots. I design data models that match the business, APIs tha…

    Explore service
  • API & Integrations

    Integrations fail quietly. I build them with retries, logging and clear failure states, so when a third-party servic…

    Explore service
  • Finance & Accounting Software

    Finance teams need numbers they can trust without reconciling three spreadsheets by hand. I have built revenue track…

    Explore service