Autocoder

Caching Interview Questions and Answers

Last updated 2026-11-19

Caching questions test whether you know both the benefit and the failure modes. Beyond "a cache makes reads faster", interviewers ask which write policy you would choose, how you keep data consistent, what happens when a hot key expires and how to compute the gain from a given hit ratio. Those are the answers below.

Redis is the most common implementation, covered in Redis questions. Caching is a building block in system design, and the related traffic layer is in load balancing. Study approach: how to crack a system design interview.

PoliciesCache-aside, read-through, write-through, write-back
Failure modesStampede, penetration, avalanche, stale data
LevelMedium

Where caches live

Caches exist at every layer: the browser, a CDN at the edge, a reverse proxy, an in-process cache in the application, a shared cache such as Redis or Memcached, and the database's own buffer pool. Each layer has a different invalidation story, so name the layer when you answer.

Hit-ratio arithmetic

Average access time equals hit ratio times cache time plus miss ratio times miss time. With a 1 ms cache, a 50 ms database read that is added on a miss (so a miss costs 51 ms) and a 95 percent hit ratio, the average is 0.95 times 1 plus 0.05 times 51, which is 3.5 ms, against 50 ms with no cache. Raising the hit ratio to 99 percent gives 1.5 ms. The last few percent of hit ratio matter a great deal.

Caching Interview Questions and Answers: questions with worked answers

  1. 1

    What is caching and why does it improve performance?

    easy

    Caching stores copies of data that is expensive to compute or fetch in a faster, closer place, so repeat requests skip the slow path. It reduces latency, lowers load on databases and services, and absorbs traffic spikes. It works because access patterns are skewed: a small set of items gets most of the reads (temporal and spatial locality). The costs are memory, stale data and extra complexity in keeping the cache correct.

  2. 2

    Compare cache-aside, read-through, write-through, write-back and write-around.

    medium

    Cache-aside: the app checks the cache, loads from the database on a miss and populates the cache; on writes it updates the database and invalidates the key. Read-through: the cache itself loads missing data from the database. Write-through: every write goes to the cache and the database synchronously, so reads are fresh but writes are slower. Write-back (write-behind): writes go to the cache and are flushed to the database later, which is fastest but risks data loss if the cache fails. Write-around: writes go straight to the database, skipping the cache, which avoids filling it with data that may never be read.

  3. 3

    Describe LRU, LFU and FIFO eviction and when each is best.

    medium

    LRU evicts the least recently used entry and suits workloads with temporal locality; it is the default in most caches. LFU evicts the least frequently used entry and suits stable popularity, such as a catalogue, but is slow to adapt when popularity shifts and needs aging. FIFO evicts the oldest inserted and is simple but ignores usage. Time-based TTL expiry complements them. Modern designs such as TinyLFU combine frequency and recency. An LRU build is shown in low-level design questions.

  4. 4

    How do you keep the cache consistent with the database?

    hard

    There is no perfect answer without cost, so choose by tolerance. TTLs bound staleness. On writes, update the database and delete the cache key (rather than updating it), so the next read reloads, which avoids two concurrent writers leaving the cache with an older value. A delayed second delete, or change-data-capture that invalidates from the database log, closes the window where a slow reader repopulates the old value. Versioned keys avoid invalidation altogether. For data that must never be stale, such as balances, read from the database.

  5. 5

    What is a cache stampede (thundering herd) and how do you prevent it?

    hard

    When a popular entry expires, many concurrent requests miss together and all query the database. Prevention: single-flight or a lock so only one request regenerates the value while others wait or receive the stale one; probabilistic early refresh before expiry; jitter on TTLs so keys do not expire at the same time; and warming the cache before known peaks. See the lock example in the Redis page.

  6. 6

    What is cache penetration and how does a Bloom filter help?

    medium

    Penetration is repeated requests for keys that exist nowhere, so every request misses the cache and hits the database; it can be accidental or an attack. Fixes: cache the negative result (a null marker) for a short TTL, validate input, rate limit, and put a Bloom filter of all valid keys in front. A Bloom filter says "definitely not present" or "probably present", so non-existent keys are rejected without touching the database. It has false positives but no false negatives, and standard versions do not support deletion.

  7. 7

    What is a cache avalanche and how is it different from a stampede?

    medium

    An avalanche is many different keys expiring at the same moment, or the cache cluster going down, so the database suddenly takes the full load. A stampede concerns one hot key. Defences: randomise TTLs, stagger warm-up, keep replicas of the cache cluster, add a fallback layer such as a local in-process cache, use circuit breakers and rate limiting to protect the database, and degrade gracefully.

  8. 8

    Local cache or distributed cache?

    medium

    A local in-process cache is the fastest (no network hop) and needs no extra infrastructure, but each server has its own copy, so entries differ across servers and invalidation is hard; total memory is multiplied by the number of servers. A distributed cache such as Redis or Memcached is shared, consistent across servers and sized independently, at the cost of a network round trip and a new component to run. Combining both, with a small short-TTL local cache in front of a distributed one, is common for very hot data.

  9. 9

    How are keys distributed across a cache cluster, and what is the problem with modulo hashing?

    hard

    With hash(key) mod N, adding or removing a server changes N, so almost every key maps to a new server and the cache is effectively flushed, causing a surge on the database. Consistent hashing places servers and keys on a ring, and a key belongs to the next server clockwise, so a change moves only about 1/N of the keys. Virtual nodes smooth the load. Redis Cluster instead uses 16384 fixed hash slots assigned to nodes, and moves slots between nodes.

  10. 10

    How does HTTP caching work: Cache-Control, ETag and conditional requests?

    medium

    The server sends Cache-Control: max-age=3600 to let browsers and CDNs reuse a response for an hour without asking. After that the client can revalidate: it sends If-None-Match with the stored ETag, and if the resource is unchanged the server replies 304 Not Modified with no body. Cache-Control: no-store forbids storing, no-cache means store but always revalidate, and private restricts caching to the user's browser. Fingerprinted file names (app.3f9a.js) allow very long max-age because a change produces a new URL.

  11. 11

    What should you cache, and what should you not?

    medium

    Cache data that is read much more often than it changes, expensive to compute, and tolerant of slight staleness: product pages, profile data, rendered fragments, results of aggregations. Avoid caching data that must always be exact (account balances, inventory at checkout), highly personal or sensitive data in shared caches without isolation, and data with very low reuse, where the cache would fill with entries never read again.

  12. 12

    How do you design cache keys?

    medium

    Keys must be unique per distinct result and predictable: include the entity type, id and any parameter that changes the result, such as user:42:profile:v3 or search:{query-hash}:page:2:lang:en. Add a version segment so a schema change can invalidate everything by bumping it. Keep keys short, avoid user-supplied raw strings (hash them) and be careful that personalised responses are not stored under a shared key.

  13. 13

    Redis or Memcached?

    medium

    Memcached is a simple, multithreaded in-memory cache of strings with LRU eviction and no persistence. Redis adds data structures (hashes, sorted sets, streams), persistence, replication, Lua scripting and clustering, so it covers use cases beyond caching, such as rate limiting and leaderboards. For a plain, large, multi-core string cache either works; most new systems pick Redis for flexibility.

  14. 14

    Design a cache for a product catalogue with 10 million items and a read-heavy workload.

    hard

    Use cache-aside with Redis. Key by product id, store a compact serialised document, set a TTL with jitter, for example 10 to 15 minutes. On a product update, write to the database and delete the key; publish the change so other layers (CDN, local caches) can invalidate. Size the memory for the hot subset (maybe the top 10 to 20 percent of items) and let LRU or LFU evict the rest. Protect against stampedes with single-flight on misses and against penetration with a Bloom filter or negative caching for unknown ids. Track hit ratio, evictions and p99 latency, and have a plan for the cache being unavailable: read from the database with rate limiting, never crash.

Frequently asked questions

What is a good cache hit ratio?

It depends on the workload, but read-heavy systems often aim for above 90 percent. Measure the gain in latency and database load, not the ratio alone.

Is caching the same as a CDN?

A CDN is a geographically distributed cache for content, mainly static files. Application caches such as Redis cache data and computed results.

What is the hardest problem in caching?

Invalidation: knowing when a cached copy is stale, and doing it without races.

Does caching help writes?

Write-back caches can absorb write bursts, but caches mostly speed up reads. Write-heavy workloads usually need partitioning or queues instead.

Related guides

More in CS Fundamentals Interview Questions

See all in this section