Apache Kafka: how it works, when to use it, and how it compares
A practical guide to Kafka's architecture (topics, partitions, consumer groups, KRaft), when it earns its complexity, how it stacks up against Pulsar, Redpanda, RabbitMQ, Kinesis and SQS, and the sharp edges most tutorials skip.
Srijan Samridh·· 11 min read
Key takeaways
Kafka is a distributed commit log: append-only partitions, not a traditional message queue
Use it when many consumers need the same stream, replay matters, or throughput is high and durable
Pulsar, Redpanda, Kinesis, RabbitMQ and SQS solve overlapping problems with different trade-offs
Offsets, consumer groups and the ISR are the mental model; ZooKeeper is no longer required on modern clusters
Kafka shows up in every backend diagram the moment traffic, events or "we might need to replay that" enter the conversation. Plenty of teams adopt it because it sounds correct. Fewer can explain what they actually bought: a distributed, append-only commit log that many independent consumers can read at their own pace.
This is the mental model I wish I had earlier: how Kafka works, when it earns the operational cost, how it compares to Pulsar, Redpanda, RabbitMQ, Kinesis and SQS, and a handful of details that rarely make the first tutorial.
Before the vocabulary, a working sketch of the path every record takes. Produce keyed messages, watch them land on partitions hosted by brokers, then see two consumer groups pull independently with their own offsets. Pause, step, or reset — same rules as a real cluster, without the cluster.
Interactive Kafka path
Producer → partitions on brokers → independent consumer groups
Producer
user-42user-7order-9user-42
Topic · orders
Broker 0
Partition 00 rec
empty
Broker 1
Partition 10 rec
empty
Broker 2
Partition 20 rec
empty
Consumer groups · own offsets
Group “billing” · 2 consumers
C0owns P0, P2P0@—P2@—
C1owns P1P1@—
Group “search” · 1 consumer
C0owns P0, P1, P2P0@—P1@—P2@—
Press play — producers append keyed records into partitions.
Teaching simulation — not a real Kafka cluster. Offsets, keys and partition assignment follow the same rules.
At the centre of Kafka is a topic: an ordered stream of records. A topic is split into partitions. Each partition is an append-only log on disk. Producers write to partitions; consumers read by offset.
That one sentence does most of the work:
Order is per partition, not per topic. If you need strict ordering for a user or an account, put those events on the same partition (usually with a partition key).
Data is not deleted when a consumer reads it. Retention (time or size) or log compaction decides when old records go away. Another consumer group can read the same partition from the beginning tomorrow.
Throughput scales with partitions. More partitions means more parallelism for producers and consumers, and more open files / metadata for the brokers. Partition count is a capacity and operational decision, not a free knob.
Drawing diagram…
Two consumer groups reading the same topic do not steal work from each other. That is the feature people reach for Kafka to get, and the reason a simple queue is often the wrong tool.
A broker is a Kafka server. Several brokers form a cluster. Each partition has one leader broker that handles reads and writes, and zero or more followers that replicate the log. Clients talk to the leader; followers exist so a broker dying does not lose the partition.
Drawing diagram…
The set of in-sync replicas (ISR) is the followers that have caught up with the leader. acks=all (or -1) means the leader waits for the ISR before acknowledging a produce. That is the durability knob most production producers should start from.
Producers batch records, optionally compress them, and send them to the leader of each partition. Useful settings in practice:
Partition key: hash of the key picks the partition. Same key → same partition → order for that key.
Idempotent producer (enable.idempotence=true): brokers dedupe retries so a network blip does not create duplicate records with the same producer identity. Turn this on unless you have a reason not to.
Transactions: let you write to several partitions (and mark consumer offsets) atomically. Heavier; use when "exactly-once between Kafka and Kafka" is a real requirement, not a slogan.
A consumer group is a set of consumers that jointly read a topic. Kafka assigns each partition to at most one consumer in the group. Add consumers, get more parallelism, up to the number of partitions. Add more consumers than partitions and the extras sit idle.
Each group stores its offsets — how far it has read in each partition. Commit offsets when your side effects are safely done (or use transactions). Commit too early and a crash can skip work. Commit too late and a crash reprocesses work — which is fine only if handlers are idempotent.
Older clusters used ZooKeeper for controller election and metadata. Modern Kafka uses KRaft: metadata is itself a Raft-replicated log inside Kafka. New clusters should be KRaft; ZooKeeper mode is legacy. If a diagram still puts ZK in the middle, treat it as historical unless you know the cluster is old.
Kafka is designed to use disk sequentially. That is why it can hold terabytes and still serve consumers quickly.
Time/size retention: drop segments older than N days or when the partition exceeds a size.
Log compaction: for a keyed topic, keep the latest value per key (changelog style). Compaction is how Kafka backs compacted topics used like a replicated last-write-wins map — the foundation of Kafka Streams state and many CDC patterns.
Leader appends to its local log and replicates to followers.
When the produce ack policy is satisfied, the producer gets an offset.
Consumers in each group fetch from their assigned partitions starting at their committed offsets (or earliest / latest on first join).
The application processes records, then commits offsets.
There is no central "dispatch" pushing work to consumers. Consumers pull. That pull model is why slow consumers do not overwhelm brokers the way push queues can, and why lag (how far behind a group is) is the metric you watch.
One producer, one consumer, short-lived jobs → SQS, RabbitMQ, or a DB-backed queue.
You need request/reply, complex routing, or message priorities as first-class features → RabbitMQ (or NATS for simpler topologies).
The team cannot afford broker ops, partition planning, consumer lag monitoring and schema discipline → a managed queue or even "write to Postgres and poll" until the problem is real.
Latency must be sub-millisecond fan-out for tiny messages and you do not need long retention → look at NATS / Redis Streams carefully before paying Kafka's cost.
RabbitMQ is a broker that routes messages; Kafka is a log that stores them. In Rabbit, once a competing consumer acks, the message is gone for others. In Kafka, the same bytes remain for every group until retention says otherwise. Use Rabbit for task queues and clever routing. Use Kafka when the stream itself is a durable product.
Pulsar separates compute (brokers) from storage (BookKeeper) and has first-class multi-tenancy and geo-replication stories. Kafka keeps storage on the broker disk (tiered storage exists now, but the classic mental model is local log segments). Pulsar can feel cleaner for huge multi-tenant clouds; Kafka's ecosystem (clients, Kafka Connect, Streams, Flink) is still deeper in most companies.
Redpanda speaks the Kafka protocol. Your franz-go, segmentio/kafka-go, Java clients and much of Connect/Streams tooling can point at it. Implementation differs (C++, different threading and storage choices). Evaluate it when you want Kafka-compatible apps with a simpler operational profile — still verify the exact features you depend on (transactions, Connect connectors, etc.).
Kinesis is the closest AWS-native analogue: shards ≈ partitions, iterators ≈ offsets. APIs and scaling habits differ; throughput is per-shard. SQS is not an analogue — it is a queue. Combining SNS + SQS gives fan-out, but not "replay last seven days of the canonical event log" the way Kafka does.
1. auto.create.topics.enable in production is a footgun.
A typo creates a topic with default partitions and retention. Disable it; create topics with deliberate partition counts and retention via IaC or an admin process.
2. Partition count is mostly a one-way door.
You can add partitions; you cannot merge them. Adding partitions also breaks partition-key ordering for keys that hash to new destinations. Start with enough for foreseeable consumer parallelism, not "hundreds just in case."
3. Compaction is not a delete button with feelings.
Compacted topics still need tombstones (null values) to remove keys, and tombstones themselves have a retention window. "We compacted, so PII is gone" is incomplete unless tombstones and source systems are designed for it.
4. Exactly-once is a narrow contract.
Kafka's idempotent producers and transactions give exactly-once within Kafka's transaction boundary. Your database write, HTTP call or email send is still at-least-once unless you design idempotency around it. Exactly-once between Kafka and an external system is your job.
5. Consumer lag is a product metric, not only an ops metric.
Lag means your business is behind reality: notifications delayed, balances stale, search wrong. Alert on lag in wall-clock time for critical groups, not only on CPU.
6. The ISR can shrink under load.
If followers cannot keep up, they drop out of the ISR. With acks=all and min.insync.replicas higher than the surviving ISR size, producers start failing. That is durability protecting you — treat produce errors during broker pain as a signal to pause traffic or scale I/O, not to flip acks=1 quietly.
7. Schema discipline beats topic sprawl.
A new topic for every minor field change creates an integration swamp. Prefer a schema registry (Avro/Protobuf/JSON Schema), explicit compatibility rules, and versioned event types in fewer, well-owned topics.
8. Kafka Connect is the underrated workhorse.
Before writing another custom poller from MySQL or into Elastic, check whether a Connect source/sink already exists. Most "we'll just sync it" jobs become Connect + an SMT (single message transform) if you let them.
9. Transactions and consumer read isolation interact.
Transactional producers write markers; consumers with read_committed skip uncommitted data. Mixed settings across the pipeline are a common source of "why do I see duplicates / gaps" confusion.
10. Disk is sequential; capacity planning is about retention × traffic.
Estimate bytes/day × retention days × replication factor, then add headroom for compaction and bursts. Kafka fails in unpleasant ways when disks fill; prevent that with alerts before the broker does it for you.
Kafka is not "the modern queue." It is a replicated log that turns events into a shared, replayable backbone. Use it when that property pulls its weight. When you only need someone to process a job once, a queue will make you happier.
If you are wiring this into Go services, the same discipline from graceful shutdown applies to consumers: on deploy, stop fetching, finish held records, commit offsets, then exit — and keep handlers idempotent for the redeliveries you cannot avoid.
Frequently asked questions
›Is Kafka a message queue?
Not in the classic sense. Kafka is a distributed append-only log. Messages stay on disk for a retention window (or forever with compaction), and many consumer groups can read the same data independently. Queues typically delete a message once one consumer acks it.
›When should I pick RabbitMQ or SQS instead of Kafka?
When you need complex routing, per-message priorities, short-lived work queues, or you want a managed queue with almost no ops. Kafka's strength is durable, high-throughput streams that multiple systems can replay.
›What replaced ZooKeeper in Kafka?
KRaft (Kafka Raft). From Kafka 3.3 onward, production clusters can run without ZooKeeper; metadata lives in an internal Raft-replicated log managed by Kafka itself.
›How is Redpanda different from Kafka?
Redpanda speaks the Kafka protocol, so most clients work unchanged, but it is written in C++ and does not use the JVM or ZooKeeper/KRaft the same way. It aims for lower latency and simpler ops while staying API-compatible.