Quick revision
The short answer
First we find which repeated queries the query cache had been absorbing, using performance_schema.events_statements_summary_by_digest. Then we cache their results in the application with explicit keys and TTLs, Caffeine for hot per-instance data and Redis for data shared across instances, invalidating through domain events where freshness matters and accepting TTL staleness where it does not. Heavy reports move to a replica or to precomputed summary tables, and we measure database CPU, cache hit ratio and freshness, preventing stampedes with request coalescing or refresh-ahead.
What to remember
In this answer, we replaced the removed query cache with a deliberate design. We found the hot repeated query shapes with events_statements_summary_by_digest, cached report results in the application with explicit keys and TTLs in Caffeine and Redis, and invalidated through after-commit domain events where freshness matters while accepting TTL staleness elsewhere. Heavy reports moved to a replica and to precomputed summary tables. We measured database CPU, hit ratio and freshness, and prevented stampedes with request coalescing and refresh-ahead.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
First we find which repeated queries the query cache had been absorbing, using performance_schema.events_statements_summary_by_digest. Then we cache their results in the application with explicit keys and TTLs, Caffeine for hot per-instance data and Redis for data shared across instances, invalidating through domain events where freshness matters and accepting TTL staleness where it does not. Heavy reports move to a replica or to precomputed summary tables, and we measure database CPU, cache hit ratio and freshness, preventing stampedes with request coalescing or refresh-ahead.
Why this question is asked
A reporting service whose database CPU triples right after an upgrade is a classic consequence of MySQL 8.0 removing the query cache: identical report queries that used to be answered from the cache now run every time. The quick fix of downgrading is not available, since 5.7 is end of life, and turning on a cache without a design just moves the problem. Interviewers ask this as a design question to see whether we can replace an implicit, database-level cache with an explicit one that is measured and correct. In this answer, we will find the hot queries, design the application cache, decide on invalidation, move the heaviest reports, and measure and protect the result.
1Finding the Queries the Cache Was Absorbing
MySQL's performance_schema groups statements by digest, a normalised form of the query with literal values replaced by placeholders, and keeps totals per digest. Sorting by total time shows which query shapes cost the most CPU now that nothing caches them.
In this example, one dashboard query called 412,000 times a day at 124 ms each accounts for most of the load. The calls column also hints at how repetitive the traffic is: many users open the same dashboard for the same region and period, which is exactly what the query cache served for free before. The sys schema's statement_analysis view shows the same data in a friendlier form.
2Designing the Application Cache
We cache at the level of the report's meaning, not the SQL text. The key names the report and its parameters, for example sales:EMEA:2026-10, and the value is the computed result the API returns. Each cache has an explicit time to live chosen with the report's owners: data for closed past months can be cached for hours, today's figures perhaps for one minute.
Where the cache lives depends on how it is shared. Caffeine, an in-process cache inside each service instance, is the fastest option and perfect for small hot data such as targets per quarter. Redis, a shared cache outside the instances, avoids each of, say, eight instances computing the same expensive report separately, and survives restarts. A common design uses both: Caffeine in front of Redis in front of MySQL.
10 messages in order. Play them, or step through them one at a time.
Later requests from any instance stop at Caffeine or Redis.
3Invalidation: Events Where Freshness Matters, TTL Where It Does Not
Each cached report needs a freshness rule agreed with its users. For reports where a delay of minutes is fine, the TTL is the whole rule: simple and robust. For data that must reflect a change quickly, such as targets that a manager edits and expects to see immediately, the service that changes the data publishes a domain event after commit, and the report service evicts the affected keys. In Spring, @TransactionalEventListener(phase = AFTER_COMMIT) runs the eviction only after the change is committed, so the cache is never refilled with data from before the change; across services, the same event goes through Kafka or another broker. Notice that the eviction is precise: a change to EMEA targets evicts only EMEA entries.
4Moving the Heaviest Reports Off the Primary
Caching reduces repeated work, but the first computation of a heavy report still costs the same. For the heaviest reports we do two more things. Report queries go to a read replica, so the primary's CPU stays for transactions; reports usually tolerate the replica's second or so of lag. And reports that aggregate millions of rows, such as monthly sales by region, read from summary tables instead: a job maintains sales_daily_summary incrementally every few minutes or after each day closes, and the report reads a few hundred rows instead of scanning millions. That turns a 124 ms query into a 2 ms one before any cache is involved.
5Measuring and Preventing Stampedes
We measure three things before and after each step: database CPU and queries per second per digest, the cache hit ratio per cache (Caffeine and Redis both expose statistics, and Spring Boot publishes cache metrics through Micrometer), and freshness, the age of the data users see compared with the agreed limit.
One danger remains: a stampede. When a popular entry expires, hundreds of concurrent requests miss at once and all run the same 124 ms query. Within one instance, @Cacheable(sync = true) or Caffeine's loading cache makes concurrent callers for the same key wait for a single load, which is request coalescing. Across instances, a short Redis lock per key or a single scheduled refresher prevents duplicate work. Refresh-ahead avoids the expiry moment altogether: Caffeine's refreshAfterWrite reloads an entry in the background while still serving the old value, so popular reports never expire in front of users.
6Follow-up questions
Why not put ProxySQL's query cache in front and be done?
It can be a quick bridge for a few query patterns while the real design is built, but it only invalidates by TTL and caches by SQL shape, so it cannot evict precisely when data changes or share computed objects. We would use it temporarily, if at all.
How do we pick TTLs?
From the users' freshness needs, not from performance. We ask how old a figure may be before it misleads someone, set the TTL below that, and then check the hit ratio. If the hit ratio is poor, we look at precomputation or refresh-ahead rather than lengthening the TTL beyond what users accept.
What if cached reports are personalised per user?
Then caching whole responses works poorly, because keys multiply. We cache the shared building blocks, such as region totals, and assemble the personal view per request, or we precompute per-user summaries if they are expensive and stable.
7Conclusion
In this answer, we replaced the removed query cache with a deliberate design. We found the hot repeated query shapes with events_statements_summary_by_digest, cached report results in the application with explicit keys and TTLs in Caffeine and Redis, and invalidated through after-commit domain events where freshness matters while accepting TTL staleness elsewhere. Heavy reports moved to a replica and to precomputed summary tables. We measured database CPU, hit ratio and freshness, and prevented stampedes with request coalescing and refresh-ahead.
Open with
The tripled CPU is the query cache's work coming back: MySQL 8.0 removed it, so the identical report queries that used to be served from it now run every time.
Then, if they want more
I would start by measuring, using performance_schema events_statements_summary_by_digest ordered by total time, which shows the query shapes, how often they run and how long they take; typically a few dashboard queries dominate.
Then I would cache in the application, keyed by the report's meaning, like report name, region and month, not by SQL text, each with an explicit TTL agreed with the users. Caffeine in each instance for small hot data, Redis when instances should share results, often both layered.
For freshness, where a few minutes of staleness is fine the TTL is enough; where it is not, the writing side publishes a domain event after commit and the report service evicts exactly the affected keys.
For the heaviest reports, I would route them to a read replica and build summary tables maintained incrementally, so the query reads hundreds of rows instead of millions. I would track database CPU, hit ratio and data age before and after each step.
And I would prevent stampedes when hot entries expire: sync true on Cacheable or a Caffeine loading cache to coalesce requests per key, a lock across instances if needed, and refreshAfterWrite so popular reports refresh in the background instead of expiring in front of users.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- MySQL 8.0
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- MySQL reference manual
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 "After an upgrade from MySQL 5.7 to 8.0, a reporting service that relied on the query...": https://scrivio-interview-library.netlify.app/databases/mysql/b2076baad4/