Skip to main content
App basics: HTTP, auth, dataLesson 8 of 11

How a Message Queue Decouples Services

The order service publishes one event. Shipping and email each read it at their own pace. Follow the event through producer → topic partition → consumer group → offset commit, then watch what happens when a consumer crashes, falls behind, or hits a message it can never process.

Messageorder 42 created
Committed offset—
Consumer lag—
Delivery—
Kafka cluster · topic ordersConsumer group shippingOrder serviceProducerPartition 0Leader broker 1Partition 1Leader broker 2Partition 2Leader broker 3Shipping 1Reads p0, p1Shipping 2Reads p2Email serviceGroup emailDLQ topicorders.dlqPublish
RequestResponse / reportAppears in a later step

What you will learn

  • Why publishing an event beats calling three services directly
  • Topics, partitions, keys, and where ordering holds
  • Consumer groups split partitions and commit offsets
  • A crash means redelivery, so consumers must be idempotent
  • Lag, poison messages and the dead-letter queue

Delivery guarantees​

GuaranteeHow you get itWhat a crash doesUse it for
At-most-onceCommit the offset before processingThe message can be lostData where a gap is fine, such as some sampled metrics
At-least-onceCommit the offset after processingThe message can be processed twiceThe usual default. Pair it with an idempotent consumer
Exactly-onceKafka idempotent producer plus transactions, consumers with isolation.level=read_committedRead-process-write inside Kafka happens onceStream processing from Kafka topic to Kafka topic. Side effects outside Kafka, such as an email or a payment call, still need an idempotency key

Try it yourself​

kafka-topics.sh --bootstrap-server localhost:9092 --create --topic orders \
--partitions 3 --replication-factor 3 # needs 3 brokers
kafka-topics.sh --bootstrap-server localhost:9092 --describe --topic orders # Leader, Replicas, Isr per partition
kafka-console-producer.sh --bootstrap-server localhost:9092 --topic orders \
--property parse.key=true --property key.separator=: # type: order-42:created
kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic orders \
--group shipping --from-beginning --property print.key=true --property print.partition=true
kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group shipping # CURRENT-OFFSET, LOG-END-OFFSET, LAG

Run two consumers with the same --group and they split the partitions. Run one with a different group and it gets every message again.

Check yourself

  1. A shipping consumer books a shipment and crashes before committing the offset. After the rebalance the same shipment is booked again. What is the right fix?
  2. The topic orders has 3 partitions and lag keeps growing. You scale the shipping group from 3 to 6 consumers. What happens?
  3. One message with a broken payload fails every time, and everything behind it in partition 2 waits. What should the consumer do?

Further reading: Kafka documentation · Kafka design: message delivery semantics · KEDA Apache Kafka scaler · Amazon SQS dead-letter queues · RabbitMQ dead letter exchanges · Autoscaling with HPA · Database basics