skip to content

In a mobile community-board app returning after eight hours in the background, the fresh first page does not reach the newest cached post; what should the client do?

level: seniorimportance: must knowfreq 50%

answer

  1. two runs, not one list
  2. no shared id means unknown posts between
  3. fill, mark or discard; never splice
  4. page down from the fresh page's cursor
  5. stop filling at the first overlap

basics

~20 s

Treat it as a gap: posts between the fresh page and the cached ones are unknown. Replace the cached window with the fresh page, or keep both with a 'load more' gap row filled until an id overlaps; never join them.

solid answer

~50 s

A refresh is safe to merge only when the fresh first page **overlaps** the cache — when at least one of its post ids is already stored at the head of the feed. Here it does not, so an unknown number of posts sits between the two runs. The client has three honest options: **replace** the cached window with the fresh page in one transaction (simplest, and right when the reader has no place to keep); keep both runs with a **gap row** that loads pages from the fresh page's cursor until a page contains a cached id; or **auto-fill** a bounded number of pages and then fall back to one of the two. What it must never do is splice the runs as if they were contiguous, so the reader scrolls from this morning's posts straight into last night's and silently misses everything between.

code

pseudocode · 18 lines
pseudocode
on_refresh(fresh, store):
  if any id in fresh.items is in store.head_run:
    merge_by_id(fresh, store)                 // the runs touch
  else if fresh.next_cursor == none:
    replace_window(store, fresh)              // the whole feed fits in one page
  else if keep_cached_run:
    in one transaction:
      merge_by_id(fresh, store)
      store.insert_gap(after = fresh.last_id, cursor = fresh.next_cursor)
  else:
    replace_window(store, fresh)              // delete old rows, insert fresh, one transaction

fill_gap(gap, store):
  page = fetch(gap.cursor)
  if any id in page.items is in store.run_below(gap):
    in one transaction: merge_by_id(page, store); store.delete_gap(gap)
  else:
    in one transaction: merge_by_id(page, store); gap.cursor = page.next_cursor

go deeper

for a junior

Recall that a fresh first page which shares no posts with the cache leaves posts unknown in between. Those two lists must never be joined as if they were one.

for a middle

Explain the overlap check, the case where no next cursor means nothing is missing, and why the gap is filled from the fresh page's cursor rather than the cached one.

for a senior

Choose between replace, gap row and bounded auto-fill from the reader's place and the feed's ordering, keep per-run cursors, and stop filling only on an overlap.

for a principal

Decide the policy across surfaces: which feeds may discard cached runs, how much background filling the data and battery budget allows, and what the server's ordering guarantees.

## What a gap is The community-board app keeps the feed in a **local store**: posts keyed by their server id, in the order the server gave them, plus the **cursor** (the server's opaque "continue from here" token) needed to page further down. A resident reads in the evening, puts the phone away, and opens the app the next morning. Overnight, neighbours have written sixty posts. The app asks for the first page and gets the newest twenty. None of those twenty is in the store. The newest cached post is at least forty posts below the bottom of the fresh page — and the client does not know how many. Between the **fresh run** (the new page) and the **cached run** (last night's posts) lies a **gap**: posts that exist on the server and that the phone has never seen. A refresh after five minutes is different: the fresh page's lower half repeats posts already cached, the overlap proves the two runs touch, and merging by id is enough. ## Detecting the gap 1. Fetch the first page as for any refresh. 2. Check for **overlap**: does any post id in the fresh page already sit in the cached head run? For a strictly chronological board you can also compare positions — if the oldest fresh post is still newer than the newest cached post, they cannot touch. 3. If they overlap, merge by id and stop. If the fresh page carries no next cursor, the whole feed fits in it, so nothing can be missing: the fresh page *is* the feed. 4. Otherwise there is a gap. Record it explicitly — for example as a gap marker between the two runs that stores the fresh page's next cursor. ## Three ways to resolve it | Option | What the reader sees | Cost | Fits when | |---|---|---|---| | **Replace** the cached window | the fresh page; old posts gone from the feed | old posts must be refetched to be seen again | the reader had no place to keep, or the server's order changed | | **Gap row** ("load more") | fresh posts, a row saying more exist, then the cached posts | an extra row type and per-run cursors | the cached posts still matter: a reader's place, offline reading | | **Auto-fill**, bounded | the feed fills in the background, then one of the above | extra requests, data use and battery | the gap is usually small and the network is good | Replacing is not a failure. One platform's own paging guide shows exactly this shape: on a refresh, it deletes the cached rows for that list and inserts the fresh page inside one transaction, so nothing reading the store sees a half-replaced feed. After eight hours away, most readers have no place to return to, and the simplest correct design wins. ## Keeping two runs honest If the design keeps both runs, the bookkeeping must say they are separate: - **Each run keeps its own cursor.** The gap is filled by paging *down* from the fresh page's next cursor. The cursor stored at the bottom of the cached run continues past *older* posts and cannot reach the gap above it. - **Filling stops at the first overlap.** When a fetched page contains an id that is already in the cached run, the runs touch: merge that page by id, delete the gap row, and the feed is contiguous again. - **A cap bounds the cost.** Auto-filling three pages and then leaving the gap row in place keeps a week's absence from turning into a hundred requests. Stopping at the cap does not close the gap; the row stays. - **Dedup still applies.** A post bumped by new replies can appear in both runs; the store holds it once, in its newer position. ## When the server's order changed while the app was away A board ordered by latest activity re-ranks constantly. After eight hours the cached run's internal order is stale too: posts in it have been bumped above the fresh page, and posts that sat at the top may now belong far down. A gap row cannot fix that — it promises that the cached run is still a correct slice of the feed. For a re-ranked feed, replacing the window is usually the honest answer; for a strictly chronological board, the cached run stays correct and a gap row can keep it. ## What the reader sees - While the check runs, the cached feed is shown as it was; how long cached data may be shown before it counts as stale is a staleness policy, decided per screen. - A gap row reads as an action ("Show 40+ newer posts"), not an error. - If the reader had left the app mid-feed and returns to that place, the fresh run is staged above them behind a "new posts" pill, and the gap sits between the staged posts and their position — never inserted so that it moves what they are reading.

  • Why can't the cursor saved at the bottom of the cached run fill the gap?
    That cursor means 'continue after the oldest cached post'. It walks further into the past, away from the gap. The missing posts lie between the fresh page and the cached head, so only the fresh page's next cursor reaches them.
  • The gap fill has fetched three pages and still found no overlap; what now?
    Stop and leave the gap row in place, now pointing at the latest cursor. Three pages is a cost bound, not proof of contiguity. Alternatively, if the gap keeps growing, replace the window: a reader is unlikely to scroll through a hundred posts to reach last night's.
  • How does a re-ranked feed change the decision?
    In a feed ordered by activity, the cached run's own order is stale after hours away, and some of its posts now belong above the fresh page. A gap row would present that stale slice as correct, so replacing the window is usually right.

  • Prepend the fresh first page to the cached posts and carry on
  • A fresh first page with no cached posts in it means the server is broken
  • Fill the gap by paging from the cached run's saved cursor
  • Cached posts missing from the fresh page must have been deleted
  • Throwing away the cached feed after hours away is always wrong