Quick revision
The short answer
With a mutable config object guarded by locks, every read must take a lock, and a reader that reads several values needs to hold it across all of them. With an immutable snapshot in an AtomicReference, readers simply get() the current snapshot without any lock, and see either the old or the new version, never a mix. Updates build a new snapshot and publish it with set or compareAndSet (a volatile field also works with a single writer). Readers that need several consistent values read the reference once per operation, and the cost is one allocation per update, which is small when updates are rare.
What to remember
In this answer, we compared a mutable config protected by locks with immutable snapshots in an AtomicReference. With locks, every read pays for the lock and a reader of several values can see a mix unless it holds the lock across all of them. With snapshots, readers take the current object with one volatile read and see one consistent version, writers build and publish a new snapshot with set, compareAndSet or updateAndGet, and a failed or invalid update never becomes visible. Readers that need several values read the reference once per operation, and the cost is a small allocation per update, ideal for rarely changing config.
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.
With a mutable config object guarded by locks, every read must take a lock, and a reader that reads several values needs to hold it across all of them. With an immutable snapshot in an AtomicReference, readers simply get() the current snapshot without any lock, and see either the old or the new version, never a mix. Updates build a new snapshot and publish it with set or compareAndSet (a volatile field also works with a single writer). Readers that need several consistent values read the reference once per operation, and the cost is one allocation per update, which is small when updates are rare.
Why this question is asked
Configuration is a textbook case of read-mostly data: every request reads it, but an admin changes it perhaps a few times a day. The way we share it decides both correctness, whether a request can see half an update, and performance, whether thousands of readers contend on a lock. Interviewers ask this to see whether we can compare the two designs on concrete terms and know the details that make the snapshot approach correct. In this answer, we will build both designs, compare how readers and writers behave, look at the per-operation consistency rule, handle concurrent updates and notifications, and weigh the costs.
1A Quick Recap of the Two Designs
The first design keeps one mutable config object and protects it with a lock, for example a ReentrantReadWriteLock, which lets many readers in at once but makes writers wait for all readers to leave. Every getter takes the read lock, and the admin update takes the write lock while it changes fields.
The second design never changes a config object. Each version is an immutable record, and an AtomicReference holds the current one. An update creates a new record and swaps the reference. An AtomicReference is a small JDK class that holds one reference with volatile semantics and offers atomic operations such as compareAndSet.
2How Readers Behave in Each Design
In the locked design, each getter is correct on its own, but a request that reads timeoutMs() and then retries() takes the lock twice. An update can happen between the two calls, so the request may combine the new timeout with the old retry count, a mix that no admin ever configured. To avoid it, the reader would have to hold the read lock across both calls, which spreads locking code through the application. Every read also costs a lock acquisition, which under heavy load means contention on the lock's internal state.
In the snapshot design, a reader calls current() once and gets a Config object that will never change. Every value it reads from that object belongs to the same version, and no lock is involved. The volatile read inside AtomicReference.get() is very cheap, and the object's final fields are guaranteed visible.
| Aspect | Mutable config + lock | Immutable snapshot + AtomicReference |
|---|---|---|
| Read cost | lock and unlock per read | one volatile read |
| Several values in one operation | mix possible unless lock held | consistent if read once |
| Writer blocks readers | yes, during the update | never |
| Update cost | change fields in place | allocate a new snapshot |
| Failure halfway through an update | readers may see partial changes | old snapshot stays in use |
| Code in readers | locking everywhere, or getters only | get once, then plain calls |
3Updating: set, compareAndSet and volatile
When only one admin endpoint updates the config, and updates replace the whole config, set is enough, and a plain volatile field would also do. But if two admins can update at once, and each update changes only part of the config, a naive read-modify-write loses updates: both read version 5, each changes a different value, and the second set overwrites the first change. compareAndSet solves this by only replacing the reference if it still points to the version we started from; updateAndGet wraps the retry loop for us.
- Read the current snapshot
ref.get(), version 5 - Build a new record from itvalidation runs in the constructor
- compareAndSet(old, new) succeeds?Yesversion 6 is liveNoanother update won: retry from the top
- Notify listenersafter the switch, not before
4 steps in order. Play them, or step through them one at a time.
updateAndGet runs this loop for us.
The function passed to updateAndGet may run more than once if another update wins the race, so it must have no side effects; it only builds a new record. Validation fits naturally into the record's compact constructor: an invalid config is never built, so it can never be published.
4Reading Once per Operation
The snapshot design gives consistency only to code that reads the reference once per operation. A service method that calls holder.current().timeoutMs() and later holder.current().retries() can still see two versions. The rule for teams is simple: at the start of a request, or of any operation that uses several settings, take one Config and pass it along. For settings that are used independently, reading current() each time is fine, and it also means the operation picks up a change as soon as it happens.
5Weighing the Costs
The snapshot design costs one allocation per update: a new record and copies of its maps. A config with a few hundred entries is a few kilobytes, so even a hundred updates a day is nothing. The old snapshot becomes garbage once no request holds it. Compared with a read lock taken on every request, millions of times a day, this is a very good trade for read-mostly data.
The design is not suitable when updates are frequent and large, for example a counter changed thousands of times per second, because rebuilding the object each time would allocate heavily; there, a concurrent structure such as LongAdder or ConcurrentHashMap fits better. For configuration, the snapshot is almost always the right choice, and it is a common pattern in configuration and feature-flag libraries.
6Follow-up questions
How would you notify components that cache derived values from the config?
I would let the holder accept listeners and call them after each successful switch, passing the old and new snapshots so a listener can check whether the values it cares about changed. Listeners rebuild their derived value, such as a compiled regex or a connection pool size, and publish it the same way, as an immutable object in their own AtomicReference. A simpler alternative is to store derived values inside the snapshot itself, computed in its constructor, so they can never be out of step; or to cache them keyed by the snapshot's version number and recompute when the version changes.
Could we use Spring's @RefreshScope instead?
@RefreshScope in Spring Cloud recreates beans when configuration changes, which helps for beans that read config at construction. It does not give per-request consistency across several beans, and beans being recreated mid-request can surprise. For settings read on hot paths, an explicit immutable snapshot is easier to reason about.
What if a reader holds an old snapshot for a long time?
It keeps working with the old values until it reads again, which is correct for the operation already in progress. Long-running loops, such as a consumer polling a queue, should read the snapshot again at the start of each iteration so they pick up changes within one cycle.
7Conclusion
In this answer, we compared a mutable config protected by locks with immutable snapshots in an AtomicReference. With locks, every read pays for the lock and a reader of several values can see a mix unless it holds the lock across all of them. With snapshots, readers take the current object with one volatile read and see one consistent version, writers build and publish a new snapshot with set, compareAndSet or updateAndGet, and a failed or invalid update never becomes visible. Readers that need several values read the reference once per operation, and the cost is a small allocation per update, ideal for rarely changing config.
Open with
For config that is read on every request and updated a few times a day, I would use an immutable snapshot in an AtomicReference rather than a mutable object with locks.
Then, if they want more
With locks, every getter has to take a read lock, and if a request reads the timeout and then the retry count, an update can happen between the two calls, so it might combine the new timeout with the old retries, a configuration nobody chose. Avoiding that means holding the lock across both reads, which spreads locking code everywhere.
With snapshots, the config is a record with copied maps, validated in its constructor. Readers call get once, which is a cheap volatile read with no lock, and every value they read from that object belongs to the same version.
Writers build a new record and publish it with set; if there can be concurrent admins making partial changes, I would use updateAndGet so no update is lost, and with a single writer even a volatile field works. A failed or invalid update never becomes visible, because the old snapshot stays in place.
The rule for readers is to read the reference once per operation and pass it along. The cost is one small allocation per update, which is negligible here. For components that cache derived values, I would add listeners called after each switch with the old and new snapshots, or compute derived values inside the snapshot itself.
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
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
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 "You are designing a configuration system that many request threads read while an admin...": https://scrivio-interview-library.netlify.app/java/immutability/dab7de0be4/