Blog
What Actually Happens When You Produce a Kafka Message
Kafka is not a queue you drain but a durable, replayable log, and understanding that single distinction reshapes how you design every event-driven service you build.
If you come from the MERN world, you probably think of messaging as putting a job on a queue and having a worker pull it off, never to be seen again. Kafka quietly breaks that mental model. It is not a queue that empties; it is an append-only commit log that retains messages for a configured time regardless of who has read them. Producers append records to the end of partitioned logs, and consumers track their own position with offsets. Nothing is deleted because someone consumed it. That one design decision unlocks replay, multiple independent readers, and the kind of durable history that event streaming depends on. Before you can reason about consumer groups, delivery guarantees, or event sourcing, you need a clear picture of how a single message physically lives inside a Kafka topic.
Topics, Partitions, And Offsets
A topic is a named stream of records, but the real unit of work is the partition. Each topic is split into one or more partitions, and every partition is an ordered, immutable sequence of records. When a record lands in a partition it receives a monotonically increasing offset, a 64-bit number that uniquely identifies its position. Ordering is guaranteed only within a single partition, never across the whole topic, which surprises developers expecting global order. If you have used MongoDB, think of a partition as a single ordered collection where the offset is an auto-incrementing index. Consumers read forward by offset and commit how far they have progressed. Because offsets are just pointers, two consumers can read the same partition at completely different positions, which is why Kafka supports many independent readers over identical data without copying it.
The Log Is The Database
Kafka stores each partition as a set of segment files on disk, appending new records sequentially. Sequential disk writes are dramatically faster than random ones, which is part of why Kafka sustains enormous throughput on ordinary hardware. Records are not removed when read; they age out by retention policy, either after a time window such as seven days or once a size threshold is crossed. You can also enable log compaction, where Kafka keeps only the latest record per key, turning a topic into a durable snapshot of current state. This retention model means the log itself becomes a source of truth you can query by replaying it. Coming from Express APIs backed by a single database, the shift is that the stream of changes, not just the final row, is now a first-class, persisted artifact you can rebuild from.
How Producers Choose Partitions
When you send a record, the producer decides which partition it lands in. If you supply a key, Kafka hashes it and maps the hash to a partition, so all records sharing a key consistently route to the same partition and therefore preserve their relative order. If you omit the key, the producer spreads records across partitions using a sticky batching strategy for throughput. This is the most important lever you control. Choosing user-id as the key keeps every event for one user ordered, which matters when later events depend on earlier ones. Choosing a poor key, like a constant value, funnels all traffic into one partition and destroys parallelism. In Node terms, the partition key is the closest thing you have to a shard key, and picking it badly creates the same hotspot problems you would see in a poorly sharded Mongo cluster.
Brokers And Replication
A Kafka cluster is a set of brokers, and each partition is hosted by one broker acting as its leader. All reads and writes for that partition go through the leader, while follower brokers replicate the data to provide fault tolerance. The set of replicas currently caught up with the leader is called the in-sync replica set, or ISR. If the leader broker dies, one of the in-sync followers is promoted, and clients transparently reconnect to the new leader. The replication factor, often three in production, determines how many copies exist. This is conceptually similar to a MongoDB replica set with a primary and secondaries, but Kafka tracks sync status per partition rather than per database. Understanding ISR matters because your durability guarantees, controlled by producer acks, depend directly on how many replicas have confirmed a write.
Producer Acknowledgements Explained
The producer acks setting governs how much confirmation you wait for before considering a send successful. With acks=0 the producer fires and forgets, never waiting, which is fast but can silently lose data. With acks=1 the leader confirms once it has written the record locally, but if that leader crashes before followers replicate, the record vanishes. With acks=all the leader waits until all in-sync replicas have the record, giving the strongest durability at the cost of latency. Pairing acks=all with a min.insync.replicas of two ensures a write is never acknowledged unless at least two brokers hold it. For a backend developer this is the dial between speed and safety, much like deciding whether a database write should wait for a majority write concern. Most production pipelines that matter run acks=all.
Why Event Streaming Differs From Queues
Traditional brokers like RabbitMQ are destructive: a message is delivered, acknowledged, and gone, and adding a second consumer means it competes for those same messages. Kafka inverts this. Because the log persists and consumers track their own offsets, you can attach a brand-new service tomorrow and have it replay months of history without affecting existing consumers. This is the heart of event streaming. The same payment-completed event can feed your billing service, your analytics pipeline, and your fraud detection model, each reading independently at its own pace. You are no longer point-to-point wiring services together; you are publishing facts to a shared, durable timeline. That decoupling is the strategic reason teams adopt Kafka, and it directly enables event-driven architecture and event sourcing patterns built on top of the same underlying log.
Throughput, Batching, And Compression
Kafka achieves its famous throughput by batching aggressively. Producers do not send one record per network round trip; they accumulate records in memory buffers and flush them as batches, controlled by linger.ms and batch.size. A small linger value adds a few milliseconds of delay to gather more records, dramatically improving efficiency. Batches can be compressed with codecs like lz4, snappy, or zstd, and importantly the batch stays compressed all the way to disk and across replication, saving both bandwidth and storage. On the consumer side, fetches also pull batches rather than single records. Tuning these knobs is how you trade latency for volume. Coming from request-response HTTP, the surprise is that adding a little artificial delay can make the whole system faster, because amortizing fixed per-request overhead across many records is where the real wins live.
A Mental Model To Keep
Carry one image forward: Kafka is a partitioned, append-only log that many independent readers scan at their own offsets, with replication for durability and keys for ordering. Producers append facts; the cluster persists and replicates them; consumers read forward and remember where they stopped. Almost every advanced topic is a refinement of this picture. Consumer groups are about splitting partitions among readers. Delivery guarantees are about how offsets and acks interact under failure. Event sourcing is about treating the log as the authoritative history of your domain. If you internalize the log model first, the rest stops feeling like magic and starts feeling like consequences. When something behaves unexpectedly, return to this base layer and ask what the offsets, partitions, and replicas are actually doing, and the behavior usually explains itself.