Quick revision
The short answer
I would start by asking for the profile and the effect on the service's real goals, because a queue appearing in a profile rarely means it is the main cost. Then I would ask the engineer to try existing options first: a different JDK queue, batching, or a proven library such as JCTools. Only if custom code is still justified would I agree, with conditions: a written correctness argument, jcstress tests, review by someone experienced in lock-free code, a narrow scope with a fallback to a standard queue, and more than one person able to maintain it.
What to remember
In this answer, we evaluated a proposal to write a custom lock-free queue. We asked for the profile and its effect on service-level targets, understood why ConcurrentLinkedQueue costs what it does, and tried cheaper options first: batching, other JDK queues and proven libraries such as JCTools and the Disruptor. For custom code we set conditions: a correctness argument, jcstress tests on x86 and ARM, expert review, a narrow scope with a fallback to a standard queue, and at least two maintainers, framed as what makes the work safe rather than as obstacles.
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.
I would start by asking for the profile and the effect on the service's real goals, because a queue appearing in a profile rarely means it is the main cost. Then I would ask the engineer to try existing options first: a different JDK queue, batching, or a proven library such as JCTools. Only if custom code is still justified would I agree, with conditions: a written correctness argument, jcstress tests, review by someone experienced in lock-free code, a narrow scope with a fallback to a standard queue, and more than one person able to maintain it.
Why this question is asked
Proposals like this come from good engineers: they found something real in a profile and want to fix it properly. A senior person's role is neither to approve the enthusiasm nor to shut it down, but to turn it into a decision based on evidence, and to protect the service from code that only one person understands. Interviewers ask this leadership question to hear how we reason about risk, cost and team capacity, not just about queues. In this answer, we will go through the questions I would ask about the profile, the cheaper alternatives to try first, the conditions for custom code, and how to frame the conversation with the engineer.
1Start With the Evidence
'ConcurrentLinkedQueue showed up in a profile' can mean very different things. It might be 2 percent of CPU in a service that is mostly waiting on a database, or 30 percent of CPU in a pipeline that does almost nothing else. So the first questions are concrete. What kind of profile was it: CPU samples, allocation, or lock and wait time? What share of time is in the queue's methods, and on how many threads? And, most importantly, what does the service-level picture look like: are we missing a latency target such as p99 under 20 ms, or hitting a throughput ceiling, and how much would removing the queue's cost move that number?
If the queue accounts for 3 percent of CPU and the service is within its targets, even a perfect queue would not change anything customers or the business notice. Saying so, with the numbers, is the most useful outcome of the review, and it costs one meeting instead of months of maintenance.
2Understand Why the Queue Is Expensive
If the cost is real, the next step is to understand it, because the cause decides the fix. ConcurrentLinkedQueue is an unbounded linked queue: every offer allocates a node, and all producers update the same tail with compare-and-set, while consumers update the same head. With many producers on many cores, the tail's cache line moves between cores constantly and CAS retries add up. The allocation also creates garbage at the message rate. A profile showing time in offer under many producers points to contention; time in GC points to allocation; time in poll on an empty queue points to a busy-polling consumer, which is a design problem rather than a queue problem.
These have different remedies, and none of them requires a new algorithm.
3Try the Existing Options First
The JDK offers several queues with different trade-offs. ArrayBlockingQueue is bounded and array-based, so it does not allocate per element and gives back pressure. LinkedTransferQueue and LinkedBlockingQueue suit hand-off between threads with blocking consumers. Often the bigger win is not the queue at all but batching: a producer that offers one list of 100 items instead of 100 items does one hundredth of the shared updates, and a consumer that uses drainTo to take many items at once does the same on the other side.
Beyond the JDK, JCTools provides lock-free queues specialised by the number of producers and consumers, such as single-producer single-consumer and multi-producer single-consumer, many array-based, bounded and allocation-free, and built by people who specialise in exactly this. The LMAX Disruptor offers a ring buffer for pipelines. These libraries have years of production use on many architectures. Trying one is usually a day's work behind an interface, with a benchmark to compare.
| Option | What it addresses | Effort |
|---|---|---|
Batch offers, drainTo on consume | Fewer shared updates per item | Small code change |
ArrayBlockingQueue | No allocation per item; bounded | Swap the class |
| JCTools MPSC or SPSC queue | Contention and allocation | Add a dependency |
| LMAX Disruptor | High-rate pipelines | Redesign the stage |
| Custom lock-free queue | Only what the others cannot | Months, ongoing ownership |
4Conditions for Custom Code
Sometimes none of that fits, for instance a very specific ordering or memory-layout need, and custom code is justified. Then I would agree with conditions, stated up front so they are not a surprise later. First, a written correctness argument: the invariants, the linearization point of each operation, and how ABA and memory ordering are handled. Second, jcstress tests, the OpenJDK harness that runs tiny concurrent scenarios millions of times and flags forbidden results, run on both x86 and ARM, because ARM's weaker memory ordering exposes bugs x86 hides. Third, review by someone experienced in lock-free code, from inside or outside the team.
Fourth, a narrow scope: the queue lives behind an interface, is used only in the hot path that justified it, and can be switched back to a standard queue with a configuration flag, so a bug in production is a flag flip rather than an emergency release. Fifth, ownership: at least two people must understand and be able to change it, with the design notes in the repository. Code that only its author can maintain becomes a risk the day that person changes teams.
- Does the queue cost move a service target?YesNext questionNoThank, document, do not build
- Fixed by batching or another JDK queue?YesMake that changeNoNext question
- Fixed by JCTools or the Disruptor?YesAdopt the libraryNoNext question
- Custom code under conditionsproof, jcstress, expert review, flag fallback, two maintainers
4 steps in order. Play them, or step through them one at a time.
5Framing the Conversation
How we say this matters as much as what we decide. The engineer did useful work by profiling, so I would start there: the finding is valuable, and we want to turn it into the biggest improvement for the least risk. Asking them to quantify the service-level effect and to benchmark two existing options teaches the habit we want across the team, and often they will come back and say that batching solved it. If custom code does go ahead, the conditions are presented as what makes it safe to run in an important service, not as obstacles, and the engineer gets the credit for a well-engineered result rather than for a clever but fragile one.
6Follow-up questions
What if the engineer insists the custom queue is twice as fast in a microbenchmark?
A microbenchmark speedup is necessary but not sufficient. I would ask for the end-to-end effect under production-like load, and for the same benchmark against JCTools. Twice as fast on 3 percent of CPU is a 1.5 percent gain, which rarely justifies permanent ownership of tricky code.
Is ConcurrentLinkedQueue a bad choice in general?
No. It is a solid general-purpose non-blocking queue with good behaviour at moderate rates. Its costs, a node allocation per element and shared head and tail, matter only at very high rates with many threads, which is exactly when specialised queues exist.
How would you know later whether the decision was right?
By the same metrics that justified it: latency percentiles and CPU at the target load, tracked before and after, plus the number of incidents and the time spent on the queue code over the following months.
7Conclusion
In this answer, we evaluated a proposal to write a custom lock-free queue. We asked for the profile and its effect on service-level targets, understood why ConcurrentLinkedQueue costs what it does, and tried cheaper options first: batching, other JDK queues and proven libraries such as JCTools and the Disruptor. For custom code we set conditions: a correctness argument, jcstress tests on x86 and ARM, expert review, a narrow scope with a fallback to a standard queue, and at least two maintainers, framed as what makes the work safe rather than as obstacles.
Open with
First I'd thank them for profiling, and then ask for the evidence: what kind of profile, how much of CPU or latency is actually in the queue, and what that means for the service's targets.
Then, if they want more
If it's a few percent of CPU and we're meeting our latency goals, a perfect queue wouldn't change anything that matters, and saying that with numbers is a valid outcome. If it is significant, I'd want to know why.
ConcurrentLinkedQueue allocates a node per offer and all producers CAS the same tail, so the cost is usually contention or allocation, and that points to cheaper fixes: batching, so producers offer lists and consumers use drainTo; a bounded array-based queue; or a specialised library like JCTools, which has multi-producer single-consumer and single-producer queues that are allocation-free and well tested.
Those are a day's work behind an interface with a benchmark.
Only if none of that works would I agree to custom code, with conditions set up front: a written correctness argument covering linearization points, ABA and memory ordering; jcstress tests on both x86 and ARM; review by someone experienced in lock-free code; a narrow scope behind an interface with a config flag to fall back to a standard queue; and at least two people who can maintain it.
I'd frame those as what makes it safe in an important service, so the engineer still owns a good result.
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
- java.util.concurrent, package 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 "An engineer proposes writing a custom lock-free queue for an important service because...": https://scrivio-interview-library.netlify.app/java/atomics/643dc99536/