← Questions

Read-through and write-through cache adapter

0
HarveyCoding

A coding interview question asked at Harvey. It tests caching, adapter and design.

Callers read and write records in a slow backing store. Put a cache in front of it with the same interface. On a read, return the cached value if there is one; on a miss, read through to the store, keep the result and return it. On a write, write through: update the store and the cache together, so a later read never sees stale data from this process.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • The backing store: store.get(key: str) -> object | None, store.put(key: str, value) -> None, store.delete(key: str) -> None. Any call may raise.
  • CacheAdapter(store, capacity: int) wraps it and offers the same three methods.
  • get(key): on a hit, no store call; on a miss, one store.get, and the result is cached.
  • put(key, value): calls store.put first; only if that succeeds is the cache updated.
  • delete(key): deletes from the store, then drops the key from the cache.
  • When the cache holds capacity keys, adding one evicts the least recently used.

Example

store = {"a": 1}                    # a fake store that counts its calls
cache = CacheAdapter(store, capacity=2)
cache.get("a")       ->  1          # miss: 1 store read
cache.get("a")       ->  1          # hit: still 1 store read
cache.put("b", 2)                   # store now {"a": 1, "b": 2}
cache.get("b")       ->  2          # hit, no store read
cache.put("c", 3)                   # cache full: evicts "a" (least recently used)
cache.get("a")       ->  1          # miss again: 2 store reads in total
cache.get("zz")      ->  None       # miss: 3 store reads

Constraints

  • Up to 10^5 operations; get and put should be O(1) apart from the store call.
  • Values are opaque objects.

Clarifying questions

  • Is this process the only writer to the store, or can other writers change it underneath us?
  • Should a miss for a key the store doesn't have (None) be cached?
  • What should happen if store.put raises: leave the cache as it was, or invalidate the key?
  • Is there a capacity or a time-to-live?
  • Will it be called from several threads?

What a strong answer covers

  • A hash map plus a recency order (OrderedDict or a doubly linked list) for O(1) hits and LRU eviction.
  • The write order: store first, then cache, so a failed write never leaves the cache ahead of the store.
  • A choice on caching misses (negative caching), with its staleness risk.
  • Thread safety: a lock around cache state, and how to avoid many threads reading the same missing key from the store at once.
  • Tests with a fake store that counts calls and can be told to fail.

Follow-ups

  • Add a time-to-live per entry.
  • Other services write to the store too. How do you keep the cache from serving stale data (TTL, invalidation events, versioned values)?
  • Change it to write-back: writes go to the cache and are flushed later. What can be lost?
  • Collapse concurrent misses for the same key into one store read.

Where it's asked

What it tests

caching · adapter · design

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding