A product catalogue is cached with a 10-minute TTL. Merchandisers complain that price changes take 10 minutes to appear. What do you propose?
Show the full answer Hide the answer
What the interviewer is testing
Whether you can move from time-based to event-based invalidation and understand what that costs.
The trade being made
A TTL is a blunt instrument: it trades staleness against origin load, uniformly, regardless of whether anything changed. Ten minutes of staleness is the price of not asking the origin.
Lowering the TTL reduces staleness and raises origin load proportionally — and for a catalogue read far more often than it is written, that is a poor exchange.
The proposal: event-based invalidation
When a price changes, publish an event and invalidate the affected cache entries. Then the TTL becomes a safety net for missed invalidations rather than the primary mechanism, and it can be raised substantially — reducing origin load and staleness at the same time.
The implementation:
- The catalogue service publishes a change event, ideally through an outbox so the event and the price change commit atomically
- A consumer invalidates the specific keys — the product, and any aggregate entries containing it, which is the part usually missed
- The TTL stays as a backstop, now measured in hours
What it costs
Key tracking. You must know which cache entries contain a given product — the product page, the category listing, the search result, the recommendation block. This is the real work, and getting it wrong means the product page updates while the listing does not.
Distributed invalidation. With multiple cache layers — CDN, application cache, client — each needs invalidating, and the CDN's purge has its own latency.
Missed events, which the TTL backstop covers, and which should be monitored rather than assumed.
What a strong answer adds
Versioned cache keys as a simpler alternative to purging: include a version in the key, and a price change bumps the version so the old entry is simply never requested again and expires naturally. This avoids distributed purge entirely and works particularly well at the CDN layer.
Common weak answers
Reducing the TTL to 30 seconds, which multiplies origin load twentyfold. Removing caching.