On a 2 GB phone, how would you size a city-guide app's memory and disk caches, and what would each evict first?
answer
- the device, not the catalogue
- a share of the app's own budget
- entries differ two hundredfold
- a ceiling the app sets itself
- one search session, forty venues
basics
~20 sSize each tier from a stated device budget: memory as a share of the app's memory budget, disk as an app-set ceiling cut when storage is low. Bound both in bytes; evict least recently used, and age out untouched entries.
solid answer
~50 sI size from the device. Say the app should stay near 200 MB on a 2 GB phone: the memory tier gets about a tenth, **20 MB**, which holds roughly 200 venue pages at 100 KB parsed, far more than the open screens need. The disk tier gets a ceiling the app sets itself, say **100 MB**, cut to a few percent of free space when storage is low; at 40 KB serialized that is about 2,500 venue pages. Entries range from a 2 KB hours record to a 400 KB venue, so both tiers are **bounded in bytes**, not entries. Memory evicts least recently used entries that no visible screen is using; disk first drops entries untouched for 30 days, then least recently used by bytes. To stop one long search session from flushing favourites, I'd put once-seen entries on probation before they can push others out.
code
pseudocode · 12 linesfunction put(tier, key, entry, size_bytes):
if size_bytes > tier.max_entry_bytes:
return # too big for this tier: the memory tier leaves it on disk only
if key in tier:
tier.total_bytes = tier.total_bytes - tier.size_of(key)
tier.remove(key)
tier.insert_as_most_recent(key, entry, size_bytes)
tier.total_bytes = tier.total_bytes + size_bytes
while tier.total_bytes > tier.max_bytes:
victim = tier.least_recently_used() # never the new entry: it is the most recent and fits
tier.total_bytes = tier.total_bytes - tier.size_of(victim)
tier.remove(victim)go deeper
Recall that each tier has a size limit and must throw something out when full, and that the limit comes from the phone, not from how much data the server has.
Do the arithmetic aloud: a stated app budget, a share for the memory tier, an app-set disk ceiling, entries per tier. Explain why the bound is in bytes and how least-recently-used eviction runs against a running total.
Name the phone-specific failures: a browsing burst flushing favourites, a count limit over 200-fold entry sizes, a ceiling that ignores free storage. Propose probation, per-entry caps and an age limit for bytes.
Treat cache budgets as a shared resource across features: who owns the 20 MB, how budgets are measured on the slowest supported phone, and how they shrink as the app grows.
Sizing a client cache starts from the device, not from the data. A city guide may know 40,000 venues; the real question is how much of a 2 GB phone the app may use, and what share of that the cache gets. ## Start from a budget you state out loud The numbers below are this scenario's own assumptions, chosen to show the method. Real values come from measuring the app on the slowest phone it targets, not from a platform constant. - **App memory budget:** the team decides the whole app should stay near **200 MB** of memory on a 2 GB phone. - **Memory tier:** about a tenth of that, **20 MB**, for parsed venue data. Images are not in it; they have their own pipeline and budget. - **Entry sizes:** a venue's hours record is about **2 KB**, a search-results page about **20 KB**, a full venue page with menu text and reviews about **100 KB** once parsed, and a few large venues reach **400 KB**: a 200-fold spread. - **Disk tier:** a ceiling the app sets itself, **100 MB**, lowered when the phone is short of space, for example to at most **5%** of free storage. On a phone with 1 GB free, that ceiling becomes 50 MB. The arithmetic: 20 MB holds about **200** full venue pages at 100 KB each, far more than the handful of screens the user has open, so the memory tier could be smaller without anyone noticing. Serialized entries are smaller than parsed objects, say **40 KB** for a venue page, so 100 MB of disk holds about **2,500** venue pages: every place a resident is likely to open in weeks. Both native platforms let the OS empty the cache folder when storage is low, but one platform's guide says plainly not to rely on the system to clean it, and one platform reports how much cache space is currently available to the app. The app's own ceiling is what keeps the cache from being the reason the phone runs out of space. ## Bound each tier in bytes, not entries When entries differ by a factor of 200, a limit of 500 entries means anything from 1 MB to 200 MB. So each entry records its size when it is stored, the tier keeps a running total, and eviction runs until the total is back under the ceiling. Exact in-memory object size is hard to measure at runtime, so estimate it from the serialized length times a factor measured once on the target device. Two more rules keep the tiers honest: 1. An entry larger than a set share of the memory tier, say a tenth (2 MB here), is kept on disk only, so one huge venue cannot flush dozens of small ones. 2. A **maximum age for bytes**: entries nobody has read for 30 days are deleted in a maintenance pass even below the ceiling. This is about storage, not about whether the data is still correct; correctness is the staleness policy's job. ## Recency or frequency on one person's phone | Policy | On a phone it... | Weak spot in this app | |---|---|---| | LRU by bytes | follows what this user is doing now, cheaply | one long search session opens dozens of venues once and pushes out favourites | | LFU | keeps venues opened often | holds on to last month's habits, admits new places slowly, needs counts that survive a relaunch | | Segmented LRU (probation and protected) | admits new entries on probation and promotes them on a second read | a little more bookkeeping | One person's reuse is dominated by recency, so LRU by bytes is a sound default. The failure worth naming is **scan pollution**: browsing forty search results opens forty venues once each. A small probation segment, or a separate small budget for search previews, stops that burst from evicting the venues on the user's saved lists. ## What goes first - From **memory**: the least recently used entries that no visible screen is using; they are still on disk. - From **disk**: entries past their maximum age, then the least recently used by bytes, and among similar entries the cheaper ones to refetch, such as old search-result pages, before full venue pages. ## Mistakes interviewers listen for - Sizing to the catalogue ("cache all 40,000 venues") instead of the device. - A count limit over entries that differ by orders of magnitude. - Taking all the free memory the device reports, which the OS and other apps need too. - Leaving disk cleanup to the OS, which purges only under storage pressure and on its own timing. - Calling the age limit a freshness rule, when here it exists to free bytes.
- Would you choose LFU instead, to protect the user's favourite venues?Not pure LFU. On one person's phone it keeps last month's favourites long after the user moved on, admits new places slowly, and needs counts that survive relaunches. A small probation segment for entries seen once, promoted on a second read, protects favourites from a browsing burst while still following the user. Giving search previews their own small budget works too.
- How do you know how many bytes an entry takes in the memory tier?You estimate it. Exact object size is hard to measure at runtime, so record the serialized byte length when the entry is stored and multiply by a factor measured once on the target phone. The budget only has to be roughly right; what matters is that a 400 KB venue counts two hundred times a 2 KB hours record.
- What changes when the phone is almost out of storage?The disk ceiling drops: derive it from free space as well as from the fixed cap, and run eviction at launch when space is low. One platform reports how much cache space is currently available to the app, which is cheap to respect. Both platforms may purge the cache folder anyway, so a lower ceiling just makes the app the polite tenant.
- Size the cache to hold the city's whole venue catalogue.
- Bound each tier by entry count, since entry sizes average out.
- The OS cleans the cache folder, so the app needs no disk ceiling of its own.
- The memory cache may use whatever free memory the device reports.
- Deleting entries by age is the same decision as marking them stale.