When would you choose Kafka and when RabbitMQ for messaging between services?
What the interviewer wants to hear
- Kafka is a durable, partitioned log that consumers read at their own offset
- RabbitMQ is a broker that routes messages to queues and deletes them after ack
- Kafka suits event streaming, high throughput, replay and many consumer groups
- RabbitMQ suits task queues, flexible routing, per-message acks and priorities
- Both give at-least-once delivery, so consumers must be idempotent
Model answer
They model messaging differently. Kafka is a distributed append-only log split into partitions. Messages stay for a retention period, and each consumer group tracks its own offset, so many independent services can read the same events and replay history. Ordering is kept per partition, and throughput is very high. I pick Kafka for event streaming, like order events consumed by billing, analytics and search, or for change data capture. RabbitMQ is a traditional broker: producers publish to exchanges, which route messages into queues, and a message is removed once a consumer acks it. It offers flexible routing, per-message acknowledgement, delayed retries and priorities, which makes it great for work queues like sending emails or resizing images. Both are usually at-least-once, so consumers must be idempotent. For simple needs, a managed queue like SQS can be enough.
Can you answer it under time pressure?
Take a timed medium round and get scored on every answer.
More medium Backend Developer questions
- An API endpoint is slow because it runs a heavy database query. How would you add caching, and how do you keep the cache correct?
- You need to rename a column in a large production table that the live app uses. How do you do it without downtime?
- How do you change a public API without breaking existing clients, such as old mobile app versions?
- Users say one API endpoint has become slow in production. How do you find and fix the cause?
- Sending emails and generating PDF invoices makes your checkout API slow. How would you move this work to the background?
- How would you add rate limiting to a public API that runs on many servers?
- You are asked to review the security of a REST API before launch. What would you check?
- Your service receives webhooks from a payment provider. How do you make handling them secure and reliable?
- How would you design an API that lets users upload large files, such as videos of several gigabytes?