What it is
An event stream is an append-only, ordered log of records. Producers add records at the end; each record gets the next offset in its partition. Consumers read from any offset they choose and keep reading as new records arrive. Reading does not remove anything: records stay until a retention limit deletes them (or compaction replaces them). Apache Kafka is the best-known implementation; Redpanda, Amazon Kinesis Data Streams, Azure Event Hubs, Apache Pulsar and NATS JetStream offer the same model.
Why it exists
A message queue is a to-do list: once a job is done, it is deleted. That is right for work, but many systems need the events themselves: several services react to the same "order placed", a new service needs last month’s orders to build its database, a buggy consumer must reprocess yesterday after a fix, a cache or search index must be rebuilt from scratch. A log keeps the facts in order and lets every reader move through them at its own pace, so producers and consumers are decoupled not only in time but also in number and in history.
How it works
Appending. Each partition is a sequence of records with offsets 0, 1, 2… Records are immutable. On the broker the partition is stored as segment files; only the newest (active) segment is written to, older ones are closed. Appends are sequential writes, and reads of recent data are usually served from the operating system’s page cache, which is why logs sustain high throughput on ordinary disks.
Reading. A consumer asks for records starting at an offset and receives them in order. It records progress by committing an offset (in Kafka, to the internal __consumer_offsets topic, per group and partition). After a restart it resumes from the committed offset. Different consumers, or different consumer groups, have independent offsets: each sees every record.
Lag. Consumer lag is the log end offset minus the committed offset: how much has been written but not yet processed. Expressed in time (how old the oldest unprocessed record is), it tells you directly how stale the consumer’s view is.
Replay. Because records are kept, a consumer can start anywhere: a new group with auto.offset.reset=earliest starts at the oldest retained record, latest starts at the end, and tools such as kafka-consumer-groups --reset-offsets move an existing group to a timestamp or offset. Reprocessing is safe only if processing is idempotent.
Retention. Brokers delete whole closed segments once they are older than retention.ms (Kafka default: 7 days) or the partition exceeds retention.bytes (unlimited by default). Deletion ignores consumers: a consumer whose committed offset was deleted gets an out-of-range error and falls back to its reset policy, skipping what was lost. Tiered storage (KIP-405, production-ready since Kafka 3.9) can move old segments to object storage for long retention at lower cost.
Compaction. With cleanup.policy=compact, a background cleaner rewrites closed segments and keeps at least the latest record per key. Offsets do not change; they get gaps. A record with a null value (tombstone) deletes its key after delete.retention.ms. A compacted topic is a replayable table of current state, used for changelogs, configuration and Kafka’s own consumer offsets.
When to use it
- Several independent consumers need the same events (analytics, search, notifications, audit).
- You need replay: backfilling new services, rebuilding read models, reprocessing after bugs.
- Event sourcing or change data capture, where the history is the source of truth or feeds other stores.
- High-throughput pipelines where per-key order matters.
When not to
- Independent jobs that each need one worker, per-message retries, delays and priorities: a work queue does this more simply.
- Low-volume request/response between two services: an API call is simpler.
- Data that must be deleted on request (personal data) without a plan: immutable logs need compaction with tombstones, short retention or encryption-key deletion to honour erasure.
Trade-offs
| Choice | You gain | You pay |
|---|---|---|
| Log instead of queue | Replay, many independent readers, per-partition order | Consumers manage offsets; no per-message ack, delay or priority |
| Long retention | Longer replay window and outage tolerance | Storage, slower recovery of replicas (less with tiered storage) |
| Short retention | Cheap, small | Slow or offline consumers lose events |
| Compaction | Complete latest state per key, bounded size | Full history lost; tombstones and timing to understand |
| Auto-commit of offsets | Simple | Possible loss or duplicates around crashes |
Alternatives
- Work queues (RabbitMQ, SQS, Azure Service Bus queues): per-message acknowledgement, retries, delays and dead-lettering, no replay.
- Pub/sub with durable subscriptions (SNS + SQS, Google Pub/Sub): every subscriber gets each message, limited replay (Pub/Sub supports seek within its retention).
- Database tables plus polling or CDC: the table is the history; Debezium-style CDC turns it into a stream.
Failure modes
- Consumer slower than retention: silently skips data after an offset reset. Alert on lag in time against retention.
- Replay side effects: reprocessing re-sends emails or charges cards unless handlers are idempotent.
- Wrong reset policy:
lateston a new group skips everything already in the log;earlieston a huge topic floods a new consumer. - Disk full: retention by time alone with a traffic spike can fill broker disks; set size limits or alerts.
- Poison record: a record that crashes the consumer blocks progress on its partition until handled.
In production
Decide retention from the longest replay you need (backfills, outages, audits), not from defaults. Monitor per-partition lag in records and seconds. Commit offsets after processing, and make processing idempotent so replays are safe. Use schemas (a schema registry with compatibility rules) because old events will be read by new code. For state-like topics, use compaction and document tombstones. Keep an explicit runbook for offset resets.
Common misconceptions
- "Kafka is a queue." It is a log: reading does not remove, and consumers track their own position.
- "Consumers that have not read an event keep it alive." Retention deletes by time or size regardless.
- "Compaction keeps only one record per key." It keeps at least the latest; recent records in the active segment are not compacted yet.
- "Replay is free." Replay re-runs side effects; it needs idempotent consumers.
Interview angle
When you introduce Kafka in a design, say why a log rather than a queue: multiple consumers, replay, ordering per key. Mention retention sizing and lag monitoring, and how a new service backfills from the log. Explaining offsets, committed position and the effect of retention on a lagging consumer shows you understand the model rather than the product name.
Model assumptions
The simulation in this lesson is a simplified model, not a benchmark. The Orders service produces 20 events per second at random (Poisson). In the first step a queue deletes each message when Billing acknowledges it. From the second step the log is modelled as a topic that copies each event into one queue per consumer; that queue’s depth stands for the consumer’s lag. A real log stores each event once and keeps one offset per consumer. Billing has 2 slots at about 30 ms per event, Analytics 2 slots at about 45 ms, Search 6 slots at about 30 ms (lognormal, cv 0.3). Search starts with no processing slots until 1.5 s into its step, so the backlog built up until then stands for the history it replays. Segments, offsets, retention, the offset-out-of-range reset and compaction are scripted illustrations with example numbers, not simulated. Not modelled: partitions (see the next lessons), disk and page cache, replication. Numbers show behaviour and orders of magnitude only.