Publish once instead of calling everyone An order is placed. Shipping, email and points all need to know. If the order service calls each one directly, one slow or broken service makes the order fail, and every new service means changing the order code.
Instead, the order service publishes one event, "order 42 created", to a topic and returns. Each interested service reads the event when it is ready. Kafka is the example here. RabbitMQ and Amazon SQS solve the same problem with different models.
A topic is split into partitions, and the key picks one The topic orders has three partitions. Each partition is an append-only log, and every message in it gets the next number, its offset.
The producer sends the message with key order-42. The default partitioner hashes the key, so every event for order 42 lands in the same partition, partition 1 here. Order is guaranteed only within one partition, not across the whole topic.
Key idea Pick the key by what must stay in order. Same order id → same partition → events for that order are read in the order they were written.
Replication and acks decide when a write is safe Each partition has a leader on one broker and followers on other brokers that copy it. With replication factor 3, the message exists on three brokers once the followers catch up.
With acks=all (the default since Kafka 3.0), the leader answers only after all in-sync replicas have the message. min.insync.replicas=2 means a write fails rather than being kept on just one broker. With acks=1 the leader answers alone, and a leader crash can lose the message.
Key idea Replication factor 3, acks=all and min.insync.replicas=2 is the common setup for data you cannot lose. It survives one broker failure without losing writes.
A consumer group splits the partitions among its members Two shipping instances join the consumer group shipping. Kafka gives each partition to exactly one member of the group: Shipping 1 reads partitions 0 and 1, Shipping 2 reads partition 2.
So the partition count caps parallelism. A third shipping instance could still help, but a fourth would sit idle with no partition to read.
Process, then commit the offset Shipping 1 reads offset 1041, books the shipment, then commits "next offset 1042 for partition 1". Kafka stores committed offsets per group in an internal topic. The message itself stays in the log until retention removes it.
Where you commit decides the guarantee. Commit after processing and a crash can repeat work. Commit before processing and a crash can skip work.
A crash before commit means the message comes again Shipping 1 reads offset 1043, books the shipment, and crashes before it commits. Its heartbeats stop, the group rebalances, and Shipping 2 now owns partitions 0, 1 and 2.
Shipping 2 starts partition 1 from the last committed offset, 1043, and books the same shipment a second time. This is at-least-once delivery. Nothing is lost, but duplicates happen.
Watch out Make the consumer idempotent: record processed order ids, or use an upsert or unique key, so a second copy of the same message changes nothing.
Another group reads the same events on its own The email service is in a different group, email. It reads every partition of the same topic with its own committed offsets. Shipping and email never slow each other down, and a new service can join later and replay from the start of retention.
That is publish/subscribe. A classic queue such as SQS or a RabbitMQ queue works differently: consumers compete for messages, and an acknowledged message is deleted. Kafka gives you both: members of one group compete, and separate groups each get every message.
Consumer lag grows when producers outrun consumers A sale triples the order rate. The shipping consumers cannot keep up. Lag is the gap between the newest offset in a partition and the group's committed offset, and it keeps growing.
Lag means events are late, not lost, until retention deletes messages nobody has read yet. Add consumers up to the partition count, make processing faster, or add partitions (which changes which partition a key maps to).
Watch out Alert on lag, or on how long the oldest unread message has waited, not on CPU. Autoscalers such as KEDA can scale consumers by lag.
A poison message blocks a partition until it goes to a DLQ One message has a broken payload. Shipping 2 throws an error, does not commit, reads it again, and fails again. Every message behind it in partition 2 waits.
The fix is a dead-letter queue. After a few retries, the consumer writes the bad message to orders.dlq with the error, commits, and moves on. Someone inspects the DLQ and replays the message once it is fixed. SQS has this built in (a redrive policy with maxReceiveCount), RabbitMQ has dead-letter exchanges, and with Kafka the consumer code or framework does it.
Durable log, shared work, idempotent consumers The producer writes once to a partition chosen by key, and replication with acks=all keeps it safe. Each consumer group gets every message. Inside a group, partitions are split among members, and committed offsets record progress.
Crashes cause redelivery, so assume at-least-once and make consumers idempotent. Watch lag, and move messages that can never succeed to a dead-letter queue instead of retrying them forever.
Summary When something looks wrong, check the group first: kafka-consumer-groups.sh --describe shows each partition's owner, committed offset and LAG.