Offline-first architecture

System design scenarios Senior Swift 5.9+ iOS 17+ Reviewed By Alok Singh

Order the steps

Order what happens when the user adds an item to the cart offline.

  • Queue a mutation with an idempotency key in the outbox
  • When online, the sync engine sends the outbox and pulls server changes
  • Write the change to the local database
  • Resolve conflicts and update the local database
  • The UI updates from the database
Hint 1 of 1

Local first, network later.

Choose an answer, then open Show answer below to check it.

Show answer

Answer: C E A B D

  1. Write the change to the local database The local store is the source of truth.
  2. The UI updates from the database The user sees the change immediately.
  3. Queue a mutation with an idempotency key in the outbox The outbox persists the intent across launches.
  4. When online, the sync engine sends the outbox and pulls server changes Sync runs whenever the network allows.
  5. Resolve conflicts and update the local database The UI reflects the reconciled state.

30-second answer

Make the local database the source of truth: the UI reads from it and updates as it changes. User actions write locally first, then go into a persistent outbox of mutations with idempotency keys. A sync engine sends the outbox when the network is available, pulls server changes since the last sync token, and resolves conflicts with clear rules, such as the server winning for prices and stock, and merging for the cart.

Practise saying it · 90 s. No recording, just a timer

Design offline-first.

Say your answer out loud, then check which of these points you covered:

  • Local DB as truth
  • Outbox with idempotency
  • Sync engine
  • Conflict rules

Deep dive

Show sync state (pending, failed) on items.

Server-authoritative data like prices must be revalidated at checkout.

Use delta sync tokens, not full downloads.

Test with Network Link Conditioner and airplane mode.

Common mistakes

  • Treating the network response as the source of truth for the UI.
  • Retrying mutations without idempotency keys.

Senior take & follow-ups

Seniors define the source of truth, the outbox, sync and per-entity conflict rules.

Follow-up questions

Think of your answer first, then open the hint.

  1. What wins in a conflict?

    Show hint

    Depends on the data: server for prices, merge for carts, last-write for notes.

  2. How do you avoid duplicate orders?

    Show hint

    Idempotency keys on the outbox mutations.

Suggest a fix if something here is wrong or out of date.