$ kafka-console-consumer --topic orders --group billing

Messaging and Streaming Platforms

A messaging or streaming platform sits between the services that produce data and the services that use it. Producers write messages to the platform; consumers read them whenever they are ready. Neither side needs to know the other exists, be online at the same time, or run at the same speed. That decoupling is why almost every real-time data pipeline, from CDC to clickstream analytics, starts with one.

There are two families, and the difference matters:

  • Message queues (RabbitMQ, Amazon SQS, ActiveMQ): a message is a task. It is delivered to one worker, acknowledged, and then deleted.
  • Event logs / streaming platforms (Apache Kafka, Redpanda, Apache Pulsar, Amazon Kinesis): a message is a fact. It is appended to a durable, ordered log and kept for a retention period. Any number of consumers read it independently, and can re-read it later.

How a Kafka-style log works

$ kafka-topics --create --topic orders --partitions 3
Producers, partitions, offsets and consumer groups
PRODUCERorders-svc TOPIC orders P0P1P2 012 012 0123 samowensam aliceryanalice mayanoramayanora 3sam 3alice key alice → P1key sam → P0 GROUP billing C1reads P0, P1 C2reads P2 → C1→ C1→ C2 committed 3committed 2committed 4 reads P0 → C3 C3 · joinedreads P1 REBALANCE GROUP analytics own offsets: P0=1 P1=0 P2=2 · reads the same messages, independently
  1. A topic is split into partitions. Each partition is an append-only log, and each message gets a position in it: the offset.
  2. The producer picks a partition by hashing the key. All orders for alice go to P1, so they stay in order. New messages are always appended at the end.
  3. A consumer group splits the partitions among its members. Each partition is read by exactly one consumer in the group.
  4. Consumers commit offsets: "next, read offset 3". After a crash, a consumer resumes from its last commit, so messages may be re-read but not skipped.
  5. A new consumer C3 joins. The group rebalances and P1 moves from C1 to C3. More consumers than partitions? The extras sit idle.
  6. Reading doesn't delete anything. A second group keeps its own offsets over the same log, until retention (say 7 days) removes old segments.

Key ideas

  • Partitions are the unit of parallelism and ordering. Order is guaranteed only within a partition. Pick a key (order ID, user ID) so related events share a partition, and pick enough partitions for your peak consumer count.
  • Replication: each partition has a leader broker and follower replicas. With acks=all and min.insync.replicas=2, a write is only confirmed once it is on at least two brokers.
  • Delivery semantics: at-most-once, at-least-once or exactly-once, decided by when the consumer commits its offset relative to its work. See the next section.
  • Retention and compaction: delete messages after a time or size limit, or compact the topic to keep only the latest value per key (useful for CDC and changelogs).
  • Schemas: put a schema registry in front of producers so a breaking change can’t reach consumers. See Avro.

Delivery semantics

A consumer does two things with every message: it processes it (writes a row, calls an API) and it commits its offset (records “I’m done up to here”). These are two separate actions, and a crash can land between them. The order you do them in decides what a crash costs you.

$ consumer --group billing --kill-after-poll
Same crash, three outcomes
orders · P0 offset / message 4041424344 #1040#1041#1042#1043#1044 consumerbilling paymentssink table AT-MOST-ONCEcommit, then work AT-LEAST-ONCEwork, then commit EXACTLY-ONCEatomic or idempotent poll 42#1042 commit 43offset saved ✗ crashbefore write restart from 43#1042 LOST poll 42#1042 write #1042row in sink ✗ crashbefore commit restart from 42#1042 written twice poll 42#1042 one transactionwrite #1042 + offset 43 crash: both undoneretry → #1042 once or an idempotent sink: MERGE ON event_id, a replay overwrites the same row
  1. The consumer reads offset 42 (order #1042) and must do two things: write a payment row and commit offset 43. A crash can hit between them.
  2. At-most-once: commit first, then process. The crash comes after the commit but before the write.
  3. On restart the group resumes at 43. Order #1042 is never processed. No duplicates, but data loss.
  4. At-least-once: process first, then commit. The write succeeds, then the crash hits before the commit.
  5. On restart the group resumes at the last commit, 42, and writes #1042 again. Nothing is lost, but you get a duplicate.
  6. Exactly-once: the write and the offset commit happen in one transaction. A crash rolls back both, so the retry processes #1042 exactly once.
  7. When the sink can't join that transaction, make the write idempotent: upsert keyed by the event id, so a replay changes nothing.
Semantics Commit order On crash Use when
At-most-once Commit, then process Messages lost Losing a few is fine: metrics sampling, some logs
At-least-once Process, then commit Messages repeated The default; pair it with idempotent consumers
Exactly-once Process and commit atomically, or dedupe by key Neither Money, inventory, counts that must match

How exactly-once is done in practice:

  • Inside Kafka (read from a topic, write to a topic): use the idempotent producer (enable.idempotence=true, the default since Kafka 3.0), which stops broker-side duplicates from producer retries, plus transactions (transactional.id). The producer writes the output and the consumer offsets (sendOffsetsToTransaction) in one transaction, and downstream consumers read with isolation.level=read_committed. Kafka Streams turns this on with processing.guarantee=exactly_once_v2.
  • Stream processors: Flink and Spark Structured Streaming checkpoint offsets and state together, and use transactional or idempotent sinks so outputs line up with the checkpoint.
  • External systems (a database, a warehouse, an API): Kafka’s transaction can’t reach them. Either store the offset in the same database transaction as the result, or make the write idempotent (MERGE/upsert on a unique event id, or a dedup table). In practice “exactly-once” here means at-least-once delivery plus idempotent processing: the message may arrive twice, but the effect happens once.

Queue vs log

Message queue Event log
After a consumer reads Message is acked and removed Message stays until retention
Multiple consumers Compete for messages Each group gets every message
Replay history No Yes, reset the offset
Ordering Per queue, weak with many workers Per partition, strong
Per-message features Routing, priorities, delays, dead-letter queues Few; it’s a log
Good for Background jobs, commands, RPC-style work Event streams, CDC, analytics, event sourcing

The line is blurring: Kafka added queue-style share groups (KIP-932, production-ready since Kafka 4.2), and RabbitMQ Streams and NATS JetStream add log-style retention and replay.

Tools in 2026

Platform Model Notes
Apache Kafka Partitioned log The standard. Kafka 4.0 (2025) removed ZooKeeper; clusters run on KRaft. Tiered storage moves old segments to object storage.
Redpanda Kafka API-compatible log Single C++ binary, no JVM. Drop-in for Kafka clients.
Apache Pulsar Log + queue Brokers are stateless; storage lives in BookKeeper. Multi-tenancy and geo-replication built in.
RabbitMQ Queue (plus Streams) AMQP, flexible routing with exchanges. Quorum queues for durability.
NATS Pub/sub + JetStream Very light, low latency. JetStream adds persistence and replay.
Kinesis, Pub/Sub, Event Hubs Managed services Cloud-native, no brokers to run. Event Hubs exposes a Kafka-compatible endpoint.

Common mistakes

  • Too few partitions. You can’t have more active consumers in a group than partitions. Adding partitions later changes which partition a key maps to.
  • No key, then expecting order. Without a key, messages are spread across partitions and per-entity order is lost.
  • Auto-committing offsets before work is done. A crash then loses messages. Commit after processing.
  • Huge messages. Kafka defaults to about 1 MB per message. Put large files in object storage and send a pointer.
  • Using Kafka as a database. It’s a log. Materialize state into a database or a stream processor’s state store.

Learning Resources

Books

Articles and docs

Videos

See also: CDC, Batch and Stream Processing