Message Queues and Event-Driven Architecture
Message queues decouple services: a producer publishes a message and one or more consumers process it later. Kafka, RabbitMQ, SQS and Redis Streams are common. Interviewers ask about delivery guarantees, ordering, retries and the outbox pattern.
What you will learn
- Compare a queue (work distribution) with a log or topic (event stream)
- Explain at-most-once, at-least-once and exactly-once
- Use dead-letter queues and idempotent consumers
- Explain the transactional outbox
Key concepts
Queue vs topic
In a queue (RabbitMQ, SQS) each message goes to one consumer, which spreads work. In a log or topic (Kafka) each consumer group reads every message at its own offset, and messages are kept for replay.
At-least-once
The usual guarantee: a message is redelivered until acknowledged, so consumers may see duplicates. Make handlers idempotent (store processed message ids).
Ordering
Kafka keeps order only within a partition. Use a key (orderId) so all events for one order land in the same partition.
Transactional outbox
Write the business change and an outbox row in the same database transaction; a relay publishes outbox rows to the broker. This avoids "saved to the database but the event was lost" (the dual-write problem).
Step by step
Order placed, event-driven
- Order service commits the order + outbox row "OrderPlaced"
- Relay publishes OrderPlaced to Kafka (key = orderId)
- Payment, inventory, email and analytics consumers each react independently
- A failing consumer retries with backoff, then sends the message to a dead-letter topic
Examples
An idempotent consumer
consumer.run({
eachMessage: async ({ message }) => {
const event = JSON.parse(message.value.toString())
await db.$transaction(async (tx) => {
const seen = await tx.processedEvent.findUnique({ where: { id: event.id } })
if (seen) return // duplicate delivery
await tx.inventory.update({ where: { sku: event.sku }, data: { reserved: { increment: event.qty } } })
await tx.processedEvent.create({ data: { id: event.id } })
})
},
})Practise it
Try these without looking back. If you can explain each one out loud, you are ready for this question.
- Explain why publishing to Kafka right after db.commit() can lose events, and fix it with an outbox.
- Choose a partition key for chat messages so each conversation stays in order.
Interview questions
What interviewers ask next on this topic. Answer out loud first, then open the model answer.
Is exactly-once delivery possible?
End to end across systems it is practically "at-least-once delivery + idempotent processing". Kafka offers exactly-once semantics inside Kafka (idempotent producers and transactions), but side effects elsewhere still need idempotency.
Kafka or RabbitMQ?
Kafka for high-throughput event streams, replay, many independent consumers and ordering per key. RabbitMQ for task queues with flexible routing, per-message acknowledgements and priorities. SQS when you want a managed queue with no operations.
Summary
Queues spread work and topics broadcast events; expect duplicates and make consumers idempotent, key messages for ordering, use dead-letter queues, and publish reliably with an outbox.
Stuck on this one? Talk to Sumit.
Keys: F focus mode · ← → previous / next topic · Esc leave focus