Scrivio Interview library
DesignIntermediate6 min read · updated 12 November 2026

A service reloads a configuration map every minute from a remote store and many request threads read it. Design the reload so readers always see a consistent configuration without locking on each read.

Build each new configuration as a complete immutable map on the reload thread, for example with Map.copyOf, validate it, and then publish it with a single write to a volatile field or AtomicReference. Request threads read that reference once per request and use the same snapshot throughout, so they never lock and never see a half-updated configuration. The published map is never mutated; each reload replaces it, and if loading or validation fails, the old configuration stays in place.

Why this question is asked

Configuration that changes at runtime, such as feature flags, rate limits or routing rules, is read on almost every request, so the read path must be cheap. It is also read by many threads while a background thread updates it, so a careless design gives requests a mix of old and new values, or a map that throws ConcurrentModificationException. Interviewers ask this design question to see whether we know the immutable snapshot pattern and the memory-model reasons it works. In this answer, we will look at what goes wrong with an updated-in-place map, design the snapshot approach step by step, handle failures, and discuss the costs and variations.

1Why Updating the Map in Place Fails

The obvious first design keeps one map and updates its entries on each reload.

Java
private final Map<String, String> config = new HashMap<>();
void reload(Map<String, String> fresh) { // every minute
config.clear();
config.putAll(fresh);
}
int timeout() { return Integer.parseInt(config.get("timeoutMs")); } // NumberFormatException on null

This has two separate problems. A HashMap is not thread-safe, so readers running during clear() and putAll() can see a corrupted structure, missing entries, or exceptions. And even with a ConcurrentHashMap, which fixes the structural problem, a request can read some keys before the reload and some after: a timeout from the old configuration and a retry count from the new one, or a missing key during the moment between clear() and putAll(). Configuration values often belong together, so a mixed view can be worse than either version on its own.

Putting a lock around reads and reloads would fix both problems, but then every request takes the lock, and a slow reload blocks all requests. We want consistency without that cost.

2The Design: Build, Validate, Publish One Reference

The idea is to never change a configuration that readers can see. Instead, the reload thread builds a completely new one off to the side and then switches a single reference to point at it.

Step throughOne reload cycle
  1. Fetch from the remote storeon a scheduled reload thread
  2. Parse into a typed snapshotMap.copyOf, records
  3. Valid?
    YesPublish with one volatile writeNoKeep the old snapshot, log, alert
  4. Readers pick it upon their next request

4 steps in order. Play them, or step through them one at a time.

Each step has a reason. Building on the reload thread means no reader can see a partial map. Making it immutable means nobody can change it after publication, by accident or otherwise. Validating before publishing means a broken remote value never reaches requests. And publishing with one volatile write means the switch is atomic: a reader sees either the whole old map or the whole new one.

3Implementing It

A small holder class keeps the snapshot. Using a record for the snapshot makes the typed values and the raw map final and immutable together.

Java
public final class ConfigHolder {
public record Snapshot(Map<String, String> values, int timeoutMs, long version) {
public Snapshot {
values = Map.copyOf(values); // immutable copy, rejects null keys and values
if (timeoutMs <= 0) throw new IllegalArgumentException("timeoutMs must be positive");
}
}
private volatile Snapshot current; // the only shared mutable field
public ConfigHolder(Snapshot initial) { this.current = initial; }
public Snapshot get() { return current; } // one volatile read, no lock
void reload(RemoteStore store) { // scheduled every 60 s
try {
Map<String, String> raw = store.fetch();
Snapshot next = new Snapshot(raw, Integer.parseInt(raw.get("timeoutMs")), current.version() + 1);
current = next; // publish: one write
} catch (RuntimeException e) {
log.warn("config reload failed, keeping version {}", current.version(), e);
}
}
}

The volatile write of current happens-before every read that sees it, and the snapshot was fully built before that write, so a reader that gets the new reference sees every value in it. The record's fields are final as well, and Map.copyOf returns an unmodifiable map, so the snapshot cannot change after it is published. If fetch() throws, or parsing fails, or the compact constructor rejects a value, current is never assigned and requests keep the last good configuration. An AtomicReference<Snapshot> would work the same way and is useful if we later want compareAndSet, for example to avoid two concurrent reloads overwriting each other.

4Reading the Snapshot Once Per Request

The other half of consistency is on the reader's side. If a request calls holder.get() several times, it may see the old snapshot for one value and the new one for the next, because a reload can happen between the calls.

Java
// Inconsistent: two reads may see two different snapshots
client.call(holder.get().timeoutMs(), holder.get().values().get("retryPolicy"));
// Consistent: read once, use it for the whole request
ConfigHolder.Snapshot cfg = holder.get();
client.call(cfg.timeoutMs(), cfg.values().get("retryPolicy")); // both from the same version

Reading once at the start of the request, and passing the snapshot down or keeping it in a request-scoped object, gives every request a consistent view. Logging cfg.version() with the request also makes it easy to tell which configuration a given request used when investigating a problem.

5Costs and Variations

The read path is a single volatile read, which costs about the same as a plain read on x86 and a little more on ARM, with no lock and no contention. The write path allocates a new map once a minute, which is negligible. Old snapshots are garbage collected once no request holds them.

A few variations are worth knowing. If the remote store can push changes, the same publish step runs on each notification instead of on a timer. If parts of the configuration change independently and are large, we can keep one snapshot per section, each behind its own reference, as long as no request needs values from two sections to agree. And in Spring Boot, Spring Cloud's refresh support offers a similar idea at the bean level, but for values read on the hot path a holder like this keeps the behaviour explicit and easy to test.

6Follow-up questions

Why not use a ConcurrentHashMap and update entries?

It avoids corruption but not mixed views: a request can read one key before a reload and another after it, and there are moments where keys are missing. The snapshot design gives each request a single consistent version, which a mutable shared map cannot.

What if two reloads run at the same time?

With a single scheduled thread they cannot. If reloads can also be triggered manually or by notifications, an older fetch could finish after a newer one and overwrite it. Either serialize reloads on one thread, or compare versions and publish with AtomicReference.compareAndSet only when the new version is higher.

How would you test it?

Unit tests cover validation and the keep-the-old-config-on-failure behaviour. A concurrency test runs many reader threads that check an invariant across values in each snapshot, for example that two related keys always come from the same version, while a writer reloads in a tight loop. Readers should never see a mixed snapshot.

7Conclusion

In this answer, we designed configuration reloads around immutable snapshots. The reload thread builds and validates a complete immutable map, publishes it with one write to a volatile field or AtomicReference, and never mutates it afterwards. Readers read the reference once per request and use that snapshot throughout, so they never lock and never see a mix of versions, and a failed reload leaves the last good configuration in place.

What to say in the interviewAbout 100 seconds aloud

Open with

I would use the immutable snapshot pattern. The reload thread fetches the configuration from the remote store, parses it into a snapshot object, say a record with typed fields plus a Map.copyOf of the raw values, and validates it, all off to the side where no reader can see it.

Then, if they want more

Only if it is valid does it publish it, with a single write to a volatile field or an AtomicReference. If fetching or validation fails, we log and alert and keep the old snapshot. The published snapshot is never mutated; every reload replaces it with a new object.

This works because the volatile write happens-before every read that sees it, and the snapshot was fully built before that write, so readers see either the complete old configuration or the complete new one, never a half-update. The record's final fields and the unmodifiable map add the final field guarantee and stop accidental changes.

On the read side, each request reads the reference once at the start and uses that snapshot throughout, rather than calling get several times, so two related values always come from the same version; logging the snapshot version per request helps debugging.

The read cost is one volatile read with no lock, and the write cost is one new map a minute, which is negligible. If reloads can be triggered from several places, I would serialize them or use compareAndSet with a version number so an older fetch cannot overwrite a newer one.

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
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 service reloads a configuration map every minute from a remote store and many request...": https://scrivio-interview-library.netlify.app/java/java-memory-model/845cff097b/