Quick revision
The short answer
I would mitigate first, by rolling back or pinning the previous library version in the affected services, and only then confirm the cause with thread dumps, which would show many threads parked on the same ReentrantLock in the token refresh path while one thread holds it during a call to the auth server. The lasting fix is to refresh the token in the background before it expires and let request threads read the current token from a volatile field without any lock. Then I would add concurrent load tests to the library's release checks and roll shared library releases out gradually with canaries and latency comparison.
What to remember
In this answer, we saw that a global lock around token refresh serialised every request and, during refresh, made every request wait on a remote call, which explains a tenfold p99 rise across services. The response is to mitigate first by rolling back or pinning the previous version, preserving thread dumps that then confirm many threads parked on one lock owned by a thread in the auth call. The fix is a background refresh before expiry with lock-free volatile reads and a single shared refresh when the token has really expired, and the follow-up adds concurrent load tests to release checks and canary rollouts with latency comparison for shared libraries.
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.
I would mitigate first, by rolling back or pinning the previous library version in the affected services, and only then confirm the cause with thread dumps, which would show many threads parked on the same ReentrantLock in the token refresh path while one thread holds it during a call to the auth server. The lasting fix is to refresh the token in the background before it expires and let request threads read the current token from a volatile field without any lock. Then I would add concurrent load tests to the library's release checks and roll shared library releases out gradually with canaries and latency comparison.
Why this question is asked
Shared client libraries are a force multiplier in both directions: one good change improves every service, and one bad change hurts all of them at the same time. A global lock on a hot path is a classic way to do the second, because it turns parallel requests into a queue. Interviewers ask this as a leadership scenario to see the order of our actions, whether we restore service before investigating, and whether the follow-up fixes the process as well as the code. In this answer, we will set out the incident, go through mitigation, confirmation, the code fix, and the process changes that stop the next one.
1Understanding What Went Wrong
A token provider gives every outgoing call an access token, usually an OAuth token that expires after some minutes. The new release made the method that returns the token, and refreshes it when needed, hold one global ReentrantLock.
Two things go wrong. Every request, even when the token is valid, must take the lock, so all request threads in the process pass through one gate one at a time. And when the token is near expiry, one thread holds the gate during a remote call, so every other request waits for the auth server's response. If the auth server takes 300 ms, the p99 of every service inherits those 300 ms, which is easily a tenfold increase over a 30 ms baseline.
2Mitigate Before Investigating
As incident lead, the first job is to stop the harm. The fastest safe action is to return to the last good version of the library. The new release is the obvious suspect, because latency rose across many services at once and they share nothing else that changed. Depending on how dependencies are managed, that means pinning the previous version in the shared BOM, the bill of materials that fixes library versions, and redeploying the worst-hit services first, or switching the new code path off with a feature flag if the library has one.
I would also assign roles: one person coordinating and communicating status, a small group rolling back, and one or two engineers capturing evidence before the rolled-back services lose it. Thread dumps from a still-affected instance are exactly the evidence we need, so we take several from one instance before it is redeployed.
- Declare the incident and assign roles
- Capture thread dumps from an affected instancethree dumps a few seconds apart
- Roll back or pin the previous library versionworst-hit services first
- Latency back to baseline?YesConfirm root cause from the dumpsNoLook for a second cause
- Fix the library, review and release through canaries
- Post-incident review and process changes
6 steps in order. Play them, or step through them one at a time.
Restore service first; the dumps preserve what we need to understand it.
3Confirm the Cause With Thread Dumps
A thread dump lists every thread and what it is doing. Taken a few seconds apart, dumps show whether threads are stuck in the same place. In this incident, we would expect most request threads to look like this.
Notice the two halves of the story. Many threads are parked on the same lock address in TokenProvider.get, and one thread owns that lock, visible under 'Locked ownable synchronizers' because we used the -l option, while it reads from the auth server's socket. That confirms the lock, not the auth server alone, is what spreads the delay to every request. If the services use virtual threads, jcmd Thread.dump_to_file -format=json shows them too.
4Fix the Design, Not Just the Lock
Removing the lock is not enough, because then many threads would refresh the token at the same moment and flood the auth server. The better design separates reading from refreshing. Request threads read the current token from a volatile field, which never blocks. A single background task refreshes the token well before it expires and publishes the new one with a volatile write. Only if the token is actually expired, for example after a long outage, does a request thread wait, and then all waiters share one refresh.
7 messages in order. Play them, or step through them one at a time.
Notice that the hot path, get(), has no lock at all when the token is valid. The volatile field gives visibility: a reader that sees the new token also sees its fully built fields. Refresh failures do not hurt readers while the old token is still valid, and a real implementation would add retries with backoff and a metric for refresh failures.
5Release Checks That Would Have Caught It
The library's tests almost certainly ran each call on one thread, where a global lock costs nothing. The missing check is a load test with concurrent callers: for example 200 threads calling the client against a stub auth server that responds in 300 ms, run across a token expiry, with a budget such as 'p99 of get() under 1 ms when the token is valid'. That test turns this incident into a failed build.
A lightweight static rule helps as well: shared libraries should not hold a lock across a remote call, and reviewers of the library should ask about concurrency for any change on the request path.
6Roll Out Shared Libraries Gradually
Finally, the delivery process. A shared library release reached many services at once, so the blast radius was the whole estate. Instead, new versions should go first to a few canary services, or a canary instance of a few services, with automatic comparison of p50 and p99 latency and error rates against the previous version for a set period, for example a day of normal traffic. Only then is the BOM version raised for everyone.
The post-incident review, held without blame, should record all of this: the design flaw, the missing concurrent test and the all-at-once rollout. The last two are the real causes, because a design flaw will happen again in some form, and the process is what should catch it.
7Follow-up questions
Why not keep the lock but only take it when the token is near expiry?
That removes the lock from the common path, which helps a lot, but every thread that arrives during the refresh window still waits for the remote call. The background refresh ahead of expiry means request threads almost never wait at all, which is the behaviour we want for a client used on every request.
How would you tell the rollback fixed the problem and not a recovery of the auth server?
By comparing timing and scope. Latency should return to baseline per service exactly when that service redeploys with the old version, while services still on the new version stay slow. Thread dumps from before and after, and the auth server's own latency graph, complete the picture.
What metrics should the library expose after the fix?
Token age and time to expiry, refresh duration and failures, how often a caller had to wait for a refresh, and a histogram of get() latency. Those make a regression visible in dashboards before it becomes an incident.
8Conclusion
In this answer, we saw that a global lock around token refresh serialised every request and, during refresh, made every request wait on a remote call, which explains a tenfold p99 rise across services. The response is to mitigate first by rolling back or pinning the previous version, preserving thread dumps that then confirm many threads parked on one lock owned by a thread in the auth call. The fix is a background refresh before expiry with lock-free volatile reads and a single shared refresh when the token has really expired, and the follow-up adds concurrent load tests to release checks and canary rollouts with latency comparison for shared libraries.
Open with
First I would restore service. Since latency jumped across many services that share only the new library version, I would capture a few thread dumps from an affected instance and then roll back or pin the previous version in our BOM, starting with the worst-hit services.
Then, if they want more
Once latency is back, I would confirm the cause from the dumps: I expect most request threads parked on the same ReentrantLock in the token provider, and with jstack -l or jcmd Thread.print -l, one owner thread sitting in the HTTP call to the auth server.
That explains the p99: every request queued on one lock, and during refresh every request waited for a remote call. For the fix, I would not just remove the lock, because then many threads would refresh at once.
Instead a background task refreshes the token at about 80 percent of its lifetime and publishes it through a volatile field, so request threads read it without locking. If the token has truly expired, all callers share a single in-flight refresh.
Then I would fix the process: add a concurrent load test to the library's release checks, with many callers against a slow stub auth server and a latency budget, and release shared libraries through canaries with automatic p99 and error comparison before raising the version for everyone. The blameless review would record both the design flaw and the missing checks.
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.
- Official documentation
- java.util.concurrent, package documentation, JLS chapter 17, Threads and Locks
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 new release of a shared client library introduced a global ReentrantLock around token...": https://scrivio-interview-library.netlify.app/java/locks/691fe5f4f9/