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?
answer
- rot starts with a second copy
- one ladder, five homes
- every store slice has an owner
- register every persisted key
- enforce in the build, not the wiki
basics
~20 sPublish 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.
solid answer
~50 sState rots in large apps through **second copies** and **orphans**: server data duplicated into stores, persisted keys nobody owns, global slices every feature writes. My rule is a short ladder every engineer applies to new state: server-originated data lives only in the client cache of server data; anything reload, Back or a link must restore goes in the address; state one component owns stays local; client state several features share goes in an app-store slice **owned by one feature**; anything that must survive a restart gets a **registered persisted key** with an owner and a version. Enforcement runs cheapest first: build-time import-boundary checks so features cannot reach into each other's state, a review checklist that asks for the row in the state map, and a periodic audit of persisted keys and store slices. Exceptions are allowed, but written down with an owner and a reason.
go deeper
Recall the homes state can live in and that a team agrees on which one to use, so two people do not store the same thing twice.
Explain the ladder rung by rung for the email client, with one example per rung and the failure the rung prevents.
Show how you would enforce it in the build and in review, and what you would retrofit first in an existing app that already violates it.
Own the trade-off between strictness and delivery speed: which checks are automated, who grants exceptions, and how evidence tells you to change the rule.
## Why a rule at all In a small app, the person adding a piece of state knows where everything else lives. In a large web email client with several teams (mail list, compose, search, settings, calendar side panel), nobody does. Without a shared rule, state placement drifts toward whatever was convenient that week, and the app **rots** in recognisable ways: - **Second copies.** Conversations fetched by search are copied into a search store; the list shows them read, search shows them unread. - **Orphans.** A persisted key from a feature removed two years ago is still read at startup. - **Global slices nobody owns.** Three features write the same flag; a bug in one breaks the other two. - **Hidden coupling.** A settings screen imports the compose feature's internal state to read one value, and compose can no longer change its shape. Each of these is cheap to create and expensive to find later: the bug report says 'the unread dot is wrong', not 'two features hold one conversation'. A rule does not replace judgement; it makes the default obvious, makes the exception visible, and gives every piece of state an owner who can be asked why it lives where it does. ## The ladder Every new piece of state is placed by the first rung that fits: 1. **Server-originated?** It lives in the client cache of server data, read by key. Copying it elsewhere needs a written exception. 2. **Must reload, Back or a shared link restore it?** It goes in the address, owned by the route's feature. 3. **Read and changed by one component?** It stays local. 4. **Shared client state across features during a session?** It goes in an app-store slice with one owning feature; other features read through what that owner exposes. 5. **Must survive closing the browser?** It gets a registered persisted key. Whether the app uses one store or several is a separate decision; this ladder holds either way. ## Ownership | Home | Owned by | What is recorded | |---|---|---| | Server-data cache keys | the feature that defines the request | the key shape, so other features reuse it rather than refetch differently | | Address parameters | the route's feature | the parameter names and their meaning | | App-store slices | exactly one feature | the slice's public reads and writes; everything else is internal | | Persisted keys | exactly one feature | name, owner, schema version, migration and what clears it | | Cross-tab messages | the platform or shell team | message types and their version | ## Enforcement, cheapest first - **Build-time boundary checks.** A dependency rule that fails the build when a feature imports another feature's internal state. This catches the commonest rot earliest and costs nothing per review. - **A persisted-key registry.** One module through which every persisted read and write goes, so an unregistered key cannot be written and each key carries its version and owner. - **Review checklist.** A change that adds state names its rung and adds its row to the feature's state map. Reviewers ask "why is this not in the server-data cache?" when they see fetched data in a store. - **Periodic audit.** A quarterly look at store slices and persisted keys: unused ones removed, owners confirmed. - **Written exceptions.** A short decision record when a team breaks the ladder for a reason, such as persisting part of the inbox for a fast start. ## The trade-offs a lead weighs - **Strictness against speed.** Every check is friction. Automate the cheap, high-value ones and keep the rest to review, or teams route around the rule. - **Retrofitting.** An existing app will violate the ladder in many places. Fix where it hurts (the duplicated conversations) and freeze the rest, rather than a rewrite. - **Who decides exceptions.** A small group with the authority to say yes keeps exceptions honest; without one, every exception is granted by its author. - **Rule versus reality.** If the same exception keeps being requested, the ladder is wrong, not the teams; revisit it. There is no single right ladder. What the interviewer grades is that you can name the failure modes, set a default that prevents most of them, and make the remaining choices visible and owned.
- A team wants to persist the last inbox page so the app paints instantly. How does the rule handle it?It is a rung-1 violation, so it needs a written exception: the owning feature, what exactly is stored, how the cached page is marked as possibly stale, how its format is versioned, and that it is cleared on sign-out. If the exception is sound it ships; the point is that it is visible and owned, not forbidden.
- How do you tell whether the rule is working a year later?Count what it was meant to prevent: bugs traced to two copies of the same server data, persisted keys without an owner, cross-feature imports blocked by the build, and exceptions requested. A falling bug count with few repeated exceptions says it works; the same exception requested by three teams says the ladder needs a new rung.
- Lets each team choose its own state homes with no shared default
- Allows fetched server data to be copied into feature stores freely
- Relies on a wiki page alone to enforce state placement
- Persists keys with no owner, version or clear-on-sign-out rule
- Forbids all exceptions, so teams route around the rule