In a mobile city-guide app, what are the memory and disk caches each for, and in what order is a venue looked up?
answer
- what each tier survives
- the screens in use right now
- a relaunch with no network
- cheapest tier asked first
- a disk hit moves up a tier
basics
~20 sThe memory tier holds parsed data for the screens in use and dies with the process; the disk tier survives relaunches but the OS may purge it. Lookups try memory, then disk, then network, copying hits upward.
solid answer
~50 sMemory is the fast tier: parsed venue objects for the screens the user has open or just left, served with no I/O or parsing, but gone when the process ends. Disk is the tier that survives a relaunch: serialized venue pages, search results and hours, so a cold start or a dead zone still shows something; a hit costs a read and a parse, and if it lives in the OS's purgeable cache folder the OS may delete it when storage is low, so nothing there may be the only copy. A lookup goes **memory, then disk, then network**: a disk hit is parsed and promoted into memory, and a network response goes into memory for the screen and is written to disk in the background. Each tier has its own byte budget; whether a hit is fresh enough to show is a separate staleness policy.
code
pseudocode · 15 linesfunction get_venue(id):
entry = memory_tier.get(id)
if entry exists:
return entry
bytes = disk_tier.read(id) # may be missing: the OS can purge it
if bytes exists:
entry = parse(bytes)
if entry is valid:
memory_tier.put(id, entry) # promotion
return entry
disk_tier.remove(id) # corrupt or old format: treat as a miss
entry = network.fetch_venue(id) # fails when offline: the screen shows its offline state
memory_tier.put(id, entry)
in background: disk_tier.write(id, serialize(entry))
return entrygo deeper
Recall the two tiers by what they survive: memory dies with the process, disk survives a relaunch but can be purged by the OS. Then recite the lookup order: memory, disk, network.
Explain the cost of each hit: memory skips I/O and parsing, disk costs both, a miss costs the radio. Show how a disk hit is promoted and how a response fills both tiers.
Show judgment about what may live in a disposable tier at all: nothing the user created, nothing the product promises offline unless it is kept outside the purgeable folder, and a separate budget per tier.
Treat the tiers as a cost ladder tuned per kind of data across a team's apps: which data earns memory, which earns disk, and which should never be cached on a shared device.
A mobile app that shows venue pages, search results and saved lists keeps copies of that data so it does not have to ask the network every time. On a phone those copies live in two **tiers** in front of the network, and each tier exists because of something the other cannot do. ## Why a phone keeps two tiers The **memory tier** is a map inside the app's own process: parsed venue objects ready to bind to a screen. A hit costs no I/O and no parsing, which is what makes going back to the venue the user just left feel instant. Its weakness is that it lives and dies with the process. When the OS ends the app in the background, which both native platforms do routinely to reclaim memory, the memory tier is gone, so it never helps a cold start. The **disk tier** is files, or rows in a small database, holding the serialized form of the same data. It survives the process ending, so a relaunch in a basement café still has something to show. A hit costs a file read and a parse: slower than memory, but far faster and cheaper than the radio. Its weakness is that it is disposable by design. When it sits in the OS's **purgeable cache folder**, as a cache normally should, the OS may delete it when storage runs low, so nothing there may ever be the only copy of anything. The **network** is the origin. It is the slowest tier, costs battery and data, and may be missing altogether. | Tier | Holds | Survives | Cost of a hit | Who can empty it | |---|---|---|---|---| | Memory | parsed objects for the screens in use | nothing beyond the process | almost none | the app's eviction; the process ending | | Disk | serialized entries for recent and likely screens | relaunch and process death | a file read plus a parse | the app's eviction; the OS when storage is low | | Network | the source data | not applicable | latency, radio time, data | not applicable | ## The lookup path A read walks the tiers from the cheapest to the most expensive: 1. Ask the **memory tier**. On a hit, return the object. 2. On a miss, ask the **disk tier**. A missing or unreadable file is simply a miss, never an error. On a hit, parse it, put the parsed object into the memory tier (**promotion**) and return it. 3. On a miss there too, ask the **network**. Put the response into the memory tier for the screen, and write it to the disk tier in the background so the next relaunch can use it. 4. If the network is unavailable, the screen shows its offline state instead of an endless spinner. Writing to disk off the main thread is safe precisely because the disk tier is a cache: if the process dies before the write lands, the next launch misses and refetches, and nothing is lost. Data the user created is a different matter and never belongs in this path at all. ## Each tier has its own budget The tiers differ in size by an order of magnitude or more, so each gets its own byte ceiling and evicts on its own. A venue evicted from memory is usually still on disk; a venue evicted from disk is gone until the network brings it back. Once the app has been used for a while, the disk tier holds many more entries than the memory tier. Sizing those ceilings from the device's budget, and choosing what each throws out first, is the next question an interviewer asks. ## What goes wrong when the tiers are confused - **Treating the disk tier as permanent**, and keeping the only copy of a saved list there, loses that list the day the OS purges the folder. - **Treating the memory tier as if it survived a background kill** produces an empty screen after a relaunch, where a disk hit would have shown the last known venue page. - **Skipping the memory tier** makes every back-navigation pay a file read and a parse, which a 2 GB phone shows as a visible delay. - **Seeing caches as an offline feature only** misses their main job while online: speed, battery and data. - **Deleting the disk copy after promotion** throws away the one tier that would have survived the next kill. ## What this question leaves to other parts of the design - Whether a cached entry is still **fresh** enough to show, and when to revalidate it, is a separate staleness policy decided per screen; a disk hit is not automatically stale. - Image caches, decoding and downsampling have their own pipeline and their own budget. - What deserves the **persistent store** rather than a disposable cache is a store-choice question; the short version is that anything the user made goes there. In an interview, the graded answer names both tiers by what they survive, walks the lookup order with promotion, and says out loud that the disk tier can vanish.
- Why not skip the memory tier and read venue pages from disk every time?Every screen visit would pay a file read and a parse, so going back to the venue the user just left costs a visible delay on a slow 2 GB phone. The memory tier exists to make that return instant. Skipping the disk tier instead would lose everything at each relaunch, so the cold start would wait for the network.
- Should a network response be shown before it has been written to disk?Yes. Show it once it is parsed and write it to disk off the main thread. The disk write is a cache fill, not a durability step: if the process dies first, the next launch misses and refetches. Data the user created is different; it belongs in the persistent store, never in a cache write that may not land.
Papers on your desk, a filing cabinet in the room, and an archive across town: the desk is cleared when you leave, the cabinet is still there tomorrow unless the building manager empties it to free space, and the archive always has the original but takes a trip.
- The disk cache is permanent, so a saved list can live only there.
- The memory cache is still there after the OS kills the app in the background.
- Reading from disk costs about the same as reading from memory on a modern phone.
- Caches only matter offline, so an online app barely needs them.
- Once an entry is promoted to memory, its disk copy can be deleted.