I was woken by a phone call a little after one in the morning. The on-call group chat was already filling up with main site API timeout messages. My first instinct wasn’t to look at the logs—it was to confirm one thing: could we roll back at this hour?
Looking back afterward, that instinctive order saved us some time, but only that. The real problem had started at ten that morning; nobody had connected it to the main site alerts at the time.
A feature where “business clicks it themselves”
The culprit was backend report export. That report aggregates across several large business tables. It used to be run manually once a week by a DBA, exported to Excel, and sent out. Business had asked several times to click it themselves.
The plan was simple: click, create a task and persist it, a background worker runs the query, generates a file and drops it into object storage, the frontend polls progress, and when it’s done, gives a download link. We load-tested it before launch: a single query took a dozen-plus seconds, which we judged acceptable.
That “dozen-plus seconds” number was misleading. It made people think the bottleneck was “how slow one query is,” so that’s what the load test tested. What actually determines whether the system collapses is “how many queries are running at the same time,” and nobody had calculated that number before launch.
Now when I see this kind of “open a backend capability so business can click it themselves” requirement, I ask two numbers first: how many people will click during peak hours, and how many queries the backend actually runs per click. Business can estimate the first. The second often goes uncalculated, because from the code’s perspective it’s just one request.
Queue-based things have another characteristic: capacity looks infinite, persisting to the database counts as success, and all the pressure lands on the worker side—while the worker’s concurrency, connection pool limits, and other such numbers almost never appear in requirement reviews.
The failure happened on the second morning after launch. During the period of concentrated business use, database CPU climbed from its usual low teens all the way up, connections were exhausted, and main site APIs started timing out. The first alerts we received came from the main site, not the export feature itself—which misdirected our attention for a while.
It took three rounds to stop the bleeding completely:
- Disable the export entry point. Didn’t work—tasks already clicked were still in the queue, and the worker kept running.
- Stop the worker’s consumption. Queued tasks got stuck in an intermediate state, and frontend polling was still hitting the API.
- Clear the queue, patch up the intermediate-state data, then ramp back up.
Those three steps look short written out, but they took a while at the time, because at every step someone asked “will this lose data?”
The thing that shouldn’t have been in the cache key
The root cause was in the cache key.
The original design: the same report with the same filter conditions is generated only once per day; everything after that hits the cache. The key was roughly composed of user id, report id, a hash of the filter conditions, and the date. Reading the code, I even thought the design was rather thoughtful.
1 | # 出事时的写法,大致长这样 |
The problem was that params contained a “cutoff time,” and the frontend defaulted it to the current time. That value changed on every request, so the hash naturally differed every time, and the cache hit rate was zero.
Worse, we had layered a second piece of logic on top: if the cached file’s generation time was more than N minutes old, regenerate it. N was set very small, the intent being to avoid serving stale files.
Put the two together: a user clicks generate once, producing a new task; the frontend polls progress every few seconds, and the polling endpoint carries the same “current time” parameter. From the server’s perspective, every poll is a new generation request. One click by one person could enqueue dozens of tasks, each running a dozen-plus-second aggregation query.
There’s a lesson here more worth recording than the cache key: the polling endpoint is essentially a read operation, but it was wired into the “generate” write path. Same parameters, code reuse—it looked convenient, but it translated “how often the frontend asks” —a purely client-side variable—directly into “how many heavy queries the backend runs.” The polling interval was set casually by the frontend; the database cost was paid by the server.
Why didn’t the load test catch it? The load test script used fixed parameters, the same key, so the cache hit every time. We tested “how fast the query is,” not “what happens on a miss.” This is one of the most common gaps in load testing: covering only the path with cache, warm-up, and index hits, while what crashes first in production is usually the path without cache.
What we do now is normalize parameters at the entry point, collapsing naturally varying quantities like “current time”:
1 | # 现在:入口先归一化,把“今天之内”折叠成同一个值 |
Along the way we changed polling to use backoff, made task ids idempotent, and made repeated requests only return status without triggering new tasks.
Normalization has a cost, and it should be stated clearly. Collapsing “within today” into a single value means that if a user changes filter conditions later that day, or the underlying data updates that day, they may still get the file generated that morning. We accept that cost because the report’s business semantics are inherently “viewed by day.” For a dashboard requiring minute-level freshness, this approach doesn’t hold—you’d need a different approach: have the frontend request a new task only when filter conditions actually change, rather than having the backend guess which parameters can be collapsed.
The criterion comes down to a simple question: would the user believe this parameter changes the result? In the user’s mind, “cutoff time” means “which day’s data I’m looking at,” not “the millisecond I pressed the button.” It varies naturally but carries no information.
Averages diluted the signal
During the incident, the average response time on the main site monitoring dashboard rose only slightly. The on-call person stared at that graph for a few minutes and thought, “not bad.”
Because only export-type requests were slow, and they were a small share, flattened out by the mass of normal requests. What reacted first was p99 and the database connection pool wait count—and neither was on the dashboard I had open by default at the time.
The problem with averages during an incident isn’t that they’re “inaccurate”—it’s that “they don’t answer the question you’re asking.” The question you’re asking is “how many people are being seriously affected”; the average answers “overall, on average, it’s fine.” Features with a low share and a high per-request cost happen to be exactly the category averages are least sensitive to. This also explains a common phenomenon: the alert didn’t fire not because nobody was monitoring, but because the monitored metrics inherently can’t see this kind of failure.
The changes were concrete:
- Alert thresholds no longer watch only averages; all core APIs get combined conditions on p99 and error rate.
- Give backend heavy work its own connection pool, with at least a concurrency cap, so it doesn’t compete with the main site for resources.
- Rate-limit polling-type endpoints by source; one user can have only one in-progress task at a time.
None of these are clever, but each corresponds to a specific action that night. I’d rather write postmortem conclusions in this form: only what can land on a specific switch, a specific dashboard, or a specific threshold counts as fixed; anything written as “strengthen monitoring” will definitely be back to the same state in two months.
A new column on the change request form
During the postmortem, someone suggested “strengthen code review.” I don’t quite agree. We had all looked at that code together, and nobody raised a question, because it looked too normal—a cache key containing business parameters, perfectly natural.
What was really missing was something else: at launch, nobody could clearly say how to stop this feature within minutes if it saturated the database. Two of the three bleeding-control rounds dealt with “requests already sent”—which itself shows we had no place to immediately shut things off.
So afterward we added a column to the change request form called “bleeding-control measures,” requiring specifics down to where: roll back the code, flip which switch, stop which consumer group, clear which table. If you can’t write it, you’re not ready to launch. Filling in this column is annoying, but it forces people to think through the failure path while writing the code.
This column has a side effect I didn’t anticipate at the time: it changes the design in turn. Being unable to write down a stopgap often isn’t because we haven’t figured out how to stop it, but because the feature has no kill switch, no idempotency, and no separation of heavy work from the main site. Filling in that column makes people a bit more honest.
Now, before every launch, I ask one question: if this thing saturates the database after going live, can we stop it within minutes? Most of the time, no one has thought about that question in advance.
Comments