Quick revision
The short answer
First I would stop the damage: roll back or pin the previous version of the shared library across the affected services, because a rollback is faster and safer than a fix under pressure. The cause is that registering and unregistering a listener per request turns CopyOnWriteArrayList, a read-mostly structure, into a write-heavy one, and each registration copies the whole listener array. Afterwards I would fix the design so listeners are registered once at startup or kept in a structure suited to churn, add an allocation or benchmark test for the hot path to the library's build, and change the release process so canaries catch performance regressions before a fleet-wide rollout.
What to remember
In this answer, we led the response to a fleet-wide CPU increase caused by a shared library that registered and unregistered listeners in a CopyOnWriteArrayList on every request, turning a read-mostly structure into a write-heavy one where each change copies the whole array. We mitigated first by rolling back or pinning the previous version across affected services, confirmed the cause with profiling, fixed the design by registering once or using a churn-friendly concurrent map, added an allocation or benchmark test to the library's build, and changed the release process so performance regressions are caught in canaries.
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 I would stop the damage: roll back or pin the previous version of the shared library across the affected services, because a rollback is faster and safer than a fix under pressure. The cause is that registering and unregistering a listener per request turns CopyOnWriteArrayList, a read-mostly structure, into a write-heavy one, and each registration copies the whole listener array. Afterwards I would fix the design so listeners are registered once at startup or kept in a structure suited to churn, add an allocation or benchmark test for the hot path to the library's build, and change the release process so canaries catch performance regressions before a fleet-wide rollout.
Why this question is asked
Shared libraries are force multipliers in both directions. One good change improves fifty services; one bad change hurts fifty services at the same moment, and nobody owns the whole picture. Interviewers ask a senior engineer to lead this kind of response because it combines technical diagnosis with coordination: stopping the damage across teams, finding the real cause, and making sure the class of problem cannot ship again. In this answer, we will walk through the first hour of mitigation, explain the technical cause with numbers, design the fix, and then improve the library's build and release process so the next regression is caught in a canary.
1A Quick Recap of the Cause
CopyOnWriteArrayList copies its entire backing array on every add or remove and publishes the copy, which is why reads and iteration need no lock. It is ideal for listener lists that are set up once and iterated often. The library change used it differently: on every request it registered a listener, for example to observe the request's lifecycle, and removed it at the end.
Two writes per request means two full array copies per request. If a service handles 2,000 requests per second with around 200 requests in flight, the list holds about 200 listeners, so every request copies arrays of 200 references twice, and every iteration over the list for an event also walks 200 listeners that mostly do not care. That is 4,000 copies per second, megabytes per second of garbage, and all writers serialized behind one lock. Spread across a fleet, it shows up as a broad CPU increase rather than an obvious outage.
2The First Hour: Mitigate, Then Diagnose
When the CPU graphs of many services move together right after a library release, the release is the first suspect, and the cheapest test is to undo it. I would open an incident channel, name one coordinator, and ask each affected team to roll back or pin the previous library version, starting with the services closest to their CPU limits or autoscaling ceilings. If the platform has a central dependency management BOM, a bill of materials that fixes library versions for all services, reverting the version there and redeploying is faster than fifty separate pull requests.
In parallel, one or two engineers confirm the cause on a single service: a JFR or async-profiler capture shows CopyOnWriteArrayList.add and remove near the top of the CPU and allocation profiles, and the diff of the library release shows the per-request registration. We confirm the rollback works by watching CPU return to its previous baseline on the first services that roll back. Only then do we announce the cause to all teams, with the evidence.
- Correlate CPU rise with the library releasemany services, same time
- Roll back or pin the old versionhighest-risk services first
- Profile one serviceCOW add/remove at top of CPU and allocation
- CPU back to baseline after rollback?YesCause confirmed, communicateNoKeep investigating other changes
- Fix the design in the libraryregister once, or a churn-friendly set
- Add hot-path test and canary gateprevent the next regression
6 steps in order. Play them, or step through them one at a time.
3Fixing the Design
The real question is why the library registers a listener per request at all. Usually the listener only needs to see events about its own request. Then the better design is one listener registered at startup that dispatches to per-request state looked up by request id, or simply passing a callback into the request's own context, so no shared list changes per request.
If per-request registration is truly needed, the data structure must suit churn. A ConcurrentHashMap keyed by request id, or ConcurrentHashMap.newKeySet(), gives O(1) add and remove with no copying, and iteration that is weakly consistent, which is fine for event delivery to in-flight requests.
4Afterwards: Tests and Release Process
The library slipped through because nothing in its pipeline measured cost on the hot path. So the follow-up adds a test that does: a JMH benchmark of the request path with a threshold, or a simpler allocation test that runs 10,000 requests and fails if the bytes allocated per request exceed a budget, measured with the JDK's ThreadMXBean allocation counters. Allocation tests are attractive because they are deterministic enough to run in CI, unlike timing tests.
The release process needs the second fix. A shared library should reach the fleet in stages: first a few canary services, which are compared against their own baselines for CPU, allocation rate and latency for a day, then a wider wave, then everyone. Automated dependency update tools should follow those waves rather than opening upgrade pull requests in every repository on release day.
Finally, a blameless incident review records the cause, the timeline and the actions with owners. The point is not who added the registration; it is that the library had no performance gate and the fleet had no staged rollout for shared code.
5Follow-up questions
Why roll back instead of shipping a quick fix forward?
A rollback returns to a version with known behaviour in minutes and needs no new code review under pressure. A forward fix may be correct, but it needs to be written, built, released and adopted by every team, and it can introduce a second problem. We fix forward afterwards, calmly, with tests.
How do you get dozens of teams to act quickly?
One coordinator, one channel, one clear instruction with the exact version to pin, and a tracking list of affected services with owners. Where possible, the platform team makes the change centrally, for example in the shared BOM, so teams only need to redeploy.
Why might a timing benchmark be a poor CI gate?
CI machines are shared and noisy, so timing thresholds either flake or must be so loose that they miss regressions. Allocation per operation and operation counts are far more stable, so they make better automatic gates, with JMH runs on dedicated hardware for deeper checks.
6Conclusion
In this answer, we led the response to a fleet-wide CPU increase caused by a shared library that registered and unregistered listeners in a CopyOnWriteArrayList on every request, turning a read-mostly structure into a write-heavy one where each change copies the whole array. We mitigated first by rolling back or pinning the previous version across affected services, confirmed the cause with profiling, fixed the design by registering once or using a churn-friendly concurrent map, added an allocation or benchmark test to the library's build, and changed the release process so performance regressions are caught in canaries.
Open with
First, mitigation. If CPU rose across many services right after a shared library release, I'd roll back or pin the previous version immediately, ideally centrally through our dependency BOM so teams only redeploy, starting with services closest to their limits.
Then, if they want more
I'd run it as an incident with one coordinator and a tracking list of services. In parallel, someone profiles one service to confirm: CopyOnWriteArrayList add and remove at the top of the CPU and allocation profiles, and CPU going back to baseline once the rollback lands. The technical cause is that CopyOnWriteArrayList copies its whole array on every write.
It's designed for listeners registered once and iterated many times. Registering and unregistering per request turns it into a write-heavy structure: two full array copies per request, serialized under one lock, plus lots of garbage. Afterwards, I'd fix the design.
Ideally the listener is registered once at startup and dispatches per request, or the per-request state goes in the request context. If per-request registration is really needed, use a ConcurrentHashMap keyed by request id, which is O(1) with no copying.
Then I'd make sure it can't recur: add an allocation-per-request test or a JMH benchmark on the hot path to the library's build, and change the release process so shared libraries roll out to canary services first and are compared on CPU and allocation before the fleet gets them.
And I'd write a blameless review focused on the missing gates, not on the person.
About this answer
- Last updated
- 12 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 documentation below. 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 "A fleet-wide CPU increase after a shared library release is traced to code that...": https://scrivio-interview-library.netlify.app/java/concurrent-collections/d9c2263fab/