Script Valley
System Design: APIs, Caching & Scalability
Caching Fundamentals/Assessment

Practice & Assessment

Test your understanding of Caching Fundamentals

Multiple Choice Questions

6
1

What is the difference between Cache-Control: no-cache and no-store?

2

A product page updates every 5 minutes. Which Cache-Control strategy serves the most users from cache while keeping data reasonably fresh?

3

You have a cache key that expires at midnight and 50,000 concurrent requests hit it simultaneously. What problem occurs?

4

Which Redis eviction policy is best for a workload with a small set of extremely popular keys alongside many rarely accessed keys?

5

In cache-aside pattern, a record is updated in the database but the cache key is NOT deleted. What is the result?

6

What is a hot key in Redis and why is it a problem?

Coding Challenges

1
1

Implement cache-aside with stampede protection

Build a function getProduct(id) that implements cache-aside using Redis. On cache miss, fetch from a mock database (async function with 100ms simulated delay). Implement stampede protection using a mutex: if a fetch is already in progress for a key, subsequent callers wait for the in-flight result rather than querying the database independently. Input: product ID (integer). Output: product object. Constraint: under concurrent load of 10 simultaneous calls for the same uncached ID, the mock database function must be called exactly once. Estimated time: 20-25 minutes.

Medium

Mini Project

1

API Response Cache Layer

Wrap an Express API with a Redis caching middleware layer. The middleware must: check Redis before every GET request using a cache key derived from the full request path and query string; return cached responses with X-Cache: HIT header; on miss call next() and cache the response body with a 60-second TTL setting X-Cache: MISS; use Cache-Control: public, max-age=60, stale-while-revalidate=300 on all cached responses; expose DELETE /cache endpoint protected by API key that flushes a specific key or all keys matching a prefix pattern. Set maxmemory-policy allkeys-lru in Redis config. Verify via test that the database mock is called only once for repeated identical requests within the TTL window.

Medium