skip to content

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%

answer

  1. views hold copies, not references
  2. one record per conversation id
  3. lists hold ids, views read records
  4. counts: the client sees one page
  5. a failed write reverts in one place

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.

solid answer

~50 s

The views disagree because each holds its **own copy**: the list response embeds message summaries, the thread response embeds its header, and the folder response carries the count. I would keep **one record per conversation**, keyed by its id; the list holds an ordered list of ids, and the row and the header both read the record, so changing the read flag once changes both. The count is different: the client sees only the loaded page of a folder, so it cannot derive it. I keep the server's number and apply a `-1` delta in the same change as the flag, then take the server's figure when it next arrives. If the write fails, I drop that one pending change and every view falls back together. Where updating in place is too complex, invalidating the affected entries and refetching is a simpler, slower alternative.

go deeper

for a junior

Recall that one conversation can be shown in several places, and that if each place keeps its own copy, changing one does not change the others.

for a middle

Explain normalising: one record per id, lists hold ids, views read records. Say why an unread count on a paged folder is carried from the server, not counted on the client.

for a senior

Show the rollback design: the change recorded once as a pending layer, removed on failure, so every view reverts and changes that arrived meanwhile survive. Say when refetching beats patching.

for a principal

Weigh normalising against invalidate-and-refetch across the whole app: shape cost, eviction, team understanding, and which entities are shared enough to earn a single record.

## Why the views disagree In a web email client the same fact, "this conversation is unread", appears in at least three places: the **bold row** in the message list, the **header** of the open thread, and the **unread count** beside the folder name. The bug in the question appears when each view keeps a **copy** of the fact from the response that fed it: - the message-list response embeds a summary of each conversation, including its read flag; - the thread response carries its own header with the same flag; - the folder-list response carries a precomputed count. Marking the thread read updates the thread's copy. Nothing tells the other two. The design question is not "how do I notify them" but **which views should hold copies at all**. ## One record per entity; lists hold ids **Normalising** shared entities means storing each conversation once, in a map keyed by its id, and letting every list refer to it by id instead of embedding it. The row and the header become two **views** of one record. ```json { "conversations": { "c-981": { "id": "c-981", "subject": "Q3 offsite", "unread": false, "labels": ["work"] }, "c-977": { "id": "c-977", "subject": "Invoice 4471", "unread": true, "labels": [] } }, "lists": { "folder=inbox&page=1": ["c-981", "c-977"] }, "folderCounts": { "inbox": { "unread": 12, "asOf": "server" } } } ``` With this shape: 1. Marking `c-981` read changes one field in one record. 2. The list row and the thread header both re-read that record and agree immediately. 3. A search result list that also contains `c-981` agrees too, without anyone having to remember it exists. The cost is real: responses must be split into records on arrival, lists must be re-joined on read, and a record that no list refers to any more must eventually be evicted. Whether the client cache does this normalisation for you, or keeps one entry per request, is a property of the cache; the design decision here is **which views read the shared record and which keep their own copy**. ## Derived or carried: the unread count | Option | How it works | Fits when | Breaks when | |---|---|---|---| | **Derive on the client** | count unread records in the folder | the client holds every conversation in the folder | the folder is paged; the client sees 50 of 4,000 | | **Carry from the server** | show the server's number | always correct after a fetch | the user's own write makes it stale until the next fetch | | **Carry plus local delta** | server's number, adjusted by `-1` or `+1` on the user's own writes | paged folders, frequent writes | another device's writes still need a refetch or a push | An email folder is paged, so derivation is wrong: the client would count only the loaded page. The count is **carried** from the server and adjusted by a local delta that is applied in the same change as the flag. ## Rolling a failed write back across every view Mark-as-read is usually applied before the server answers. If it then fails, every view that showed the change must revert. Three designs, in increasing robustness: - **Each view keeps an inverse patch.** Works until one view forgets; then the list is bold, the header is not. - **Restore a snapshot taken before the write.** Reverts everything, but also erases what happened in between, such as a new message that arrived during the request. - **Record the change once as a pending layer over the confirmed data.** Views read confirmed data with pending changes applied on top. Failure drops that one pending change; every view reverts together, and unrelated changes survive. The third is the design that the normalised shape makes cheap: there is exactly one place the change was written, so there is exactly one place to remove it. The details of the optimistic write itself, and which writes deserve one, are a separate decision. ## When refetching is the better answer Normalising is not free, and it is not always needed: - If the affected views are few and the requests cheap, **invalidate the list, thread and folder entries and refetch**; all three converge on the server's truth. - The cost is a request per view and a brief flicker, which may be acceptable for rare writes and unacceptable for a flag toggled on every open. - Many apps combine both: patch the shared record for the instant feedback, then refetch the count to replace the local delta with the server's number. In the round, name the copies, collapse them to one record, say why the count is carried rather than derived, and show that rollback is one removal. That sequence is what the interviewer is listening for.

  • A new message arrives in another folder while mark-as-read is in flight, and the write then fails. What should the user see?
    The read flag reverts and the new message stays. That is why the change is held as a pending layer over confirmed data rather than undone by restoring a pre-write snapshot: removing one pending change leaves everything that arrived meanwhile untouched. A snapshot restore would silently erase the new message until the next refetch.
  • Should the search results list hold its own copy of each conversation?
    It should hold ids and read the shared records, so a conversation marked read in the thread is not bold in search. The search results themselves, which ids in which order, stay a separate cached list keyed by the query; only the conversation data is shared.
  • When would you leave a view holding its own copy on purpose?
    When it shows a deliberate point-in-time value, such as a sent-message confirmation or an audit trail that must show what was true then. Those are snapshots by design; the rule is that any view meant to show the current truth reads the shared record.

An airport does not update each departure screen by hand when a gate changes: every screen reads one schedule, so the gate is corrected once and every screen follows, including the screens nobody remembered existed.

  • Says the fix is notifying each view, without asking why each view holds a copy
  • Derives the folder's unread count from the conversations loaded on the current page
  • Restores a pre-write snapshot of the whole mailbox when a write fails
  • Says normalising entities is free and should be applied to every response
  • Lets each view keep its own copy and refetches all of them every few seconds