Scrivio Interview library
ScenarioAdvanced6 min read · updated 12 November 2026

A team is building a pooled-object, lock-free buffer manager to reduce GC in a low-latency service, reusing node objects. In design review, what concerns would you raise about ABA and maintainability?

I would raise four concerns. Reusing nodes removes the protection garbage collection gives lock-free code against ABA, so plain CAS on references can succeed with stale state, and fixing that needs version stamps or another scheme that adds complexity and cost. Before accepting that, the team should measure whether GC is really the bottleneck, since modern collectors such as generational ZGC may already meet the latency goal. If pooling is justified, they should build on proven libraries such as JCTools or Agrona, and the code must come with jcstress tests and a named owner.

Background to this question

Low-latency teams sometimes reach for object pooling and lock-free code together: pooling to avoid allocation and GC pauses, lock-free code to avoid lock waits. Each idea is reasonable alone, but combining them creates a classic trap, and the result is code few people can maintain. In a design review, a senior engineer's job is to find that trap early, ask for evidence and set conditions rather than simply approve or reject. In this answer, we will recap why GC protects lock-free code, show how pooling breaks that, discuss what a fix costs, ask whether GC is the problem at all, and then set out the review conditions.

1What Garbage Collection Quietly Gives Lock-Free Code

Lock-free structures such as stacks and queues use compare-and-set on references: 'set head to n, but only if head is still the node I read'. That check is only meaningful if 'the same reference' means 'the same moment in the structure's history'. In Java this is normally true, because a node a thread still holds cannot be reclaimed by the garbage collector, and a newly allocated node always has a new identity. So the ABA problem, where a value changes from A to B and back to A, cannot happen with fresh nodes.

This protection is invisible, which is exactly why teams forget they depend on it. Most published Java lock-free algorithms assume it.

2How Pooling Breaks It

A pool keeps released node objects and hands them out again. Now the same object can leave the structure and come back while some other thread is paused holding a reference to it. That thread's CAS sees the expected reference and succeeds, but the node's fields, its next pointer, its buffer and its length, now belong to a different use. The result is a buffer handed to two owners, a lost node, or a corrupted list, and it happens only under rare timings, typically in production under load.

There is a second, related hazard: a thread may still be reading a node's buffer after the node was returned to the pool and reissued, so it reads another request's data. GC prevents that too, and pooling removes the protection for both.

Step throughA recycled node fools a CAS
Thread 1Free list headThread 2Pool
read head = N, N.next = M
paused
take N, take M
return N to pool
N reissued and pushed back
CAS(N, M)
succeeds: M is in use elsewhere

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

With pooling, the same node object returns, so reference equality no longer proves nothing changed.

3What a Fix Costs

The team then needs version stamps: AtomicStampedReference, which allocates an internal pair on every update and so defeats part of the point of pooling, or a packed long holding an index and a version, which means turning the design into an array of slots addressed by index. Either way, every operation reads and writes more state, the code becomes harder to read, and there are new questions to answer: what happens when the version wraps, how are slots handed back, which memory ordering does each access need. Each of those is a place for a subtle bug, and the bugs only appear under concurrency that ordinary tests do not reproduce.

So the real proposal is not 'a pooled buffer manager' but 'a custom lock-free allocator with versioned indexes', which is a much bigger thing to own.

4Is Garbage Collection Really the Bottleneck?

Before we pay that price, we ask for evidence. What are the latency targets, for example p99.9 under 2 ms, and what do GC logs or JFR recordings show as pause times and allocation rates today? Short-lived objects in Java are cheap: allocation in a thread-local buffer is a pointer bump, and objects that die young cost almost nothing to collect. With generational ZGC, the default mode of ZGC in recent JDKs, pause times are typically well under a millisecond regardless of heap size, and Shenandoah has similar goals.

Often the measurement shows that GC is fine and the latency comes from elsewhere: I/O, locks, a noisy neighbour, or a large allocation such as copying buffers that could be avoided by reusing a few large off-heap buffers with ordinary, simple ownership. If GC does turn out to matter, the cheapest step is usually tuning or a collector change, followed by reducing allocation in the hottest code, and only then pooling.

Step throughQuestions for the review
  1. Latency target missed because of GC?
    YesNext questionNoLook elsewhere; no pooling
  2. Fixed by ZGC or tuning?
    YesChange collector, keep simple codeNoNext question
  3. Can a proven library do it?
    YesUse JCTools or AgronaNoCustom code with conditions
  4. Conditions met?jcstress tests, expert review, named owner, fallback

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

5Prefer Proven Libraries

If pooling and lock-free hand-off are justified, the team is not the first to need them. JCTools provides well-tested lock-free queues specialised by number of producers and consumers, such as single-producer single-consumer and multi-producer single-consumer queues, many of them array-based so they do not allocate per element. Agrona provides ring buffers and off-heap buffer utilities used in low-latency messaging. Netty has a mature pooled buffer allocator with reference counting. These libraries have been through years of production use and stress testing on several architectures, which a new in-house design cannot match.

6Conditions If Custom Code Is Still Needed

If none of that fits, I would approve custom code only with conditions. It needs a written correctness argument: the invariants, the linearization point of each operation, and how ABA and use-after-reissue are prevented. It needs jcstress tests, the OpenJDK harness that runs tiny concurrent scenarios millions of times and reports forbidden outcomes, run on both x86 and ARM, because ARM's weaker memory model exposes ordering bugs that x86 hides. It needs review by someone experienced in lock-free code, a named owner plus a second maintainer, a feature flag or fallback to a simpler implementation, and benchmarks proving the gain at the service level. Those conditions are not bureaucracy; they are the minimum that makes such code safe to run and possible to keep.

7Follow-up questions

Is pooling ever a good idea in Java?

Yes, for objects that are genuinely expensive to create, such as large direct buffers, connections or threads. Pooling small, short-lived objects to save GC is rarely worth it today, because allocation is cheap and pooled objects live long enough to be promoted, which can make GC work harder.

What does jcstress catch that unit tests do not?

It runs two or more actors concurrently on shared state many millions of times and records every outcome, flagging results that should be impossible, such as a value read twice or a lost update. Unit tests run one interleaving at a time and almost never hit the rare ones.

How would you measure the latency effect of GC?

With GC logs or JFR, correlating pause events with latency outliers, and with a load test that compares the current collector against generational ZGC at production allocation rates. If outliers do not line up with pauses, GC is not the cause.

8Conclusion

In this answer, we reviewed a pooled, lock-free buffer manager. Garbage collection normally protects lock-free code from ABA and from use-after-reissue, and reusing nodes removes that protection, so the design would need version stamps or index schemes that add complexity and cost. We asked for evidence that GC is really the bottleneck, given modern collectors such as generational ZGC, preferred proven libraries such as JCTools, Agrona and Netty's allocator, and set conditions for any custom code: a correctness argument, jcstress tests on x86 and ARM, expert review and a named owner.

What to say in the interviewAbout 90 seconds aloud

Open with

My first concern would be ABA. In Java, lock-free structures that CAS on references are protected by the garbage collector: a node a thread still references can't be reclaimed, and a new node always has a new identity, so a CAS that sees the same reference really means nothing changed.

Then, if they want more

Pooling breaks that. A node can leave the structure, be reissued and come back while another thread is paused holding it, and that thread's CAS succeeds with stale state, so we get a buffer with two owners or a corrupted list, only under rare timing.

Fixing it needs version stamps, either AtomicStampedReference, which allocates on every update, or packed index-plus-version longs, which makes the design much more complex. So my second point is to measure first: what's the latency target, and do GC logs or JFR show pauses lining up with the outliers?

Generational ZGC typically keeps pauses well under a millisecond, and young garbage is cheap, so a collector change may solve it without any custom code. Third, if pooling is justified, I'd use something proven, JCTools queues, Agrona ring buffers or Netty's pooled allocator, rather than a new design.

And if custom code is still needed, I'd set conditions: a written correctness argument, jcstress tests run on x86 and ARM, review by someone experienced in lock-free code, a named owner and a second maintainer, and a fallback to a simple implementation.

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 "A team is building a pooled-object, lock-free buffer manager to reduce GC in a...": https://scrivio-interview-library.netlify.app/java/atomics/59298c7068/