⚡ Redis and Caching — interview questions
12 questions. Filter by what the role asks for; open one for the six layers of the answer. The course is where the answers come from.
12 of 12
01A service increments a page-view counter in Redis by doing `GET counter`, adding one in application code, then `SET counter <newValue>`. Under load from several instances, the final count is consistently lower than the number of requests. What's happening, and what's the one-line fix?Redis fundamentalseasy0-2y02A team adds a Redis cache in front of a product lookup and is surprised the win is only partly about speed — what does a cache actually buy beyond raw latency, and why should every cached entry get a TTL even when the application also deletes the entry on every write?Caching fundamentalseasy0-2y03A service stores one Redis hash per user session, and a new feature proposes merging in a second, related hash of many more fields under the same key rather than keeping two separate hash keys, on the grounds that "one key is simpler." What actually changes about the memory cost as the merged hash grows, and why isn't merging automatically the cheaper choice?Redis internalsmedium3-5y04A Redis cache configured with `maxmemory-policy allkeys-lru` holds a tenth of the working set under realistic skewed traffic. A teammate suggests switching to `allkeys-lfu` to improve the hit ratio. Would that help, and what's actually being tuned when the eviction policy changes?Caching fundamentalsmedium3-5y05A scraper walking through sequential product ids, many of which don't exist, is sending every one of those requests straight through the cache to the database, even though the service already uses cache-aside. Why doesn't cache-aside help here, and what closes the gap?Caching fundamentalsmedium3-5y06A sliding-window rate limiter — allow 10 requests per rolling second per user — is implemented by calling `ZREMRANGEBYSCORE` to trim old entries, then `ZCARD` to count what's left, then conditionally `ZADD`, as three separate Redis commands from application code. Under concurrent requests from the same user, more than 10 get allowed in some windows. What's wrong, and how does wrapping it in a Lua script fix it?Redis fundamentalsmedium3-5y07A work queue built on `LPUSH` + `BRPOP` loses in-flight items whenever a worker crashes mid-job, because a popped list item is already gone from Redis the moment it's popped. A Redis stream with a consumer group is proposed instead. What does the pending entries list actually guarantee, and what obligation does it put on the workers?Redis fundamentalsmedium3-5y08A home page built from a 300 ms query is cached for a minute. When its entry expires, 200 concurrent requests all miss the cache at once and all run the expensive query. Walk through why a per-instance single-flight lock only partly fixes this across four application instances, and why a Redis-based rebuild lock introduced a second bug on top of the first fix.Consistency and reliabilityhard5-8y09Three service instances coordinate through a Redis lock (`SET lock:invoice-1041 <owner> NX PX 1000`) before sending an invoice, so only one should send it. Worker A acquires the lock, then a GC pause holds it inside its critical section for 1.5 seconds — longer than the lock's 1-second expiry. Walk through what actually happens, and what a plain `DEL` on release makes worse.Distributed systemshard5-8y10Under write latency pressure, a team proposes switching a Redis-backed cache from write-through to write-behind: acknowledge the write immediately, buffer it, and flush to the database on a schedule. What exactly is being traded away, and is it a bug to fix or a tradeoff to make deliberately?Caching fundamentalshard5-8y11A team is deciding whether to use Redis's Redlock algorithm (acquiring a lock across a majority of independent Redis nodes) for a correctness-critical operation — one where two holders acting at once would corrupt data. What does Redlock's quorum actually buy over a single Redis instance, and where does Martin Kleppmann's critique of it actually land?Distributed systemssenior8+y12A cache entry costing 300 ms to rebuild is protected from a stampede by probabilistic early expiration (XFetch) instead of a Redis rebuild lock. Walk through the actual formula being evaluated on every read, and explain precisely why a read close to the entry's expiry is far more likely to trigger an early rebuild than one early in the entry's life — without the formula needing to know explicitly how much time is left.Consistency and reliabilitysenior8+y