Scrivio Interview library
DesignAdvanced7 min read · updated 12 November 2026

Your platform team provides an in-memory registry (routing table and feature flags) used by many services, read on every request and updated a few times a minute from a control plane. Choose between a ConcurrentHashMap updated in place and an immutable snapshot replaced atomically, and define the consistency guarantees.

Updating a ConcurrentHashMap in place keeps every single key consistent, but a request that reads several keys during an update can see some old entries and some new ones, for example a new route pointing at a cluster whose feature flag is still off. Publishing a complete immutable snapshot through a volatile field or an AtomicReference lets each request take one reference and see exactly one version. At a few updates per minute, copying the whole registry per update is cheap, so I would choose snapshots, stamp each one with a version number, and document the guarantee as per-request consistency within an instance and eventual propagation across instances.

Why this question is asked

Shared in-memory registries sit on the hottest path a platform has: every request consults routes, flags, quotas or tenant settings, often several of them. When they are updated while traffic flows, the question of what a single request is allowed to see stops being academic. A request that routes with the new table but checks the old flag can call a backend that is not ready, and such bugs are almost impossible to reproduce. Interviewers ask this at senior level because it tests whether we can turn a data-structure choice into a clear consistency contract. In this answer, we will recap the two options, show the mixed-version problem with in-place updates, build the snapshot design, look at its cost, add versioning, and write down the guarantees.

1A Quick Recap of the Two Options

The first option is one long-lived ConcurrentHashMap. When the control plane, the central service that owns the configuration, sends a change, we call put or remove for each changed entry. Readers call get whenever they need a value. Each get is thread-safe and sees the latest completed put for that key.

The second option is an immutable snapshot. We hold the whole registry in an object that never changes after it is built, for example a record containing Map.copyOf of the routes and the flags. To update, we build a brand-new snapshot from the old one plus the changes, and then replace a single reference to point at it. Readers read that reference once and use the object it points to for the rest of the request.

2Why In-Place Updates Can Mix Versions

Suppose version 41 of the configuration routes /checkout to cluster A with the flag newCheckout off. Version 42 moves /checkout to cluster B and turns newCheckout on. Cluster B only works with the new checkout flow. The update arrives as two puts, and a request reads both values in between them.

Java
// writer applying version 42 in place
routes.put("/checkout", "cluster-b"); // step 1
flags.put("newCheckout", true); // step 2
// a request running between step 1 and step 2
String target = routes.get("/checkout"); // "cluster-b" (new)
boolean useNew = flags.get("newCheckout"); // false (old)
// the request sends the old flow to the new cluster: an error nobody can reproduce later
What a request can observe
DesignSingle keySeveral keys in one request
ConcurrentHashMap updated in placeLatest completed writeMix of old and new entries possible
Immutable snapshot behind volatileValue from the snapshot it holdsAlways one complete version

Each key was perfectly consistent; the map did nothing wrong. The problem is that the request needed a consistent view across keys, and in-place updates cannot give that. Even within one map, a reader iterating the routes while a batch of puts is applied sees a weakly consistent mix of entries. Reordering the writes only moves the window around, it never closes it, and adding a lock around every read would put a lock on the hottest path of the platform.

3Building the Snapshot Design

The snapshot design needs three things: an immutable value, a single reference that is safely published, and readers that take the reference once per request. Safe publication means that when another thread sees the new reference, it also sees everything written into the object before the reference was set. A volatile field or an AtomicReference gives exactly that guarantee under the Java memory model.

Java
record Registry(long version, Map<String, String> routes, Map<String, Boolean> flags) {
Registry {
routes = Map.copyOf(routes); // immutable copies; the record can never change
flags = Map.copyOf(flags);
}
}
final class RegistryHolder {
private final AtomicReference<Registry> current =
new AtomicReference<>(new Registry(0, Map.of(), Map.of()));
Registry snapshot() { return current.get(); } // one volatile read per request
void apply(ConfigUpdate u) { // called by the control-plane listener
current.updateAndGet(old -> {
if (u.version() <= old.version()) return old; // ignore stale or duplicate updates
var routes = new HashMap<>(old.routes()); routes.putAll(u.routes());
var flags = new HashMap<>(old.flags()); flags.putAll(u.flags());
return new Registry(u.version(), routes, flags);
});
}
}
// in request handling
Registry r = holder.snapshot(); // version 42
String target = r.routes().get("/checkout"); // "cluster-b"
boolean useNew = r.flags().get("newCheckout"); // true: same version as the route
Step throughPublishing a new version
Control planeUpdaterHolderRequest
snapshot()
version 41
update to v42
copy v41 and apply changes
set reference to v42
finishes on v41 unchanged
next request: snapshot()
version 42

8 messages in order. Play them, or step through them one at a time.

Notice the discipline on the reader side. The request calls snapshot() once and passes the Registry down, instead of calling the holder again in each layer. If it called the holder twice, it could still get two different versions. A request-scoped object or simply a method parameter carries the snapshot through the call.

4What the Snapshot Costs

The cost is a full copy of the registry for every update. Let us put numbers on it. A registry with 20,000 routes and 2,000 flags is a few megabytes at most, and copying it takes a few milliseconds. At five updates per minute that is well under a tenth of a percent of one core, and the old snapshot becomes garbage as soon as the last request holding it finishes, typically within a second. Reads, meanwhile, become as cheap as they can be: one volatile read and then plain immutable map lookups with no locking.

The calculation changes if updates become frequent and the registry is large, say hundreds of updates per second on a million entries. Then the copying would dominate, and we would look at persistent data structures that share unchanged parts, or partition the registry into independent snapshots per domain. For the stated workload, the simple copy is the right answer.

5Versions and the Written Guarantee

Every snapshot carries the version number assigned by the control plane. We log it with each request, expose it on an admin endpoint and as a metric, and the updater rejects versions older than the current one, which protects against updates arriving out of order. When a support engineer asks why a request went to cluster B at 10:42, the log line answers it: the request used registry version 42.

Then we write the contract down for the teams that use the registry. Within one instance, each request sees one complete version, and versions only move forward. Across instances, propagation is eventual: after the control plane publishes version 42, each instance applies it within its own delay, normally a second or two, so two instances can briefly serve different versions. Code that needs an all-or-nothing switch across the fleet must not rely on the registry alone; it should check the version or use a staged rollout.

6Follow-up questions

Why not a ReadWriteLock around the ConcurrentHashMap?

A read lock held for the whole request would give consistency, but every request on the hottest path would touch shared lock state, and an update would have to wait for all in-flight requests, or block new ones, to get the write lock. The snapshot gives the same consistency with a single volatile read and never blocks readers.

Is a volatile field enough, or do we need AtomicReference?

A volatile field is enough for publication if only one thread writes it, such as a single control-plane listener. AtomicReference adds compare-and-set and updateAndGet, which matter when more than one thread may update, or when we want the stale-version check to be atomic with the swap.

7Conclusion

In this answer, we compared two ways to serve a hot, rarely updated registry. Updating a ConcurrentHashMap in place keeps each key consistent but lets a request see a mix of old and new entries across keys. Publishing an immutable snapshot through a volatile field or an AtomicReference gives each request one complete version, and at a few updates per minute the cost of a full copy per update is negligible. We added a version number to every snapshot for tracing and ordering, and documented the guarantee as per-request consistency within an instance with eventual propagation across instances.

What to say in the interviewAbout 90 seconds aloud

Open with

I would choose an immutable snapshot replaced atomically. With a ConcurrentHashMap updated in place, each key is consistent, but a request that reads several keys during an update can see a mix: for example the new route to cluster B and the old feature flag that cluster B depends on.

Then, if they want more

That kind of bug is very hard to reproduce. With a snapshot, I build an immutable registry object, a record with Map.copyOf for routes and flags, and publish it through an AtomicReference or a volatile field, which also guarantees the contents are visible to readers.

Each request reads the reference once, passes the snapshot down, and sees exactly one version from start to finish.

The cost is copying the whole registry on every update, but at a few updates a minute, even twenty thousand routes is a few milliseconds of work and a short-lived garbage object, while reads become a single volatile read with no locks at all.

Each snapshot carries the control plane's version number; the updater ignores older versions, and we log the version with every request so behaviour can be traced.

Then I would document the guarantee: within an instance, every request sees one consistent version and versions only move forward; across instances propagation is eventual, typically within a couple of seconds, so anything that needs a fleet-wide atomic switch needs a staged rollout or a version check.

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 "Your platform team provides an in-memory registry (routing table and feature flags) used...": https://scrivio-interview-library.netlify.app/java/concurrent-collections/2423bc7963/