Scrivio Interview library
ConceptFoundations6 min read · updated 13 November 2026

A profile service receives 20,000 reads per second. Redis answers in about 1 ms, the PostgreSQL database in about 15 ms, and the database is comfortable up to about 1,500 queries per second.

How does the cache hit ratio determine database load and average latency here, and what hit ratio does the service need to stay safe?

Try it yourself

Cache-aside: one read, three cache states

See what a cache hit saves, what a miss costs, and why a write deletes the cached entry instead of updating it.

Read this example as text

When the entry is in the cache (a hit)

  1. A request arrives. A caller asks the service for product 42. The cache already holds it.
  2. Ask the cache first. The service always asks the cache before the database. It sends GET product:42.
  3. Hit: the value comes back. The entry is there and still fresh, so the cache answers at once. The database is never touched.

1 cache call, 0 database reads.

When it was never cached (a miss)

  1. A request arrives. A caller asks the service for product 42. Nobody has asked for it before, so the cache holds nothing.
  2. Ask the cache first. The service always asks the cache before the database. It sends GET product:42.
  3. Miss: nothing comes back. The key was never stored. The cache answers nil, and the service must find the value itself.
  4. Read the database. The service runs SELECT ... FROM products WHERE id = 42. This is the slow, authoritative read the cache exists to avoid.
  5. The row comes back. The database returns the row. The service now has the value, but the cache is still empty.
  6. Store it for next time. Before answering, the service writes the value into the cache with a time to live: SET product:42 ... EX 300. The next read inside 300 seconds will be a hit.
  7. Answer the caller. The caller gets the value. It cost one database read, which a hit would have saved.

2 cache calls, 1 database read.

When its time to live has run out

  1. A request arrives. A caller asks the service for product 42. The cache held it once, but its time to live ran out 12 seconds ago.
  2. Ask the cache first. The service always asks the cache before the database. It sends GET product:42.
  3. Miss: nothing comes back. An expired key is the same as a missing one: Redis no longer returns it, and removes it. The cache answers nil, and the service must find the value itself.
  4. Read the database. The service runs SELECT ... FROM products WHERE id = 42. This is the slow, authoritative read the cache exists to avoid.
  5. The row comes back. The database returns the row. The service now has the value, but the cache is still empty.
  6. Store it for next time. Before answering, the service writes the value into the cache with a time to live: SET product:42 ... EX 300. The next read inside 300 seconds will be a hit.
  7. Answer the caller. The caller gets the value. It cost one database read, which a hit would have saved.

2 cache calls, 1 database read.

When the price changes afterwards

  1. The price changes. Later, someone updates the price to 25. The service writes the database first: UPDATE products SET price = 25 WHERE id = 42.
  2. Delete the cached entry. Then it deletes the key: DEL product:42. It does not write 25 into the cache. Deleting is simpler and safer: the next read misses and loads whatever the database holds then.
  3. The next read will miss. The cache is empty for this key again, so the next request repeats the miss path and stores 25.

3 cache calls, 1 database read, 1 database write.

What this example leaves out
  • One request at a time. Two requests missing at the same moment (a stampede) are not shown.
  • The write deletes the key. Some systems update the cache instead, which this example does not model.
  • The numbers are counts of round trips, not measured times.

Database load is the miss ratio times the request rate: at a 95 percent hit ratio, 20,000 reads per second send 1,000 queries per second to PostgreSQL, and at 90 percent they send 2,000, above its comfortable 1,500. Average latency is about the hit ratio times 1 ms plus the miss ratio times 16 ms, so 1.75 ms at 95 percent and 1.15 ms at 99 percent, while tail latency is set by the misses. The service needs at least 92.5 percent just to sit at the database limit, and a good deal more to survive a cold cache, a peak or a failover, so the hit ratio must be measured per cache with real traffic.

Why this question is asked

Caches are often discussed in terms of speed, but in most systems their real job is protecting the database. A team that says the cache hits 90 percent of the time may think it is doing well, while the database underneath is overloaded. Interviewers ask this question to see whether we can do the simple arithmetic that connects hit ratio, database load and latency, and draw the right conclusions from it. In this answer, we will define the terms, calculate database load and latency, see why a few percent make a large difference, find the minimum safe hit ratio and the headroom we need, and look at what really determines the hit ratio.

1A Quick Recap of the Terms

The hit ratio is the fraction of reads the cache answers by itself. The miss ratio is the rest, one minus the hit ratio. In a cache-aside setup, a miss means the service first asks Redis, finds nothing, then queries PostgreSQL and usually writes the result back to Redis. So a miss costs the cache round trip plus the database query.

In this service, Redis answers in about 1 ms, PostgreSQL in about 15 ms, and the database is comfortable up to about 1,500 queries per second. The service receives 20,000 reads per second.

2Calculating Database Load

Every miss becomes one database query, so the database load is simply the request rate times the miss ratio.

Properties
requests = 20_000 # reads per second
db_limit = 1_500 # queries per second the database handles comfortably
for hit in (0.90, 0.925, 0.95, 0.99):
db_qps = requests * (1 - hit)
print(hit, db_qps)
# 0.90 2000.0 -> above the limit: the database is overloaded
# 0.925 1500.0 -> exactly at the limit, no headroom
# 0.95 1000.0 -> comfortable
# 0.99 200.0 -> very comfortable

Notice how sensitive this is. Going from 95 to 99 percent hits looks like a small improvement of four points, but the miss ratio drops from 5 percent to 1 percent, so the database load falls five times, from 1,000 to 200 queries per second. The other direction matters even more: dropping from 95 to 90 percent doubles the database load. This is why hit ratio must be monitored per cache, not assumed.

3Calculating Average and Tail Latency

A hit costs 1 ms. A miss costs the 1 ms cache lookup plus 15 ms in the database, 16 ms in total. The average latency is the weighted mix of the two.

Text
def avg_latency(hit, cache_ms=1, db_ms=15):
return hit * cache_ms + (1 - hit) * (cache_ms + db_ms) # = cache_ms + (1 - hit) * db_ms
avg_latency(0.95) # 1.75 ms
avg_latency(0.99) # 1.15 ms
avg_latency(0.90) # 2.50 ms (if the database could even keep up)
Hit ratio, database load and latency at 20,000 reads/s
Hit ratioDB queries/sAverage latencyStatus
90%2,0002.50 msover the 1,500 limit
92.5%1,5002.13 msat the limit, no headroom
95%1,0001.75 mssafe in steady state
99%2001.15 mssafe with headroom

The average looks good in every case, but it hides the tail. At a 95 percent hit ratio, 5 percent of requests take about 16 ms, so the p99 latency is a miss, around 16 ms or more, not 1.75 ms. And when the database approaches its limit, its 15 ms becomes 50 or 100 ms as queries queue, so the tail grows much faster than the average suggests.

4The Minimum Safe Hit Ratio, and Why It Is Not Enough

To keep the database at or below 1,500 queries per second, the miss ratio must be at most 1,500 / 20,000 = 7.5 percent, so the hit ratio must be at least 92.5 percent. That is the break-even point, and running there leaves no margin at all.

The service needs headroom for the moments when the hit ratio collapses. After a deployment that changes the key format or a Redis failover with an empty replica, the cache is cold, and almost every request misses for a while. At a traffic peak of twice the normal rate, even 95 percent hits produce 2,000 queries per second. So a steady-state hit ratio of 98 or 99 percent, plus protections for cold starts, such as warming the cache, coalescing concurrent misses for the same key and limiting database concurrency, is what makes the service safe in practice.

5What Determines the Hit Ratio

The hit ratio is not a setting; it comes from the workload and the cache design. Key popularity matters most: profile reads usually follow a skewed distribution, so a cache holding the most popular 10 percent of profiles may serve 90 percent of reads. Cache size decides how much of that popular set fits. TTLs decide how often entries are thrown away and reloaded. Shared versus local caches matter too: if each of 20 instances has its own in-memory cache, each sees only a twentieth of the traffic per key and misses more than one shared Redis would.

So we measure it with real traffic: hits and misses per cache, exported as metrics, and watched during deployments and peaks. A model like the one above then tells us what any change in the measured ratio means for the database.

6Follow-up questions

How would the numbers change if the cache itself slowed to 5 ms?

The database load would stay the same at the same hit ratio, since it depends only on misses. Average latency would rise to 5 plus the miss ratio times 15: 5.75 ms at 95 percent and 5.15 ms at 99 percent. Two side effects matter more. By Little's law, the number of requests in flight grows with latency, from about 35 to about 115 at 20,000 per second, so thread and connection pools may run out. And if the client's Redis timeout is close to 5 ms, some lookups time out and fall back to the database, which lowers the effective hit ratio and can push the database over its limit.

Why not cache everything forever to get 100 percent?

Data changes, so long TTLs serve stale profiles unless every write invalidates the cache reliably, and memory is finite, so the least used entries are evicted anyway. The goal is a hit ratio high enough for safety with an acceptable staleness, not 100 percent.

Does write traffic affect the hit ratio?

Yes. Every update that deletes or overwrites a cache entry causes the next read of that key to miss. For read-heavy data like profiles the effect is small, but for frequently changing data it can dominate the miss rate.

7Conclusion

In this answer, we calculated that database load equals the request rate times the miss ratio and that average latency is the cache time plus the miss ratio times the database time. At 20,000 reads per second, 95 percent hits give 1,000 queries per second and 1.75 ms, 99 percent give 200 and 1.15 ms, and 90 percent overloads the database. The break-even is 92.5 percent, but cold caches, peaks and failovers demand much more, and since the hit ratio depends on popularity, size, TTLs and how the cache is shared, it must be measured, not assumed.

What to say in the interviewAbout 90 seconds aloud

Open with

The database only sees misses, so its load is the request rate times the miss ratio. At 20,000 reads per second, a 95 percent hit ratio means 1,000 queries per second, which is fine, but 90 percent means 2,000, which is above the 1,500 the database handles.

Then, if they want more

Latency is the cache time plus the miss ratio times the database time: a miss costs 1 millisecond for the cache lookup plus 15 in PostgreSQL, so the average is about 1.75 milliseconds at 95 percent and 1.15 at 99 percent.

The average hides the tail, though: at 95 percent, one request in twenty is a miss, so p99 is about 16 milliseconds, more if the database is busy. What stands out is the sensitivity. Going from 95 to 99 percent cuts database load five times, from 1,000 to 200 queries per second, and dropping to 90 percent doubles it.

The break-even is 92.5 percent, since 7.5 percent of 20,000 is 1,500, but running there leaves no headroom for a cold cache after a deploy, a peak or a failover.

So I would target 98 to 99 percent in steady state, add protections like miss coalescing and database limits, and measure the hit ratio per cache with real traffic, because it depends on key popularity, cache size, TTLs and how many instances share the cache.

About this answer

Last updated
13 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "How does the cache hit ratio determine database load and average latency here, and what...": https://scrivio-interview-library.netlify.app/architecture/caching-strategies/1413b48aa0/