In a mobile news app that keeps its feed pages on disk, how do you bound that window, and what happens when the reader scrolls back to evicted pages?
answer
- every commute adds pages
- a small memory window, a capped disk window
- trim whole pages from an edge
- keep the cursor at the new edge
- a re-fetched page may differ
basics
~20 sCap the stored window and trim whole pages from its far edge, never the middle, keeping the cursor at the new edge. Pages dropped from memory reload from disk; pages dropped from disk are re-fetched by cursor and may come back different.
solid answer
~50 sWithout a bound, a daily commuter adds hundreds of articles a week and the feed table only grows. I keep two windows: a small one in memory around the reader, which a memory-pressure signal can shrink because every page is also on disk, and a capped one on disk, say 10 pages or about 200 articles, an assumption sized to an instant relaunch. Trimming removes whole pages, with their page records, from the edge farthest from where the reader resumes, so the rest stays contiguous and the new edge keeps the cursor that continues past it. If the reader scrolls back into trimmed pages, the app requests each again with its stored cursor if the contract still honours it, removes articles already shown elsewhere by id, and falls back to the head of the feed if the cursor is rejected.
go deeper
Recall that the saved feed needs a size limit, that pages far from the reader are removed first, and that they are loaded again if the reader goes back.
Explain the memory window versus the disk window, which signal shrinks each, and why pages are trimmed whole and only from an edge.
Size the cap from stated assumptions, keep the cursor chain intact at the new edge, and handle a re-fetched page that differs or a cursor the server rejects.
Decide which edge each feed surface trims based on where its readers resume, and whether one storage budget is shared by all the app's feeds or split per surface.
## Why the window needs a bound A stored window is what makes the news feed appear at once on relaunch. Left alone it also grows without limit: every page fetched is a page written, and nothing removes it. With stated assumptions, about **2 KB** of text and metadata per article (images live in the image cache), **20** articles per page and a commuter reading **3 pages a session, 4 sessions a day**, the feed table gains about 240 articles, roughly **480 KB**, a day and about **14 MB** a month. The bytes are modest; the other costs are not: - queries over the window slow down as it grows, and the relaunch query runs on every launch; - most of those articles are days old and will never be shown again; - trimming at the wrong place breaks the chain of cursors the window depends on. ## Two windows, two bounds The feed keeps pages in two places, with different bounds and different costs to bring back. | | Memory window | Disk window | |---|---|---| | Holds | Pages around the reader's position | The stored feed used for relaunch and offline reading | | Assumed bound | About 5 pages, 100 articles | About 10 pages, 200 articles, plus a hard ceiling mid-session | | Shrunk by | Distance from the viewport; a memory-pressure signal | A row or byte cap, applied when the app goes to the background | | Cost of bringing a page back | A local read | A network request with the page's stored cursor | The memory window is small because a page that is also on disk is cheap to reload. Both native platforms send a memory-pressure signal, and the guidance on one is explicit: free what has an on-disk representation and can be reloaded later, because failing to reduce memory can end in the app being terminated. Feed pages kept in the store are exactly that kind of data. ## Trimming without breaking the cursor chain Every stored page ends with the cursor that continues after it. That chain is why trimming is done by **whole pages**, from an **edge**, never from the middle: 1. **Pick the edge.** Trim the edge farthest from where the reader will resume. A news app that reopens at the top trims the **tail**, the deepest pages, when it goes to the background. 2. **Delete whole pages**, their articles and their page records together, in one transaction, so no page record survives without its articles and no article survives without its page. 3. **Re-point the edge.** After a tail trim, the last remaining page's `next_cursor` is the load cursor again, so scrolling down continues exactly where the trimmed pages began. 4. **During a very long session**, when the reader is deep and the store hits its hard ceiling, trim the **head** instead, but keep a placeholder record for each trimmed page holding its `request_cursor`, so the gap can be filled if the reader scrolls back. Removing a page from the middle, or deleting individual articles the reader has passed, leaves a hole that the surrounding cursors no longer describe: the page above points to content that is gone, and the list shows a jump with no way to fill it. ## When the reader comes back - **Into pages dropped from memory only:** read them back from the store. No network, no visible change. - **Past the trimmed tail:** this is ordinary forward paging with the last page's `next_cursor`. - **Into a trimmed head placeholder:** request the page again with its stored `request_cursor`, if the contract still honours it. The answer is the server's **current** page for that cursor, not a replay: articles may have been added, removed or re-ranked. Write it into the placeholder's place and drop articles already stored elsewhere in the window by `article_id`. - **If the cursor is rejected:** the hole cannot be filled in place. Offer a way back to the top of the feed, which is a refresh of the head and belongs to the refresh design. ## What to avoid - An unbounded store "because disk is cheap". - Trimming while the reader scrolls through the pages being trimmed; trim on background or far from the viewport. - Leaving page records behind after their articles are deleted, which resurrects cursors that point at nothing. - Expecting a re-fetched page to match the one that was trimmed. How large each window should be is never a standard figure; derive it from the scenario's own numbers and say they are assumptions.
- The OS signals memory pressure while the reader is deep in the feed. What do you release?Feed pages far from the viewport that are already stored on disk, since they can be reloaded with a local read; the pages on screen and next to it stay. That follows the platform guidance to free whatever has an on-disk copy. Decoded images and prepared rows near the viewport are trimmed by the list and media designs, not by the paging window.
- Why trim the disk window when the app goes to the background rather than after every page?Because the reader is not looking, so deleting pages cannot move the list under them, and one batched delete is cheaper than one per page. Trimming while scrolling risks removing pages the reader is about to return to. A hard ceiling still applies mid-session so one very long read cannot grow the store without limit.
- Feed rows on disk are cheap, so the stored window can grow without limit.
- Evicting a page from the middle is fine once its articles are deleted.
- Re-fetching a trimmed page with its old cursor returns exactly the same articles.
- A page dropped from memory has to be fetched from the network again.
- Page records can stay behind after their articles are trimmed.