Quick revision
The short answer
ARM has a weaker hardware memory model than x86, so reorderings that x86 never performs, such as a store becoming visible before an earlier store, now really happen, and fields shared without volatile or locks expose them. The cache had data races all along; x86 simply hid some of them. The plan is to fix the races with proper synchronization, volatile or immutable snapshots rather than architecture-specific workarounds, add jcstress or stress tests and run CI on ARM as well as x86, and audit the other services moving to ARM for the same patterns.
What to remember
In this answer, we connected the corrupted cache to the memory model: the code had data races all along, x86's strong ordering hid most of them, and ARM's weaker ordering lets a reader see a new reference before the fields it points to. We fixed the race with an immutable record published through a volatile field rather than with platform-specific workarounds, added jcstress and stress tests that run in CI on ARM as well as x86, and planned an audit of other services moving to ARM for the same patterns.
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.
ARM has a weaker hardware memory model than x86, so reorderings that x86 never performs, such as a store becoming visible before an earlier store, now really happen, and fields shared without volatile or locks expose them. The cache had data races all along; x86 simply hid some of them. The plan is to fix the races with proper synchronization, volatile or immutable snapshots rather than architecture-specific workarounds, add jcstress or stress tests and run CI on ARM as well as x86, and audit the other services moving to ARM for the same patterns.
Background to this question
Teams move to ARM-based cloud instances to save money, and most Java services move without any change, because the JVM runs the same bytecode everywhere. Then one service starts showing a rare corrupted entry, and the natural first suspicion is a JVM or hardware bug. This question tests whether we can connect the symptom to the memory model, resist the temptation of a quick platform-specific patch, and turn one incident into a safer migration for everyone. In this answer, we will look at the kind of code that causes this, see why x86 hid it, fix it properly, and then set up testing and an audit so it does not happen again.
1What the Code Probably Looks Like
The review found fields shared between threads without volatile or locks. A typical in-memory cache with this problem refreshes entries on a background thread and serves them to request threads.
Nothing in this code creates a happens-before edge between the refresh thread and the request threads. That makes the write of current and its reads a data race, and the Java Memory Model allows a reader to see the new reference before it sees the fields written into the object.
2Why x86 Hid the Bug
On x86, stores become visible to other cores in the order the core made them, and loads are not reordered with other loads. So once a reader sees the new reference, it almost always sees the fields too, because they were written before the reference and x86 keeps stores in order. The only remaining source of reordering is the JIT, which in this code may well leave the stores in order. The race existed, but the hardware almost never produced a bad outcome.
ARM allows stores to different addresses to become visible in a different order, and loads to be satisfied out of order. The write of current can reach another core before the writes of currency and amountCents. The reader then follows the reference to an object whose fields still look like their defaults. That is the rare corrupted state: a null currency, a zero price, a timestamp of 0.
So the honest diagnosis is that the code was wrong all along, under the language rules, on every platform. x86 just did not exercise the reordering, and the move to ARM made a latent bug visible.
5 messages in order. Play them, or step through them one at a time.
Allowed by the JMM because there is no happens-before edge, and performed by ARM hardware.
3Fixing the Races Properly
The fix should follow the language rules, not the architecture. Adding ARM-specific fences or relying on a JVM flag would make the code depend on the platform, and the same pattern elsewhere would still be wrong. For this cache, the cleanest fix is an immutable snapshot published through a volatile field.
Two things now protect the reader, and either alone would be enough here. The record's fields are final, and the JMM guarantees that a thread that obtains a reference to a properly constructed object sees its final fields as set in the constructor. And the volatile write of current happens-before every read that sees it, which also publishes the constructor's writes. Using both is good practice: the record documents that entries never change, and the volatile field documents that current is shared.
Where the shared state is more complex, such as a map that is updated entry by entry, the fix is a ConcurrentHashMap, or an immutable map rebuilt and swapped through a volatile field, or a lock around the compound operations. The rule is the same in every case: every shared mutable field needs a documented happens-before edge.
4Testing on ARM, Not Just on x86
The incident shows that tests on x86 cannot be trusted to catch this class of bug. Two kinds of tests help. jcstress, the OpenJDK concurrency stress harness, runs small concurrent actors millions of times and reports outcomes we marked as forbidden, such as seeing a non-null entry with a null currency. Service-level stress tests run many readers against a cache that refreshes rapidly and check every entry they read for consistency.
Both must run on ARM. The simplest step is to add ARM runners to CI for the services that will run there, so every build runs the concurrency tests on both architectures. Before the fix, the jcstress test should reproduce the forbidden outcome on ARM, which confirms the diagnosis; after the fix it should never appear.
5Auditing the Other Services
If one service had this pattern, others probably do too, especially those written by the same teams or using the same internal libraries. Before more services move, I would run a focused audit.
- Confirm with a jcstress test on ARMforbidden outcome appears
- Fix the race in codeimmutable snapshot, volatile, or lock
- Test passes on ARM and x86?YesShip the fixNoRevisit the fix
- Add ARM runners to CIfor every service moving
- Audit other servicesshared fields, DCL, custom lock-free code
5 steps in order. Play them, or step through them one at a time.
The audit can start with tooling: searches for non-final, non-volatile fields in classes used as singletons or caches, static analysis checks for inconsistent synchronization and unsafe lazy initialisation, and a list of hand-written lock-free or double-checked locking code. The findings then go through human review, ranked by how critical the service is. Services move to ARM after their critical shared state is either fixed or shown to be correctly synchronized.
6Follow-up questions
Could this be a JVM bug on ARM instead?
It is possible in principle, so we should check that the JDK build is a current, supported one for ARM. But when code review shows unsynchronized shared fields and the symptom is exactly what the memory model allows, the data race is by far the most likely cause. A jcstress test that reproduces the forbidden outcome, and stops reproducing it after the fix, settles the question.
Would adding volatile to every field fix it?
It would remove the races but is a blunt tool. It costs some performance on every access, and it does not fix compound updates such as check-then-act, which also need atomicity. An immutable snapshot behind one volatile reference is usually cleaner: one publication point, objects that cannot change, and readers that always see a consistent set of values.
Why not just keep this service on x86?
That only hides the bug again; the JMM allows the same outcomes on x86, and a future JIT optimisation could expose them there too. It also leaves the same pattern in other code that will move. Fixing the race once makes the code correct on every platform.
7Conclusion
In this answer, we connected the corrupted cache to the memory model: the code had data races all along, x86's strong ordering hid most of them, and ARM's weaker ordering lets a reader see a new reference before the fields it points to. We fixed the race with an immutable record published through a volatile field rather than with platform-specific workarounds, added jcstress and stress tests that run in CI on ARM as well as x86, and planned an audit of other services moving to ARM for the same patterns.
Open with
The connection is the hardware memory model. x86 keeps stores in order and loads in order, so if one thread writes an object's fields and then the reference, other threads almost always see them in that order, even without synchronization.
Then, if they want more
ARM is weakly ordered: it can make the write of the reference visible before the writes of the fields, and satisfy loads out of order. So with shared fields that have no volatile and no lock, a request thread can follow a fresh reference to an object that still looks half-built, which is exactly the rare corrupted cache entry.
The important point is that the code was wrong all along. There is no happens-before edge, so the Java Memory Model allows this outcome on any platform; x86 just hid it. My plan would be to first reproduce it with a jcstress test on ARM, to confirm the diagnosis.
Then fix the race with language-level tools, not ARM-specific tricks: for a cache, an immutable record with final fields published through a volatile reference, or a ConcurrentHashMap, or a lock for compound updates. Then add ARM runners to CI so concurrency and stress tests run on both architectures, since x86-only tests clearly do not catch this.
Finally, because the same patterns probably exist elsewhere, I would audit the other services before they move, using static analysis for unsynchronized shared fields and lazy initialisation, plus review of any hand-written lock-free or double-checked locking code, ranked by criticality.
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 passes all tests on developer laptops and x86 servers, but after moving to...": https://scrivio-interview-library.netlify.app/java/java-memory-model/218d1ed318/