Serving repeat reads without querying the database
Which work can avoid repetition
Imagine a product page. Thousands read the same description, which rarely changes. Fetching it from the database every time makes the database repeat the same work.
A cache keeps a ready copy for quick return on the next read. Here Redis is the cache and PostgreSQL the main database. Caching helps requests whose suitable copy is already available.
Hits, misses and TTL
The application checks cache first. A found value is a cache hit: the database does not participate. A missing value is a cache miss: data must come from the database. A normal app can cache that response for future reads; this lab considers an already warm flow.
TTL is the cache entry lifetime. A TTL of 30 seconds means the entry cannot be used after that period without refreshing. Short TTL causes more misses; long TTL keeps a copy usable longer, but in real applications it may lag behind the database. This task does not evaluate freshness.
How caching offloads the database
With 1,000 reads/s and 700 served by cache, the remaining 300 go to the database. If cache serves only 400, the database receives 600. What matters is how many requests Redis actually answers, not merely adding it.
A cache has limits too. Here its capacity is measured in lookups per second, lookup/s, not memory. Requests unable to check the cache in time go directly to the database. Its load combines misses and this bypass traffic.
Which metrics to watch
In the analysis, compare hits, misses, bypass and database requests. Hit rate is the hit share among requests that checked the cache. With bypass, a high hit rate does not mean cache serves the same share of all incoming traffic.
Latency is time to response. p95 is the boundary containing 95% of measured latencies. Here it is an educational estimate for served requests only, not a measurement of real Redis or PostgreSQL.
Change one parameter at a time: repeated-read share, then TTL, then Redis throughput. Repeats are spread over a 60-second window; this assumption defines the TTL-to-hit relationship. It is not a universal Redis formula. If every value is requested for the first time, cache alone cannot remove database load.
Task
The API receives 1,000 reads per second, while PostgreSQL can handle only 300. Add a cache to offload the database and reduce latency without unnecessary costs.
This is a warmed-up, read-only flow. The API does not limit load. Repeat-read ages are uniform within a 60s window. Hit share among lookups = potential repeat reads × min(TTL / 60, 1). This is a teaching workload assumption, not a Redis formula. Cache capacity is in lookups/s, not megabytes of memory. Bypassing on overload is the chosen application policy. The model omits cold starts, eviction, stampedes, writes, invalidation and fill costs. A long TTL can yield stale data; freshness is not checked in this task. TTL here means value retention time, not request duration.
- See how many reads reach the database without a cache.
- Add Redis from the panel and explore the effects of repeated reads, TTL and lookup limits.
- Reach acceptable database load, latency and cost at the given traffic.