skip to content

In a mobile mail client that syncs incrementally, why must deleted messages arrive as tombstones rather than simply being left out?

level: juniorimportance: should knowfreq 40%

answer

  1. a delta lists only what changed
  2. silence cannot mean deleted
  3. a page is not the whole set
  4. deleted ids travel explicitly
  5. an unknown deleted id is a no-op

basics

~20 s

An incremental pull returns only what changed, so a message missing from it is simply unchanged. The server must name each deleted message's id as a tombstone; otherwise the phone keeps showing mail that is gone everywhere else.

solid answer

~50 s

A delta pull answers "what changed since my token": created, updated and deleted. A deleted message has no record left to send, and leaving it out looks exactly like "not changed", so the client would keep it forever. The server therefore keeps a **tombstone** — the id plus the fact of deletion — and returns those ids in the change response. The client removes the local row together with its cached body and attachments in one transaction, and treats a deleted id it never stored as a no-op. Absence is never evidence, not even inside one page of results: the next page may carry the record. Tombstones are kept only for a bounded time, so a client away longer than that cannot be told what it missed and must fall back to a full resync.

code

json · 8 lines
json
{
  "oldToken": "t-8812",
  "newToken": "t-8840",
  "moreChanges": false,
  "created": ["m-5001", "m-5002"],
  "updated": ["m-4410"],
  "deleted": ["m-3977", "m-4102"]
}

go deeper

for a junior

Recall the core idea: a delta lists changes, so a deletion needs its own explicit signal, an id the server reports. Absence from a response never means deleted.

for a middle

Explain the client side: delete the row and its cached body and attachments in one transaction, treat unknown deleted ids as no-ops, and never infer deletion from one page.

for a senior

Show that tombstones expire: a client away longer than the server keeps them cannot catch up incrementally and needs a full resync that protects pending writes.

for a principal

Treat the retention window as a contract between teams: longer tombstone retention costs server storage, shorter forces more full resyncs from phones away for weeks.

## What an incremental pull returns A mobile mail client cannot download the whole mailbox every time it wakes up: thousands of messages across several folders would cost minutes of radio time and megabytes of data to learn that three flags changed. So it syncs **incrementally**. It stores a **sync token** — an opaque string the server issued that names the state the client last caught up to — and asks, "what changed since this state?" The server answers with three kinds of change: - **created** — messages that did not exist at that state; - **updated** — messages whose flags, folder or content changed; - **deleted** — messages that existed and no longer do. Some services return only ids in each list and let the client fetch the changed records afterwards; others return the records inline. Either way, the response describes *differences*, not the whole collection. ## Why absence cannot mean deleted The first two lists describe things that still exist, so the server has a record to send. A deleted message has nothing left to send. If the server just omitted it, the client would see the same response as for a message that did not change at all — and it would keep showing it, possibly forever. Other devices would have moved it to the trash and emptied it, while this phone still lists it in the inbox. The fix is a **tombstone**: when a message is deleted, the server keeps a small record saying "id `m-3977` was deleted at this point in the change history". A delta pull that spans that point reports the id in its deleted list. Deletes become *positive information* — an id the client is told about — instead of a guess from silence. The same reasoning rules out another tempting shortcut. When a large change set arrives in **pages** ("more changes to come"), a message missing from one page says nothing; it may be in the next. A client that deletes "whatever this page did not contain" deletes mail it simply had not been told about yet, then downloads it again a page later. ## What the client does with a tombstone 1. Delete the local row for that id, together with everything that hangs off it — the cached body, attachments, search-index entries — in the **same transaction** that applies the rest of the page, so a kill cannot leave half a message behind. 2. If the id is unknown locally, do nothing. A message created and deleted while the client was away may be reported only as deleted, or not at all; deleting something absent must be a harmless **no-op**, never an error. 3. If the id has a pending, unsent edit in the outbox, do not silently delete the row under it. That is an edit-versus-delete case, and the branch's write-conflict policy decides what happens. | Signal in the change response | What it means | Client action | |---|---|---| | id in created | new since the token | insert (or fetch, if ids only) | | id in updated | changed since the token | upsert by id | | id in deleted | gone since the token | delete row and its cached content | | id in deleted, unknown locally | created and removed while away | no-op | | id not mentioned | unchanged since the token | keep as is | ## Tombstones do not live forever A tombstone costs the server storage, and a mailbox with years of deletions would accumulate millions of them. So servers usually keep change history, tombstones included, only for a bounded window — a few weeks of changes, or a cap on how many. How the server stores and expires that history is the server's own design. The client's half is what happens when it has been away longer than the window: the server can no longer compute a correct delta from its token, because the deletes it would need to report are gone. It rejects the token, and the client must fall back to a **full resync** — comparing its whole local copy with a full listing from the server, without losing writes still in its outbox. ## Moves and the scope of a token In a mail client a "move to Archive" can look different depending on scope. If the client syncs each folder as its own collection with its own token, a move appears as a delete from one folder and a create in another. Applying changes by **stable message id** rather than by folder position lets the client move the existing row and keep its cached body instead of deleting and re-downloading it. If one token covers the whole account, the same move usually arrives as an update to the message's folder field. ## The short version - Deletion is a change that leaves no record behind, so it needs its own explicit signal. - Only a complete listing proves absence; a delta or a single page never does. - Applying a delete must be idempotent, and must respect pending local writes. - Tombstone retention bounds how long a client can stay away and still catch up incrementally.

  • What should the client do when a tombstone names a message that has a pending, unsent edit in its outbox?
    It is an edit-versus-delete case, so the branch's write-conflict policy decides — restore the message, drop the edit with a notice, or ask the user. The sync code's job is only to avoid deleting the row underneath a queued write: it flags the record and hands it to that policy instead of applying the delete blindly.
  • Why might a message moved between folders arrive as a delete plus a create?
    When each folder is synced as its own collection with its own token, a move leaves one collection and enters another, so the first reports a deletion and the second a creation. Applying both by stable message id lets the client move the existing row and keep its cached body rather than deleting it and downloading it again.

A library's weekly bulletin lists new books, changed shelf locations and withdrawn titles. A reader keeping a home catalogue would never notice a withdrawal if the bulletin only listed what is still on the shelves; the withdrawn list is what keeps the home copy honest.

  • If a message is not in the change response, the client can delete it locally.
  • Deleting whatever one page of results did not contain is safe.
  • A deleted id the client never stored is a sync error to report.
  • The server can drop deletion records the moment it deletes the message.
  • Tombstones are kept forever, so a client can always catch up incrementally.