skip to content

State Placement & Sync

Deciding where each piece of state lives (component, store, URL, server cache, storage), normalising shared entities and syncing tabs. State design is where large apps rot, so interviewers push here.

on this pageshow

questions

6

In a frontend design round for a web email client, how do you decide where each piece of state lives?

level: middleimportance: must knowfreq 68%

answer

  1. first cut: who owns the truth
  2. server data goes in one cache
  3. shareable view state in the address
  4. home follows who else needs it
  5. survive restart? persist it, deliberately

basics

~20 s

Cut server data from client-only state first: mail, counts and labels live in a client cache of server data; folder, thread and filters go in the URL; one-widget state stays local; shared UI state in an app store; preferences persist.

solid answer

~50 s

I start with one question per piece of state: **who owns the truth?** Conversations, folders, unread counts and labels belong to the server, so the client keeps them in a cache of server data keyed by request, not copied into a second home. What is left is client-only, and I place it by who needs it and how long it must live: the current folder, open thread and search filters go in the address so reload, Back and a pasted link rebuild the view; a menu or hover stays local to its component; the minimised compose draft and app-wide notices go in an app store the shell owns; the reading-pane layout is persisted and applied at startup. The rule costs a little discipline up front and saves the classic rot: two copies of the same message that disagree.

go deeper

for a junior

Recall the two kinds of state: data the server owns and state that exists only in this browser. Know the five homes: component, app store, URL, server-data cache, persisted storage.

for a middle

Walk the email client piece by piece and give each a home with a reason tied to lifetime and audience. Explain why server data gets one cached copy instead of a store copy.

for a senior

Show the failure each wrong placement causes in production: two truths, a lost place on reload, private text in history. Say what changes in the map when a requirement shifts.

for a principal

Treat the map as a team contract: the ladder that decides a home, who may add to the app store, and how persisted keys are versioned and owned across features.

## The first cut: who owns the truth A **state map** is a table that gives every piece of state in the app one home and a reason. In a frontend design round it usually follows the component diagram, and the interviewer reads it to see whether you know that not all state is the same kind. The first and most important split is **server state** versus **client state**: - **Server state** originated on the server and is shared with it: conversations, the message list, unread counts, labels, the folder tree. The client holds only a *copy* that can go stale, so it needs fetching, caching, refetching and invalidation. - **Client state** exists only in this browser: which thread is selected, whether a menu is open, the half-typed reply, the reading-pane layout. Nobody else has a copy, so it never goes stale, but it can be lost. Getting this cut right removes most later bugs. The classic failure is copying server data into a general-purpose app store "so every component can reach it": now there are two copies, and every write must update both. ## The five homes and what each costs | Home | Lives | Survives | Costs | |---|---|---|---| | **Component-local** | inside one component instance | nothing beyond that instance | lost when it unmounts; awkward to share | | **App store** (shell-held) | in memory, above the routes | navigation inside the app | lost on reload; every reader couples to it | | **The address (URL)** | path and query string | reload, Back, a shared link | public, size-limited, must be parsed and validated | | **Client cache of server data** | in memory, keyed by request | navigation; refetched when stale | freshness policy; one entity can appear in many entries | | **Persisted storage** | `localStorage`, IndexedDB | reload and restart | per device, stale on other devices, needs versioning | None is "best". Each answers a different question about **lifetime** and **audience**. ## The email client's map | Piece of state | Home | Why | |---|---|---| | Folder list and unread counts | server-data cache | server owns them; other devices change them | | Message list for folder + filters | server-data cache, keyed by folder, filters and page | server data; the key makes each view its own entry | | Open thread's messages | server-data cache, keyed by thread id | server data | | Current folder, open thread id, search filters | the URL | reload, Back and a pasted link must rebuild the view | | Compose draft while minimised | app store held by the shell, saved to the server as a draft | must outlive moving between folders | | Labels the user owns | server-data cache | server data, rarely changes | | Reading-pane layout | persisted storage, plus server settings if it should follow the user | must apply before first paint on the next visit | | Menu open, hover, a tooltip | component-local | nobody else reads it | | "Message sent" notice | app store | must survive the jump back to the inbox | ## The rule as a sequence 1. **Did it come from the server?** Then the server-data cache is its default home. Derive views from it; copying it elsewhere is an exception you write down and own. 2. **Should reload, Back or a shared link restore it?** Put it in the address. 3. **Does only one component read and change it?** Keep it local. 4. **Do several features or the shell need it during the session?** Put it in an app store with one owning feature. 5. **Must it survive closing the browser?** Persist it deliberately, with a version, and decide what clears it. A piece of state can move down the ladder as requirements change: a filter that was local becomes shareable and moves to the URL. That move is cheap when the map says where each thing lives. ## What goes wrong when the map is skipped - **Two truths.** The list response and an app-store copy disagree about whether a message is read. - **Lost place.** The selected thread lives only in memory, so reload drops the user on an empty inbox. - **Private data in the address.** Draft text in the query string ends up in history and in shared links. - **Persisted server data shown as fact.** Yesterday's unread counts render from `localStorage` before the real ones arrive, and nothing marks them stale. - **Everything global.** Hover and menu state in an app store make every reader re-render and couple features that never needed each other. In the round, draw the table, say the first cut out loud, and defend two or three placements against an alternative. That is what the interviewer is grading: a reason per row, not a store library.

  • The product adds a 'snooze until Monday' feature. Where does the snooze time live?
    On the server, with the conversation. Snoozing must hold on every device and must fire even when no tab is open, so the client cannot own it. The client sends the write and shows the snoozed state from the server-data cache, adjusting the list on its own write. Keeping it in persisted browser storage would make the snooze vanish on another device and never fire with the browser closed.
  • When would you persist part of the server-data cache to browser storage?
    When a fast or offline start matters more than freshness: show the last inbox instantly, mark it as possibly stale, and revalidate at once. That is a deliberate offline policy with its own questions: what to store, how to version it, and what to wipe on sign-out. It belongs to the caching and offline design, not to the basic state map.
  • Two features both need the current user's display name. Does that justify an app store?
    Not by itself. The name is server data, so both features read it from the server-data cache under one key; the cache already shares it. An app store earns its place for client-only state that several features change, such as the minimised draft, not for server data that two features happen to read.

  • Copies fetched conversations into an app store so every component can read them
  • Keeps the selected thread only in memory, so reload loses it
  • Puts draft text in the query string so reload restores it
  • Treats persisted unread counts from the last visit as current
  • Puts every piece of state, hover included, in one global store by default
open as a page

When a web email client is open in several tabs, which state should sync between them, which must not, and how would you carry it?

level: middleimportance: should knowfreq 46%

basics

~20 s

Sync what describes the account, not the tab: sign-out, read flags and unread counts, preferences. Never sync the open draft, selection or filters. Post small change notices on a BroadcastChannel, treat them as hints, and revalidate from the server.

open as a page

In a web email client, how do you decide which state is tied to the current route and which the app shell holds across navigation?

level: middleimportance: should knowfreq 44%

basics

~20 s

Ask what the user expects after navigating away, pressing Back, reloading or sharing a link. The open thread and filters are route-keyed, in the address; a minimised draft, a sent notice and the layout preference are shell-held.

open as a page

In a web email client, opening a thread marks it read, yet the list row stays bold and the folder count is unchanged; how would you redesign the state so every view agrees?

level: seniorimportance: should knowfreq 50%

basics

~20 s

Keep one record per conversation, keyed by id, that the list row and thread header both read; carry the folder count from the server with a local delta applied alongside the flag. A failed write then reverts every view at once.

open as a page

In a single-user kanban board web app, what should enter the undo history, where should it live, and how does undo meet changes the server already saved?

level: seniorimportance: should knowfreq 36%

basics

~20 s

Record intents that change the board, such as moving, creating or editing a card, never selection or filters. Keep one board-scoped history in shell-held memory, leave typing to the field's own undo, and reverse saved changes with a new write.

open as a page

As frontend lead of a large web email client, what rule would you set for where new state may live, and how would you own and enforce it?

level: principalimportance: should knowfreq 30%

basics

~20 s

Publish one ladder: server data in the server-data cache, shareable view state in the address, single-owner state local, shared client state in an owned store slice, durable state in a registered key. Enforce it with build checks, review and audits.

open as a page