Kafka Deep Dive
Apache Kafka is the streaming standard. It powers event pipelines at LinkedIn (where it was created), Netflix, Uber, Airbnb, and basically every modern SaaS at scale. Understanding how Kafka actually works under the hood is what separates βI use Kafkaβ from βI run Kafka well.β
The Core Abstraction: Distributed, Partitioned, Replicated Log
Kafka is fundamentally a commit log: an ordered, immutable sequence of records. The clever part is that this log is distributed, partitioned, and replicated for scale and fault tolerance.
Topic: "orders" (3 partitions, replication factor 3)
Partition 0: [ msg0 β msg1 β msg2 β ... ] Leader: Broker 1 Followers: Broker 2, 3
Partition 1: [ msg0 β msg1 β msg2 β ... ] Leader: Broker 2 Followers: Broker 1, 3
Partition 2: [ msg0 β msg1 β msg2 β ... ] Leader: Broker 3 Followers: Broker 1, 2
Producers
A producer publishes messages to topics. It chooses the partition based on:
Producer acks
How many replicas must acknowledge before the producer considers the write successful?
Idempotent producer
When enabled (enable.idempotence=true), the producer attaches a sequence number to each message. The broker rejects duplicates from the same producer. Combined with acks=all, you get exactly-once writes within Kafka.
Consumers
A consumer reads messages from topics. The key concepts:
Consumer group example
Topic: "orders" (4 partitions)
Consumer Group "order-processor":
- Consumer A β Partition 0, 1
- Consumer B β Partition 2, 3
Consumer Group "analytics":
- Consumer X β Partition 0, 1, 2, 3 (one consumer reads everything)
Both groups receive all messages independently.
This is what makes Kafka different from a queue: multiple consumer groups read the same data independently. Each group tracks its own position. Want to add a new pipeline tomorrow? Spin up a new consumer group; it reads from the beginning (or wherever you want).
Replication and Fault Tolerance
For each partition, one broker is the leader; others are followers. All reads and writes go through the leader. Followers replicate from the leader.
In-sync replicas (ISR)
A follower is βin-syncβ if it has caught up with the leader within a configurable threshold. Only ISRs are eligible for leadership.
Leader election
When a leader fails, a follower from ISR is promoted. With unclean.leader.election.enable=false (recommended), only ISRs can be elected β preventing data loss at the cost of availability.
High water mark
The high water mark is the highest offset replicated to every in-sync replica. The leader advances it only after the slowest member of the ISR has that record. Consumers may read only up to the high water mark, so a fetch never returns a record that could vanish if the leader fails before the ISR has it. On failover the controller promotes an in-sync replica, preferring the one whose committed offset is furthest ahead, which is why a record below the high water mark (the record acks=all waited for) is still present on the new leader.
A follower whose lag passes the configured threshold leaves the ISR. It can no longer be elected, and it no longer holds the high water mark back. The partition keeps accepting writes against a smaller quorum.
Brokers store each partition as segment files. A segment is closed and a new one opened when it reaches a size or age limit, so retention deletes whole files. One broker is the cluster controller: historically it won by creating the lowest ephemeral sequential ZooKeeper node under the broker registry, and a session timeout starts a new election. KRaft stores that same metadata as a log. The per-partition ISR and high water mark rules stay as they are.
Storage
Kafka writes data to disk. The trick: sequential disk writes are nearly as fast as memory writes because of OS page cache and disk read-ahead.
Kafka Streams and KSQL
Kafka isnβt just a transport β it has built-in stream processing:
For more sophisticated stream processing (or polyglot environments), use Flink with Kafka as input/output.
Kafka Connect
Pre-built connectors for getting data in and out of Kafka:
Connect handles offset management, error handling, exactly-once. You write configuration; the connectors handle the rest.
Operational Realities
For most teams, managed Kafka is the right call:
Multi-Tenant SaaS Patterns with Kafka
A few patterns specifically for SaaS:
Recap
- Kafka is a distributed, partitioned, replicated commit log.
- Topics β partitions (parallelism) β replicas (durability).
- Producers choose partitions by key (ordered) or round-robin (max throughput).
acks=allfor durability. - Consumer groups distribute work; multiple groups read the same data independently.
- ISR + min.insync.replicas + acks=all = production-grade durability.
- Kafka Streams / ksqlDB for in-Kafka stream processing. Connect for getting data in/out.
- Managed Kafka (Confluent, MSK, Aiven) is the right call for most teams.
- For SaaS multi-tenancy: topic-per-tenant for isolation, key-partitioning for scale, tier-based for hybrid models.