Skip to main content
← PROBLEMS / CONCURRENCY
An end-to-end domain learning journey

Double-Checked Locking &
Memory Barriers.

After migrating a high-throughput Java 21 microservice from x86-64 (TSO) to AWS Graviton ARM64 instances, the service experiences rare NullPointerExceptions and zeroed configuration fields during startup bursts. The culprit is a lazy-initialized RoutingTable singleton using Double-Checked Locking (DCL) without volatile. Explain the exact hardware/JIT reordering failure, fix the DCL implementation with minimal read-path overhead, and compare it against alternative safe lazy-initialization patterns.
Start the journey5 stages
Loading diagram…
Double-Checked Locking & Memory Barriers architecture sketch diagram
FIRST PRINCIPLES. PRODUCTION SCALE.
The idea to keep with you

After migrating a high-throughput Java 21 microservice from x86-64 (TSO) to AWS Graviton ARM64 instances, the service experiences rare `NullPointerException`s and zeroed configuration fields during startup bursts. The culprit is a lazy-initialized `RoutingTable` singleton using Double-Checked Locking (DCL) without `volatile`. Explain the exact hardware/JIT reordering failure, fix the DCL implementation with minimal read-path overhead, and compare it against alternative safe lazy-initialization patterns.

From patterns to decisions.

After migrating a high-throughput Java 21 microservice from x86-64 (TSO) to AWS Graviton ARM64 instances, the service experiences rare `NullPointerException`s and zeroed configuration fields during startup bursts. The culprit is a lazy-initialized `RoutingTable` singleton using Double-Checked Locking (DCL) without `volatile`. Explain the exact hardware/JIT reordering failure, fix the DCL implementation with minimal read-path overhead, and compare it against alternative safe lazy-initialization patterns.

01
CONCURRENCY · STAGE 01L4 · FUNDAMENTALS

x86-64 TSO preserves store-store order.

x86-64 TSO preserves store-store order in hardware, whereas ARM64 allows the pointer store to become visible before constructor field stores.

ChoiceWhat it buysWhat it costs
Trade-off 1Understanding hardware memory models prevents subtle cross-architecture production outages.

Break `new RoutingTable()` into

(1) allocate, (2) init fields, (3) assign instance.

Step 2

Show how (1) -> (3) -> (2) passes if (instance != null) on Thread B while fields are still null/0.

Alternative 1

Testing under JCStress on ARM64 to surface weak-memory-model races in CI.

Failure Mode 1

Assuming final fields alone save DCL if the instance reference itself is read without a barrier (in practice final has freeze semantics, but non-final fields or JIT optimizations still break).

// BROKEN without volatile:
// Thread A: 1) mem = alloc(); 3) instance = mem; [Thread B reads instance != null!] 2) mem.routes = load();
// Thread B: routingTable.routes.get(k) -> NullPointerException!
02
CONCURRENCY · STAGE 02L5 · APPLIED DESIGN

Use volatile for happens-before safety.

Use volatile for happens-before safety and a local variable result to perform only one volatile read on the hot path.

ChoiceWhat it buysWhat it costs
Trade-off 1Local variable caching avoids a second `ldar` instruction on every fast-path call.

Step 1

Declare private static volatile RoutingTable instance;.

Step 2

Read RoutingTable result = instance; once before the outer if.

Step 3

Synchronize on RoutingTable.class only when result == null.

Alternative 1

VarHandle.getAcquire() / setRelease() for even finer control.

Failure Mode 1

Assigning instance = new RoutingTable(...) inside synchronized without volatile still fails.

public final class RoutingTable {
    private static volatile RoutingTable instance;
    private final Map<String, String> routes;

    private RoutingTable(Map<String, String> routes) {
        this.routes = Map.copyOf(routes);
    }

    public static RoutingTable getInstance(Supplier<Map<String, String>> loader) {
        RoutingTable result = instance; // single volatile read
        if (result == null) {
            synchronized (RoutingTable.class) {
                result = instance;
                if (result == null) {
                    result = new RoutingTable(loader.get());
                    instance = result; // volatile store-release
                }
            }
        }
        return result;
    }
}
03
CONCURRENCY · STAGE 03L6 · SCALE & TRADE-OFFS

Use the Holder idiom for.

Use the Holder idiom for parameterless singletons with zero-barrier reads; avoid it when initialization needs runtime arguments or must be retryable on failure.

ChoiceWhat it buysWhat it costs
Trade-off 1Holder has ~0.5ns read cost vs. ~1.5ns for volatile DCL, but lacks retryability and runtime parameters.

Step 1

JVM class initialization lock guarantees thread-safe lazy loading of Holder.INSTANCE.

Step 2

If new RoutingTable() throws in <clinit>, the class is permanently poisoned with NoClassDefFoundError.

Alternative 1

Java enum singleton (eagerly initialized on first enum reference).

Failure Mode 1

Using Holder for I/O-dependent initialization that can fail transiently during boot.

public final class HolderSingleton {
    private HolderSingleton() {}
    private static class Holder {
        static final HolderSingleton INSTANCE = new HolderSingleton();
    }
    public static HolderSingleton getInstance() {
        return Holder.INSTANCE; // zero volatile read overhead
    }
}
04
CONCURRENCY · STAGE 04L6 · SCALE & TRADE-OFFS

Swap an immutable RoutingTable instance.

Swap an immutable RoutingTable instance atomically via AtomicReference or a volatile field updated by a background scheduler.

ChoiceWhat it buysWhat it costs
Trade-off 1Copy-on-write immutable swap gives lock-free reads at the cost of holding 2x table memory briefly during swap.

Step 1

Build the new immutable Map.copyOf(...) off the request thread.

Step 2

Atomically publish the new reference via volatile write (instance = nextTable).

Step 3

Readers continue using RoutingTable r = instance; with zero locks.

Alternative 1

StampedLock optimistic reads for fine-grained mutable structures.

Failure Mode 1

Mutating the underlying Map in place instead of swapping an immutable snapshot.

private static volatile RoutingTable current = new RoutingTable(Map.of());

public static void refresh(Map<String, String> nextRoutes) {
    current = new RoutingTable(nextRoutes); // atomic immutable swap
}

public static String lookup(String key) {
    return current.routes.get(key);
}
05
CONCURRENCY · STAGE 05L7 · STAFF / PRINCIPAL

Use OpenJDK JCStress on ARM64.

Use OpenJDK JCStress on ARM64 runners to exhaustively test interleavings of Actor initialization and Observer reads.

ChoiceWhat it buysWhat it costs
Trade-off 1JCStress catches JMM bugs in seconds that otherwise appear once a month in production.

Step 1

Annotate a test class with @JCStressTest and @Outcome(id = "0", expect = FORBIDDEN).

Step 2

Run on Graviton/ARM64 CI runners where weak memory ordering actively manifests.

Alternative 1

ThreadSanitizer (TSan) for C++/Go workloads.

Failure Mode 1

Running concurrency stress tests only on x86-64 laptops.

// @JCStressTest
// @Outcome(id = "42", expect = Expect.ACCEPTABLE)
// @Outcome(id = "0", expect = Expect.FORBIDDEN, desc = "Partially constructed object observed!")
// @State public class DclStressTest { ... }

Theory & Tips and Tricks

Practical heuristics & interview traps

  • 01Interview anchor: Without volatile, the JVM and ARM64 CPU can reorder pointer assignment before constructor completion; fix it with volatile (plus a local variable cache) or the lock-free Initialization-on-Demand Holder idiom.
  • 02Trade-off tip: Holder has ~0.5ns read cost vs. ~1.5ns for volatile DCL, but lacks retryability and runtime parameters.
  • 03Watch out for: Mutating the underlying Map in place instead of swapping an immutable snapshot.
Behavioral Progression · Double Checked Locking Memory Barriers

Double Checked Locking Memory Barriers — Normal → Load / Fault → Recovery

Send Request
1. Normal Request Ingress
Traffic: 1x baseline · Latency (p99): 4 ms
Process State
2. Core Execution & State Lookup
Traffic: 1x baseline · Latency (p99): 9 ms
Inject 10x Spike
3. Scale Spike / Failure Injection
Traffic: 10x burst · Latency (p99): 185 ms

Without volatile, the JVM and ARM64 CPU can reorder pointer assignment before constructor completion; fix it with volatile (plus a local variable cache) or the lock-free Initialization-on-Demand Holder idiom.

Synthesis / One connected design

Putting Double-Checked Locking & Memory Barriers Together

Without `volatile`, the JVM and ARM64 CPU can reorder pointer assignment before constructor completion; fix it with `volatile` (plus a local variable cache) or the lock-free Initialization-on-Demand Holder idiom.

A candidate architecture, not a universal answer

Putting Double-Checked Locking & Memory Barriers Together

Step 1
Step 2
Step 3
  1. Declare private static volatile RoutingTable instance;.
  2. Read RoutingTable result = instance; once before the outer if.
  3. Synchronize on RoutingTable.class only when result == null.
Swap an immutable `RoutingTable` instance atomically via `AtomicReference` or a `volatile` field updated by a background scheduler.
Interview Readiness Check

Swap an immutable RoutingTable instance atomically via AtomicReference or a volatile field updated by a background scheduler.

Walk through bottlenecks, back-of-the-envelope math, and failure recovery.

Practice / Explain the consequences

Make the trade-off yourself.

Practice Check

Test your understanding

Question 1 of 3

When is ACID Transaction Fundamentals the right default for a production team?

Key takeaway

Double-Checked Locking & Memory Barriers

Without volatile, the JVM and ARM64 CPU can reorder pointer assignment before constructor completion; fix it with volatile (plus a local variable cache) or the lock-free Initialization-on-Demand Holder idiom.