Skip to the page

Building blocks of a client architecture · senior

Normalised client stores and optimistic updates

Store each entity once by id (posts, users, comments) and keep ordered id lists per view, so one change shows everywhere. Apply optimistic updates with a snapshot, so you can roll back when the server says no.

The notes

What you must know

  • Shape: { entities: { posts: { byId }, users: { byId } }, lists: { 'feed:home': { ids: [...], nextCursor } } }.
  • Why: one source of truth (a like updates the feed card and the detail page together), cheap updates, no deep nested copies. Relay and Apollo cache by id for the same reason.
  • Merging pages: append ids and dedupe (live inserts shift pages and repeat items).
  • Optimistic flow: snapshot → apply locally → send with a client id / idempotency key → on success reconcile with the server's values → on error restore the snapshot and tell the user.
  • Temporary ids: create with a client id, replace with the server id on ack; keep a map so in-flight references still work.
  • Conflicts: the server is the source of truth; send a version or ETag; on 409, refetch and show what changed.
  • TanStack Query caches per query key, not per entity: either update every affected key in onMutate, or invalidate them in onSettled. Its docs: cancel outgoing refetches first, snapshot, roll back in onError, invalidate in onSettled.
  • Garbage-collect entities that no list references any more (or rely on the cache's gcTime).

The red pen

Common traps: mistake → fix

  • Mistake: TanStack Query normalises automatically

    Fix: it caches by query key; you update or invalidate each key.

  • Mistake: Optimistic means you can skip error handling

    Fix: you must roll back and explain.

  • Mistake: Deleting from the current list is enough

    Fix: remove it from every list and the entity map (or mark it deleted).

  • Mistake: Rolling back to the snapshot is always safe

    Fix: it can erase a newer server update; reconcile by version when you can.

Easy to hard

Questions interviewers ask, easy to hard

  1. How do you store posts and comments on the client?
  2. A user likes a post. Walk me through the optimistic update.
  3. The server rejects the like. What happens on screen?
  4. The same post appears in two feeds and a detail page. How do they stay in sync?

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview