Quick revision
The short answer
During each reload the library holds the write lock while it rebuilds the map, and the write lock blocks every reader, so every request in every service waits until the reload finishes. I would redesign it to build the new data completely outside any lock and then publish it as an immutable snapshot through a single volatile reference. Readers never block and always see one complete version; the cost is briefly holding two copies in memory, which must be budgeted, and the library should expose a version number and last-reload metrics.
What to remember
In this answer, we saw that the spikes come from the write lock held for the whole reload, which blocks every reader in every service until the rebuild finishes. The redesign builds the new data outside any lock and publishes it as an immutable snapshot through a volatile reference, so readers never block and always see one complete version, and failed or suspicious reloads leave the old version in place. The cost is briefly holding two copies, which must be budgeted, and the library should expose a version number, data age and reload metrics.
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.
During each reload the library holds the write lock while it rebuilds the map, and the write lock blocks every reader, so every request in every service waits until the reload finishes. I would redesign it to build the new data completely outside any lock and then publish it as an immutable snapshot through a single volatile reference. Readers never block and always see one complete version; the cost is briefly holding two copies in memory, which must be budgeted, and the library should expose a version number and last-reload metrics.
Why this question is asked
Reference data, such as currency codes, product categories or routing tables, is read on almost every request and changes rarely. When it lives in a shared library, the way it reloads becomes part of the latency of 40 services at once. Interviewers ask this design question to see whether we can spot why a 'correct' lock causes latency spikes, and whether we know the standard pattern for read-mostly data that changes as a whole. In this answer, we will recap the current design, explain the spikes, design the snapshot replacement in code, look at memory and failure handling, and add the operational signals the library needs.
1A Quick Recap of the Current Design
The library probably looks something like this. Readers take the read lock for each lookup. Every five minutes, a scheduled task takes the write lock, clears the map and refills it from a database or a file.
The design is correct in the sense that readers never see a half-filled map. But correctness is bought with blocking.
2Why Reloads Cause Latency Spikes
While a thread holds the write lock, no thread can hold the read lock. The reload holds the write lock for the whole rebuild, including the database query, which might take a second or two for a large table. So for that time, every request that needs reference data waits, which in practice is every request. The services see a spike in latency every five minutes, exactly matching the reload schedule.
It can be worse than the reload time. A writer waiting for the lock also makes new readers queue behind it, so requests start waiting even before the reload begins, as soon as the writer is in the queue. And if the source is slow or times out, the spike lasts as long as the timeout.
7 messages in order. Play them, or step through them one at a time.
Every request in every service waits for the whole reload.
3The Redesign: Build Outside, Publish a Snapshot
The key idea is that readers do not need to be stopped while new data is built; they only need to switch from the old version to the new version in one step. So we build the new map completely, with no lock held, from a private local variable that no other thread can see. When it is ready, we freeze it as an immutable map and publish it by writing a single volatile field. Readers read that field once per lookup and use whichever version they got.
Notice why this is safe. The volatile write publishes the snapshot with a happens-before edge, so any reader that sees the new reference also sees the fully built map inside it. Map.copyOf produces an unmodifiable map, so no reader can change it and the map itself needs no lock. And a reader always sees one complete version, old or new, never a mix, because a lookup reads the reference once. A caller that needs several lookups from the same version takes snapshot() once and uses it.
4What It Costs: Two Copies in Memory
For the short time between building the new map and the moment the last reader stops using the old one, both copies are in the heap. If the reference data takes 300 MB, the service needs room for about 600 MB at reload time, plus the temporary HashMap before Map.copyOf, unless we build the final structure directly. With 40 services, that must be part of each service's memory budget and heap sizing, otherwise reloads could trigger long garbage collections or even OutOfMemoryError.
For very large data there are ways to reduce this: build into the final immutable structure directly instead of copying, reload only changed partitions if the source supports it, or keep the data off-heap. For most reference data, a few tens of megabytes, the double copy is simply accepted and documented.
| Aspect | Write lock rebuild | Snapshot swap |
|---|---|---|
| readers during reload | blocked for the whole rebuild | never blocked |
| what a reader sees | old data or new data | one complete version, old or new |
| slow or failed source | spike as long as the timeout | readers keep the old version |
| memory at reload | one copy | two copies briefly |
| reader cost | read lock atomic updates | one volatile read |
5Handling Failed or Bad Reloads
A snapshot design also makes failure handling much better. If the source query fails or times out, the reload simply throws before the publish line, and readers carry on with the previous version. If the new data looks wrong, for example it is empty or 90 percent smaller than the last version, validate() can refuse it and keep the old one, logging an alert instead of breaking 40 services with an empty table. In the old design, map.clear() had already destroyed the old data by the time a failure could be detected.
6Operational Signals: Version and Last Reload
A shared library is only as good as our ability to see what it is doing in each service. Each snapshot carries a version number and a load time, so the library can expose metrics through Micrometer: the current version, the age of the data in seconds, the reload duration, the entry count and the number of failed reloads. A dashboard across 40 services then shows immediately if one service is stuck on an old version, and an alert fires when data age passes, say, three reload intervals.
The version is useful in logs too: logging the reference data version with an important decision, such as a price calculation, lets us explain later which data was used. Finally, the release of the redesigned library should go out through canary services, comparing latency during reloads, which is exactly where the improvement should be visible.
7Follow-up questions
Why not use ConcurrentHashMap and update entries in place?
Readers would never block, but during a reload a reader could see some keys from the new data and some from the old, and deleted keys need separate handling. For reference data that changes as a whole, a complete version swap is simpler and gives consistent reads.
Does the reload thread need a lock at all?
Not if exactly one scheduled thread performs reloads. If reloads can also be triggered manually, the reload method should be synchronized, or use a single-flight guard, so two reloads do not race; readers are still unaffected because they never take that lock.
How would you roll this out to 40 services safely?
Keep the public API the same, so services only bump a version. Release to a few canary services first, compare p99 latency at reload times and memory use, then raise the version in the shared BOM. The metrics added in the same release show whether every service is on the new behaviour.
8Conclusion
In this answer, we saw that the spikes come from the write lock held for the whole reload, which blocks every reader in every service until the rebuild finishes. The redesign builds the new data outside any lock and publishes it as an immutable snapshot through a volatile reference, so readers never block and always see one complete version, and failed or suspicious reloads leave the old version in place. The cost is briefly holding two copies, which must be budgeted, and the library should expose a version number, data age and reload metrics.
Open with
The spikes come from the reload holding the write lock for the whole rebuild, including the database query. While the write lock is held no reader can get in, so every request in every service waits until the reload finishes, every five minutes.
Then, if they want more
And because a waiting writer makes new readers queue behind it, the wait starts even before the rebuild. I would redesign it as an immutable snapshot.
The reload builds a completely new map in a local variable with no lock held, validates it, freezes it with Map.copyOf, and publishes it by assigning a single volatile field that holds a snapshot with the map, a version number and a load time.
Readers read that volatile field and look up the key, so they never block, and because the volatile write gives happens-before, they always see a fully built, consistent version. Callers that need several lookups from one version take the snapshot once.
A failed or suspicious reload, say an empty result, throws or is rejected before the publish, so readers keep the old data. The cost is memory: both copies exist briefly, so if the data is 300 MB we budget around 600 MB at reload time.
Finally, the library should expose the version, data age, reload duration and failure count as metrics, and I would roll it out through canaries comparing latency at reload times.
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 shared reference-data library used by 40 services guards a large map with a...": https://scrivio-interview-library.netlify.app/java/locks/b5eebc90f6/