Skip to content
Tech Interview Prep
Technical interview guide

Message Queues & Async Processing

Decoupling a slow or unreliable step from the request path by handing it to a queue and processing it separately.

Minutes to read
4
Practice MCQs
25
Interview Q&A
25
Edition
v7
Editorial status
Reviewed

Scope: Vendor-neutral distributed-systems principles; current AWS architecture guidance.

Overview

Curated: · Written: · Reviewed:

Message Queues & Async Processing

In one line

Hand a slow or unreliable step to a queue so the request returns before the work finishes. Reach for it to absorb bursts, isolate failures and smooth load. The price: the outcome is known later, and messages can arrive more than once, so the consumer must be idempotent.

Key definitions

  • At-least-once: the broker redelivers until acknowledged, so duplicates are normal. Consumers must tolerate them.
  • Idempotent consumer: processing a message twice leaves the same end state. Built from a stable natural key and a conditional write.
  • Visibility timeout (SQS): hides an in-flight message. If it expires before the ack, another worker receives the message.
  • Partition key (Kafka): the same key always maps to the same partition, which is the only place order is guaranteed.
  • Dead-letter queue (DLQ): holds messages that exhausted their retries. It needs a depth alarm and a replay path.
  • Outbox: the business row and an event row commit in one transaction. A relay publishes the event afterwards.

Formulas and complexity

Sizing with Little's Law, where L is handlers in flight, lambda the arrival rate and W the handler time:

L=λW=1,200/s×0.040 s=48L = \lambda W = 1{,}200/\text{s} \times 0.040\,\text{s} = 48

Recovery time after an outage, with backlog B, service capacity mu and arrival rate lambda, where mu must exceed lambda:

Tdrain=Bμ−λ=72,0001,500−1,200=240 sT_{drain} = \frac{B}{\mu - \lambda} = \frac{72{,}000}{1{,}500 - 1{,}200} = 240\,\text{s}

Kafka parallelism within one group:

active consumers=min⁡(members, partitions)\text{active consumers} = \min(\text{members},\ \text{partitions})

How it works

  1. The producer appends the message, and the broker stores it durably before acknowledging.
  2. A consumer receives it. On SQS that leases the message for the visibility timeout. On Kafka it reads from a position.
  3. The handler runs. Success is an ack, or an offset commit.
  4. On failure there is no ack, so the message returns after the timeout. After the retry limit it moves to the DLQ.
retries exhausted Commit order row + outbox row Relay reads unsent outbox rows Publish to broker Mark row sent Consumer: idempotent write Ack or commit offset DLQ + depth alarm

When to use and when not to

SituationQueueKeep synchronous
Caller must return the resultNeeds a status channel to report laterYes
Bursts above steady capacityYes, absorbs themNo
Downstream slow or flakyYes, with backoffOnly if it is fast and reliable
Strict global orderPoor: serialises everythingPossible but slow
Event must match a DB commitOutboxNot needed
Modest rate, already on PostgresDB table with SKIP LOCKEDPossible

Traps interviewers probe

  1. Key built from a clock or random ID. Every retry looks new and the dedupe check never fires. Fix: use a stable producer identifier, such as the order ID plus the event type, and a unique index on it.
  2. Assuming exactly-once covers the database write. Kafka transactions cover only Kafka. An offset commit and a Postgres INSERT are not atomic, so a crash between them reprocesses the record. Fix: record the processed key in the same transaction as the write.
  3. Publish-then-commit, or commit-then-publish. Either order loses or invents events on a crash. Fix: the outbox, with the relay publishing at least once.
  4. Visibility timeout shorter than the handler, or non-retryable errors retried. The first gives concurrent duplicate processing. The second wastes capacity on errors that fail identically forever. Fix: extend the timeout explicitly for long work, and classify errors so validation failures go straight to the DLQ.
  5. Adding consumers beyond the partition count. Surplus members sit idle. Fix: raise partitions, or make the handler faster.

Interview tips

Open with the guarantee: "Delivery is at-least-once, so the handler is idempotent, keyed on a stable ID, with the dedupe write in the same transaction as the business write."

Expect these follow-ups:

  • Ordering: per key, not global. Ordered partitions or FIFO groups serialise the key, so parallelism is capped at the number of partitions or groups.
  • Poison messages: bounded retries, then the DLQ, then inspect and replay after the fix.
  • Backlog: compare arrival rate with service rate, check drain time, and bound the queue so failure is a decision, not an accident.
  • Replay: a log-based system such as Kafka can re-read history. A queue that deletes on ack cannot.
  • Fan-out: each subscriber gets its own queue, so one slow consumer does not block the others, and each needs its own idempotency.
  • Monitoring: queue depth, oldest message age, consumer lag, DLQ depth and retry rate.

30-second outline: the guarantee, then the mechanism (outbox, then idempotent consumer, then DLQ), then the trade (added latency, duplicates handled, ordering per key only).

Feedback on this overview

Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.