Caching Mid
When is caching actually a bad idea? Can you name scenarios where adding a cache would decrease performance or reliability?
Select the correct answer
When the dataset is large but reads vastly outnumber writes across all keys
When the backing store is slow and cannot keep up with incoming read traffic
When many clients repeatedly request the same small set of hot keys
When data changes constantly so cached entries are stale before they are reused
How does the principle of temporal and spatial locality justify the use of a cache in the first place?
Select the correct answer
Network bandwidth grows over time, so cached copies become cheaper to serve later
Data access is uniformly random, so caching any subset reduces total latency equally
Recently used data and nearby data are likely reused soon, so caching them pays off
Writes always follow reads, so caching write buffers improves overall throughput greatly
Why might you choose NOT to cache a piece of data even if it is frequently accessed?
Select the correct answer
It requires strong consistency or freshness that a cache cannot reliably guarantee
It is too small to provide any meaningful memory savings when stored in the cache
The eviction policy would prioritize it and push out other more valuable entries
Frequently accessed data is already fast to fetch, so caching offers no real benefit
Under what circumstances might adding a cache actually increase the overall latency of a request?
Select the correct answer
When hit rates are low, each miss adds a lookup before fetching from the source
When hit rates are high, the cache must constantly evict entries to free memory space
When reads dominate writes, the cache invalidation logic blocks incoming requests
When the cache is colocated with the application server reducing network round trips
What makes a good cache key? What pitfalls should you avoid when designing cache keys?
Select the correct answer
It relies on random identifiers so that different requests never collide in store.
It stays as short as possible, omitting parameters to save memory on the server.
It deterministically encodes all inputs that affect the result and stays unique.
It uses the current timestamp so that every incoming request gets a fresh value.
What characteristics make a piece of data a good candidate for caching versus a poor one?
Select the correct answer
Data that is unique per request and rarely accessed more than a single time overall.
Data written far more often than it is read and tolerant of being slightly stale now.
Data read frequently, changing rarely, and expensive to compute or fetch each time.
Data that must stay perfectly consistent and changes on essentially every request made.
What is the fundamental difference between the Cache-Aside (Lazy Loading) pattern and the Read-Through pattern, and in which layer does the loading logic reside for each?
Select the correct answer
Both load through the application, but cache-aside writes sync and read-through async.
In cache-aside the cache loads on a miss; in read-through the application loads it.
In cache-aside the application loads on a miss; in read-through the cache loads it.
Both load through the cache layer, differing only in their eviction and expiry rules.
Explain the trade-off between Write-Through and Write-Back (Write-Behind) caching. Which one would you choose for a system where data loss is unacceptable, and why?
Select the correct answer
Write-Back persists to the database synchronously; choose it since loss is unacceptable here.
Write-Through defers database writes for throughput; choose it since loss is unacceptable.
Write-Back writes through immediately on each update; choose it since loss is unacceptable.
Write-Through persists to the database synchronously; choose it since loss is unacceptable.
When would you use a Write-Around policy instead of Write-Through, and how does it impact the cache miss rate for subsequent reads?
Select the correct answer
Use it for read-heavy hot data; recently written items are then served from cache faster.
Use it for data needing strong consistency; it lowers the miss rate on every later read.
Use it when writes are rare but reads frequent; it eliminates misses right after a write.
Use it for write-heavy rarely-read data; recently written items cause more read misses.
Compare Write-Through, Write-Back (Write-Behind), and Write-Around. What are the data loss risks associated with Write-Back?
Select the correct answer
Write-Back duplicates every write, risking loss if both copies are evicted at one moment.
Write-Back acknowledges from the cache before persisting, risking loss if the cache fails.
Write-Back skips the cache entirely, risking loss if the network fails before each write.
Write-Back persists synchronously first, risking loss if the database fails before a flush.
Compare Least Recently Used (LRU) and Least Frequently Used (LFU). In what specific traffic pattern would LFU outperform LRU?
Select the correct answer
When access is uniformly random with no items favored over any of the others.
When a stable hot set is repeatedly accessed amid occasional scanning traffic.
When the working set rotates completely on every short interval time window.
When recent items are far more likely to be requested again very soon after.
How would you define and measure a 'Cache Hit Ratio'? What does a high hit ratio with high latency suggest about your cache design?
Select the correct answer
Hits divided by total lookups; high hit ratio with high latency means the cache layer itself is slow.
Hits divided by total writes; high hit ratio with high latency means the backing database is overloaded.
Reads divided by total memory; high hit ratio with high latency means the cached objects are too large.
Misses divided by total lookups; high hit ratio with high latency means the cache is badly undersized.
Explain the trade-off between memory usage and hit ratio when sizing a cache.
Select the correct answer
Hit ratio is fixed by the workload, so adding memory only wastes resources without any real benefit.
Less memory raises the hit ratio because a smaller cache keeps only the hottest items always resident.
More memory raises the hit ratio but with diminishing returns, so added cost eventually outweighs gains.
More memory always lowers the hit ratio because a larger cache scans more entries on every lookup.
If your cache is primarily storing user session data that expires after 30 minutes of inactivity, which eviction policy would you configure and why?
Select the correct answer
An expire-after-write policy that removes each session 30 minutes after the moment it was first created.
A least-recently-used policy that evicts the oldest entries only once the cache reaches its capacity.
An expire-after-access (idle TTL) policy that resets the timer on each access and evicts after inactivity.
A first-in-first-out policy that evicts the earliest written sessions regardless of any recent activity.
When would Most Recently Used (MRU) eviction outperform LRU?
Select the correct answer
During bursty write workloads where the newest entries are almost always the next ones to be read back.
During random-access workloads where every item has an equal and independent probability of being reused.
During repeated sequential scans of a dataset larger than the cache, where old items are reused soonest.
During workloads with a small hot set that is accessed far more frequently than everything else stored.
Explain the Cache-Control: no-cache vs. no-store directives. Which one allows the browser to keep a copy?
Select the correct answer
no-cache forbids storage, while no-store stores a copy reused without revalidation.
Both directives forbid storage, so neither one ever allows the browser to keep a local copy.
no-store lets the browser store a copy but it must revalidate with the server before reuse.
no-cache lets the browser store a copy but it must revalidate with the server before reuse.
What is the purpose of the Vary HTTP header in caching, and how does it prevent a mobile user from receiving a cached desktop version of a page?
Select the correct answer
It tells caches to key entries on headers like User-Agent, storing separate mobile and desktop copies.
It tells the origin server to compress responses differently for mobile clients to reduce bytes sent.
It tells caches how long entries may live before they must be revalidated against the origin server.
It tells browsers to vary request order so the mobile and desktop versions load in different priorities.
How do ETags work to facilitate conditional GET requests, and how does this save bandwidth?
Select the correct answer
The server compares timestamps in Last-Modified and resends the full body only when the bytes differ.
The client sends the stored ETag in If-Modified-Since; an unchanged resource returns 200 with no body.
The client sends a HEAD request first and downloads the body only if the content length has changed.
The client sends the stored ETag in If-None-Match; an unchanged resource returns 304 with no body.
Explain the public vs. private directives in Cache-Control. Why is this distinction critical for security when caching authenticated responses?
Select the correct answer
private disables all caching entirely while public forces revalidation on each single request
public encrypts responses for shared caches while private compresses them for browsers only
private restricts storage to the user's browser; public permits shared proxy caches to store it
public restricts storage to the user's browser; private permits shared proxy caches to store it
What is the difference between the ETag and Last-Modified headers?
Select the correct answer
ETag is an opaque identifier of content; Last-Modified is a timestamp of the last change
ETag is sent only by clients in requests; Last-Modified is sent only in server responses
ETag stores the resource's size in bytes; Last-Modified stores the MIME type of content
Last-Modified is an opaque identifier of content; ETag is a timestamp of the last change
Explain the difference between freshness (Cache-Control/Expires) and validation (ETag/Last-Modified) in HTTP caching. When does a browser send a Conditional GET?
Select the correct answer
Freshness applies only to images; a Conditional GET is sent only for HTML documents and scripts
Freshness forces revalidation on each request; a Conditional GET is sent for every cached resource
Validation avoids server contact while fresh; a Conditional GET is sent before the response is stale
Freshness avoids server contact while fresh; a Conditional GET is sent once the response is stale
Explain the Stale-While-Revalidate pattern. How does it improve user-perceived latency while still ensuring data eventually becomes fresh?
Select the correct answer
Block the request until revalidation completes, then serve only the freshly fetched content
Serve stale content instantly while revalidating in the background for the next request
Serve stale content instantly and skip revalidation until the cache is manually purged later
Reject stale content instantly and return an error while revalidation occurs in background
What is the purpose of cache warming or pre-warming, and when is it necessary to do this before a deployment or high-traffic event?
Select the correct answer
Increase the cache's memory limit before traffic to prevent evictions during peak demand
Pre-expire cache entries before traffic so the cache revalidates everything during the event
Flush the cache before traffic so stale entries don't serve outdated data during the event
Pre-populate the cache before traffic so a cold cache won't cause a flood of misses
What is the difference between evicting data based on a Time-To-Live (TTL) versus evicting based on memory pressure (Max-Memory), and can a key be evicted before its TTL expires?
Select the correct answer
TTL evicts on time expiry; memory pressure only rejects new writes, never removing keys
TTL evicts by policy on memory limits; memory pressure evicts on time, never before TTL
TTL evicts on time expiry; memory pressure evicts by policy, so keys can go before TTL
Both evict only on time expiry, so a key can never be removed before its TTL elapses
How do you determine the optimal TTL (Time-to-Live) for a specific piece of data?
Select the correct answer
Use a fixed global value of five minutes for every key regardless of its access
Always match it to the database's transaction log retention window for consistency
Set it to the longest possible value so the cache hit rate is always maximized
Balance how often the data changes against tolerance for staleness and backend load
What is the difference between active (eager) expiration and lazy (passive) expiration of TTL'd entries, and what are the trade-offs?
Select the correct answer
Active removes keys only on access; lazy scans the keyspace continuously, trading memory for CPU
Active proactively removes expired keys; lazy removes them only on access, trading CPU for memory
Active never frees memory until eviction; lazy frees it immediately on every single write operation
Active applies to writes only; lazy applies to reads only, with essentially no memory trade-offs
What is the difference between explicit (event-based) invalidation and time-based (TTL) expiration, and when would you combine them?
Select the correct answer
Explicit invalidation guarantees no stale reads ever; TTL guarantees the same with less coding
Explicit invalidation works only in single-node caches; TTL works only in distributed clusters
Explicit removes entries on data-change events for freshness; TTL expires them as a safety net
Explicit invalidation expires entries on a timer; TTL removes them only when source data changes
What is Cache Penetration, how does it differ from a standard cache miss, and why is negative caching or a bloom filter used to solve it?
Select the correct answer
Requests for non-existent keys always miss and hit the database; negative caching or bloom filters block them
Requests overflow the cache memory limit and evict entries; negative caching frees room for them quickly
Requests bypass the cache layer entirely by design; bloom filters reroute them back through the cache again
Requests for expired keys repeatedly recompute the value; bloom filters store the recomputed results instead
Explain the Cache Avalanche scenario. How can jitter (adding randomness to TTLs) prevent a database from crashing during a mass expiration event?
Select the correct answer
The whole cache server goes offline at once; randomizing TTLs makes clients retry their failed lookups at staggered intervals later.
Stale entries are served past expiry forever; randomizing TTLs forces periodic refreshes so the database stays fully consistent always.
Many keys expire simultaneously, flooding the database; randomizing TTLs spreads expirations across time to smooth the load.
A single popular key expires and many requests rebuild it at once; randomizing TTLs delays each request before reading from cache.
Explain the difference between 'Cache Penetration' and 'Cache Avalanche'. How do their mitigations differ?
Select the correct answer
Penetration is requests for nonexistent keys hitting the DB, fixed by negative caching or Bloom filters; avalanche is mass expiry, fixed by TTL jitter.
Penetration is serving stale data fixed by shorter TTLs; avalanche is too many writes fixed by batching updates before they reach the database.
Penetration is one hot key rebuilt by many threads fixed by locking; avalanche is the cache crashing fixed by adding replica failover nodes.
Penetration is mass simultaneous expiry fixed with TTL jitter; avalanche is nonexistent-key requests fixed with negative caching or Bloom filters.
How does 'Negative Caching' help protect your downstream database?
Select the correct answer
It caches error responses briefly and retries the database automatically until a valid value is eventually found and stored.
It blocks all requests for unknown keys at the load balancer so they never reach the cache or the database tier at all.
It stores a marker for missing keys so repeated lookups for them are answered by the cache rather than the database.
It keeps frequently failed queries in a separate queue so the database can process them in batches during off-peak hours.
How does consistent hashing minimize cache misses when adding or removing a node from a distributed cache cluster?
Select the correct answer
Keys are replicated to every node in the ring, so when one node leaves another already holds the data and no misses can occur at all.
Keys are mapped around a hash ring, so only keys belonging to the changed node move, leaving most keys on their existing nodes.
Keys are reassigned evenly across all nodes on each change, which balances load perfectly while invalidating only a tiny part of the cache.
Keys are rehashed with a new modulus on every change, but the ring caches the old results so lookups still resolve to the same node.
What is Consistent Hashing, and why is it preferred over simple 'mod N' hashing for distributing cache keys across a cluster?
Select the correct answer
It sorts keys into ranges per node so adding a node splits one range only, while mod N distributes keys randomly and cannot be rebalanced.
It replicates keys across all nodes so failures cause no remapping at all, while mod N stores each key on one node without redundancy.
It assigns keys by modulus of a fixed prime so changing nodes shifts only a few keys, while mod N must move every key to a new node.
It maps keys and nodes onto a ring so adding or removing a node remaps only a fraction of keys, unlike mod N which remaps nearly all.
Explain the difference between sharding (partitioning) and replication in a cache cluster. Which one helps with read throughput, and which one helps with storage capacity?
Select the correct answer
Sharding copies the full dataset to every node for redundancy, while replication splits keys to improve overall write throughput.
Both sharding and replication mainly improve write latency, but neither one increases the total storage capacity that is available.
Replication spreads data across nodes to add storage capacity, while sharding copies data to extra nodes to raise read throughput.
Sharding spreads data across nodes to add storage capacity, while replication copies data to extra nodes to raise read throughput.
From a high-level architectural perspective, why might you choose Redis over Memcached (or vice-versa) for a caching layer?
Select the correct answer
Memcached offers rich data structures, persistence, and replication, whereas Redis is a simpler single-threaded key-value store.
Redis offers rich data structures, persistence, and replication, whereas Memcached is a simpler multithreaded key-value store.
Both store only strings, but Redis scales reads better while Memcached is the sole option that supports clustering.
Redis is purely volatile with no persistence, whereas Memcached can durably store complex nested data structures on disk.
What is the 'Cache Invalidation' problem, and why is it considered one of the 'two hard things' in computer science?
Select the correct answer
Choosing a hash function that distributes keys evenly is hard, and getting it wrong is what causes most stale-data problems.
Allocating enough memory so the cache never evicts any entry is hard, which is why invalidation rarely happens in practice.
Deciding which serialization format to store cached values in is hard, since the wrong choice silently corrupts old entries.
Knowing exactly when cached data becomes stale and must be refreshed or removed is genuinely hard to get right at scale.
How does using a versioned key (e.g., user:v2:123) help with cache invalidation during a schema change or deployment?
Select the correct answer
Bumping the version prefix compresses old entries in place, so both old and new code keep reading the same migrated values.
Bumping the version prefix forces the cache to synchronously delete every old key, blocking reads until the purge completes.
Bumping the version prefix locks all old keys for writes only, letting old code still read them until their TTL expires.
Bumping the version prefix makes old keys unreachable, so new code reads fresh values without deleting the stale entries.
What is the difference between Strong Consistency and Eventual Consistency in a multi-node cache cluster?
Select the correct answer
Eventual consistency returns the latest write on every node instantly; strong consistency allows brief stale reads that converge.
Strong consistency returns the latest write on every node immediately; eventual consistency allows brief stale reads that converge.
Both guarantee the newest value on every read, but strong consistency achieves it faster by skipping inter-node coordination.
Strong consistency means all nodes hold identical data forever; eventual consistency means nodes never synchronize their values.
Explain the concept of 'Eventual Consistency' in the context of a cache-aside pattern.
Select the correct answer
The cache may briefly serve stale data after a write until invalidation or TTL makes it converge.
Reads block until the cache and database are fully synchronized, guaranteeing identical values always.
The database is updated only after every cached copy has been confirmed consistent across all nodes.
The cache always reflects the database immediately because writes update both atomically in one step.