Scrivio Interview library
DesignAdvanced7 min read · updated 12 November 2026

Two components of a system communicate as producer and consumer. One proposal uses an in-process bounded queue, another uses Kafka. How do you decide?

An in-process bounded queue is fast and simple, but its contents vanish when the process crashes or restarts, and it only connects threads inside one JVM. Kafka adds durability, replay, many independent consumer groups and scaling across processes, at the price of running a cluster, more latency and more failure modes to handle. We decide from the failure requirements first: if work must survive restarts or be processed effectively once, we need a durable log or a database outbox; then we weigh ordering, throughput and the team's experience.

Background to this question

Producer-consumer is a pattern, not a technology, and the same diagram of two boxes and an arrow can be built with a few lines of java.util.concurrent or with a distributed log. Teams argue about this choice often, and both sides are usually right about something. A senior engineer is expected to turn the argument into questions about requirements. In this answer, we will recap what each option is, compare what happens on a crash, look at durability, replay and fan-out, explain the outbox for the case where work must not be lost, compare ordering, and end with a decision process.

1A Quick Recap of the Two Options

The in-process option is a BlockingQueue, for example an ArrayBlockingQueue with a capacity of 10,000, shared by producer and consumer threads in the same JVM. Handing over an item costs well under a microsecond, there is nothing to deploy, and the bounded capacity gives backpressure: a fast producer blocks when the queue is full.

Kafka is a distributed, replicated log. Producers append records to a topic, which is split into partitions stored on several brokers. Consumers read records by offset, their position in a partition, and commit the offsets they have processed. Records stay in the log for a retention period, such as seven days, whether or not anybody has read them. The two options look similar in a diagram but behave very differently when something fails.

2What Happens When the Process Crashes

This is the question that decides most cases. With an in-process queue, everything in the queue lives in the heap. If the JVM crashes, is killed by the orchestrator for using too much memory, or is simply redeployed, every queued item is gone, and so is any item a consumer was halfway through. With 10,000 items in the queue during a busy period, a routine deployment loses 10,000 pieces of work unless shutdown drains the queue carefully, and a crash loses them regardless.

With Kafka, the producer's send is acknowledged only after the record is written, and with acks=all and a replication factor of 3 it is stored on several brokers. If the consumer crashes, it restarts and resumes from its last committed offset, so the unprocessed records are simply read again. The price is that records may be processed twice, once before the crash and once after, so consumers must be idempotent: processing the same record twice must have the same effect as once.

Step throughA consumer crash with each option
ProducerQueue or topicConsumer
in-process: put(item) x 10,000
JVM crashes or redeploys
in-process: all 10,000 items lost with the heap
Kafka: send, acknowledged after replication
consumer crashes before committing offset 5,200
restart: read from committed offset 5,000
records 5,000 onward again; idempotent handling

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

The queue forgets on a crash; the log remembers and replays from the committed offset.

3Durability, Replay and Independent Consumers

Kafka gives three capabilities the queue cannot. Durability is the one we just saw. Replay means that because records stay in the log, a new consumer or a fixed consumer can reread last Tuesday's records, for example to rebuild a search index after a bug. Independent consumers means several consumer groups can read the same topic, each at its own pace: billing and analytics both receive every order event, and a slow analytics job does not hold back billing.

Kafka also scales across processes. A topic with 12 partitions can be consumed by up to 12 instances in one group, each owning some partitions, so the producer and consumer can be different services, deployed and scaled separately. An in-process queue ties producer and consumer to one JVM.

These capabilities cost something. A Kafka cluster must be run, monitored, upgraded and secured, or paid for as a managed service. Each hop adds latency, typically a few milliseconds end to end rather than microseconds. And the application must handle new failure modes: broker unavailability, rebalances that move partitions between consumers, duplicate delivery and poison messages that fail on every retry.

4When Work Must Not Be Lost: The Outbox

Sometimes the producer writes to a database and must also publish the work, for example saving an order and asking for an email to be sent. Writing to the database and then sending to Kafka are two separate actions; a crash between them loses the message, and sending first and failing to commit sends a message about an order that does not exist. The transactional outbox solves this: the producer inserts the message into an outbox table in the same database transaction as the order, and a separate relay reads the outbox and publishes to Kafka, or a consumer processes the outbox rows directly.

Java
@Transactional
public void placeOrder(Order order) {
orders.save(order);
outbox.save(new OutboxMessage("order-placed", order.id(), toJson(order))); // same transaction
}
// A relay (or a CDC tool such as Debezium) publishes outbox rows to Kafka,
// then marks them sent; a crash only causes a resend, never a loss.

Notice that for a single service whose work must survive restarts, the outbox table alone can be the durable queue, without Kafka at all: consumers poll rows with SELECT ... FOR UPDATE SKIP LOCKED in PostgreSQL or MySQL 8, process them and mark them done. That is often the right middle ground when durability is required but fan-out and replay are not. Processing exactly once in effect, rather than literally, always comes from at-least-once delivery plus idempotent processing, whichever store we use.

5Ordering Guarantees

An in-process queue is FIFO, so with one consumer items are processed in exactly the order they were produced. With several consumers, that order is lost as soon as two items are processed in parallel. Kafka guarantees order only within a partition. Records with the same key, such as a customer id, go to the same partition and are processed in order by one consumer; records with different keys may be processed in any order relative to each other. For most business flows, per-entity ordering is what matters, and keying by that entity is enough. If a flow truly needs one global order, both options force a single consumer, and then the throughput limit is that consumer's speed.

6Making the Decision

The decision runs in a fixed order, because the failure requirements rule options out and the rest only rank what remains.

Step throughChoosing between an in-process queue and Kafka
  1. May queued work be lost on a crash or redeploy?
    YesIn-process bounded queue is a candidateNoNeed durable storage
  2. Do several independent consumers or replay matter?
    YesKafkaNoOutbox table may be enough
  3. Must the publish match a database write?
    YesAdd a transactional outboxNoPublish directly
  4. Check ordering, throughput and team experienceper-key order, rates, operational skills

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

Failure requirements first; performance and familiarity only rank what is left.

After failure requirements come throughput and latency: millions of small hand-offs per second inside one process favour the queue, while thousands per second across services are routine for Kafka. Last comes the team. A team that already runs Kafka well pays little extra for one more topic; a team that has never operated it is taking on a new platform. As an example, buffering log lines before batching them to a collector, where losing a few seconds on a crash is acceptable, is a perfect in-process queue. Order events that billing, shipping and analytics all consume, and that must survive deployments, belong in Kafka with an outbox on the producer side.

7Follow-up questions

Can we make an in-process queue durable?

We can persist items to disk or a database before acknowledging them, but then we are building a small, less tested message store. Chronicle Queue and similar libraries do this well for single-host use, but for cross-process durability an existing broker or an outbox table is usually the better engineering choice.

What about RabbitMQ or a cloud queue?

They are durable queues rather than logs: messages are deleted once acknowledged, so there is no replay, and fan-out requires an exchange or topic per subscriber. They fit work distribution with per-message acknowledgement well. The same first question applies: do we need durability, replay, fan-out?

How does backpressure work with Kafka?

The log absorbs bursts, so the producer is not slowed; instead, consumer lag grows. Consumers process at their own pace, and we monitor lag and scale consumers up to the number of partitions. Within a consumer, a bounded in-process queue still has its place when handing records to worker threads.

8Conclusion

In this answer, we compared an in-process bounded queue with Kafka for a producer and consumer. The queue is fast and simple but loses its contents on a crash and works only inside one process. Kafka adds durability, replay, independent consumer groups and cross-process scaling, at the cost of operations, latency and new failure modes. When work must survive restarts or be processed effectively once, a durable log or database outbox is required; ordering is global for a single queue consumer and per partition in Kafka; and the decision starts from failure requirements before throughput and team experience.

What to say in the interviewAbout 80 seconds aloud

Open with

I would start with failure requirements, because they rule options out. An in-process bounded queue is fast and simple, with sub-microsecond hand-offs and natural backpressure, but everything in it lives in the heap, so a crash or even a deployment loses whatever is queued, and it only works within one JVM.

Then, if they want more

Kafka gives durability, since records are replicated and consumers resume from committed offsets, plus replay, several independent consumer groups reading the same topic, and scaling producer and consumer as separate services. The costs are running a cluster, a few milliseconds of latency, rebalances and duplicate deliveries, so consumers must be idempotent.

So my first question is can we lose queued work on a crash. If yes, the in-process queue is a good fit. If no, we need a durable log or a database outbox.

If the producer also writes to a database, I would use a transactional outbox so the message and the data commit together; and if there is only one consumer service and no replay or fan-out, the outbox table polled with SKIP LOCKED might be enough without Kafka.

Then ordering: a single queue consumer gives global order, Kafka gives order per partition, so I key by entity. Finally throughput and how experienced the team is with running Kafka.

About this answer

Last updated
12 November 2026
Versions it names
MySQL 8
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. 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 "Two components of a system communicate as producer and consumer. One proposal uses an...": https://scrivio-interview-library.netlify.app/java/concurrency-patterns/ca5fbe75aa/