In a mobile app's infinite feed, why can a refresh show the same post twice, and why should the client merge posts by stable id?
answer
- the feed moves between requests
- a fresh first page overlaps the cache
- appending pages copies, it does not merge
- one stored row per server id
- a repeat updates, it never adds
basics
~20 sA fresh first page overlaps the cached head, and a bumped or re-ranked post can come back in a second page. Appending pages copies those posts; storing each post once, keyed by its server id, turns a repeat into an update.
solid answer
~40 sDuplicates come from treating pages as lists to concatenate. A refresh's first page nearly always contains posts that are already cached, and on a community board a post that gets new replies can be bumped and come back in a later page too. The fix is identity: store each post in the local store keyed by its server-issued id, write every incoming page as an *upsert*, and let the screen show one row per id. When an id arrives again, the newer copy replaces the older one's fields and the post keeps a single row. A row index or a position inside a page is not identity — it changes whenever something is inserted above — so it cannot tell a repeat from a new post.
code
pseudocode · 9 linesmerge_page(page, store):
in one transaction:
for post in page.items:
stored = store.find(post.id)
if stored == none:
store.insert(post)
else if post.revision >= stored.revision:
store.update(post.id, post.fields, post.feed_position)
// else: a late, older copy; keep what is storedgo deeper
Recall that the feed changes between requests, so pages overlap. The fix is to store each post once under its server id and treat a repeat as an update.
Explain the upsert in one transaction, the separate ordering field, and why an index or content hash fails as identity when posts are inserted above or edited.
Show the store as the single source of truth so every screen dedups once, and guard updates with a server revision so a late, older copy cannot undo a newer one.
Weigh where identity is guaranteed: a server id contract every surface relies on, versus each screen inventing keys, and what that costs when feeds, search and saved lists share posts.
## The scenario A neighbourhood community-board app shows residents an endless feed of notices, offers and questions. The app keeps the posts it has fetched in a **local store** on the phone (an embedded database or similar), and the feed screen reads from that store. The server hands the feed out in **pages**: a request returns, say, 20 posts plus a **cursor**, an opaque token the client sends back to get the next page. A common first implementation keeps an array, appends every page that arrives to the end, and prepends the page a refresh returns. Within a day, residents report the same notice twice in a row, or a post they read an hour ago turning up again further down. ## Why the same post arrives twice The server's feed is not a frozen list. Between two requests it changes, and several ordinary events put one post into two responses: - **A refresh overlaps the cache.** Pull-to-refresh asks for the first page again. Unless more than a page's worth of posts was written since the last fetch, most of that page is already cached. - **Bumping and re-ranking.** If the board orders by latest activity, a post that gets a new reply moves up. It was cached in page 3 and now also arrives in page 1. - **Offset paging under inserts.** If the server's contract pages by offset rather than by a cursor, a new post at the top pushes the last post of page 1 into page 2, which then repeats it. How the cursor is built is the server's contract; the client only has to survive what it returns. - **A retried request.** A page request that timed out may have been answered; asking again returns posts already written. None of these is a server bug. A client that treats every page as new rows will show duplicates however good the server is. ## Pages are not the unit of storage The fix is to stop storing pages and start storing **posts**: 1. Give the local table a primary key that is the post's **server-issued id**, never a locally assigned row number. 2. Write every incoming page as an **upsert** inside one transaction: insert ids the store has never seen, update the ones it has. 3. Keep the post's place in the feed in a separate ordering field (the server's rank or timestamp, or the position the latest fetch gave it), so a repeat can move a post without duplicating it. 4. Let the feed screen observe a query over that table. The list then shows exactly one row per id, whatever the network delivered. With the store as the **single source of truth** — the list reads the store and the network only writes into it — deduplication happens once, in one place, for every screen that shows posts. ## What a merge does with a repeat | Incoming post | What the merge does | Why | |---|---|---| | id not in the store | insert a row | it is genuinely new | | id in the store, newer data | update the fields in place | the reply count or title changed | | id in the store, older data | keep the stored copy | a late response must not undo a newer one | | id in the store, new position | update its ordering field | one row moves; nothing is added | "Newer" needs a way to tell: a **revision number** or update time that the server issues per post is the usual answer. A device clock is not, because it says when the phone received something, not when the post was written. ## Why a row index is not an identity Identity is what lets a list work out what changed. A list fed by stable identifiers can compare the old set of ids with the new one and compute **inserts, deletes and moves**; a list addressed by row position cannot, because positions change every time something is added, removed or rearranged above them. Two practical consequences for a feed: - An index-keyed list shifts every row below an insert, so the wrong rows animate and per-row state (an expanded preview, a half-typed reply) lands on the wrong post. - Content-based keys are no better: hashing the title makes an edited post look like a new one, and two residents posting the same "lost cat" notice collide. ## What deduplication does not solve Merging by id answers "is this post already here?". It does not answer "is anything missing between what I fetched and what I had?" — a fresh first page that shares no post with the cache is a **gap**, which is a separate decision. Nor does it decide whether new posts should appear above a reader who is mid-feed. Those are the next questions a design round asks, and both depend on the store already holding one row per id.
- The same post arrives in two pages at different positions; where should it appear?Once, at the position the most recent fetch gave it, because that is the server's latest view of the order. The row moves rather than duplicating, and because the list diffs by id it animates one move instead of a delete and an insert.
- Why deduplicate in the local store rather than only in the list on screen?Other screens read the same store: a saved list, a search result, a badge count. Deduplicating only in one list leaves duplicate rows on disk, inflates storage and counts, and brings the duplicates back on the next launch, which reads the store before any network call.
- Duplicate posts after a refresh mean the server has a bug
- Use the row's index in the list as the post's key
- Identify a post by hashing its title and text
- Skip any id already cached; the stored copy is still current
- Prepend the whole fresh first page to the cached list