Kafka Concepts

November 12, 2026

Kafka is designed as an event streaming system so application can react immediately as the event occurs.

Event source > Event Stream (Kafka) > Event destination

Where event stream contains records, and each record contains:

Record =>
  timestamp
  key
  value
  headers (optional)

Where key is used for 3 purposes:

  • enforcing ordering
  • locating data and keep it in sync
  • retention


✦ Each record’s key and value is serialized into bytes by producers before sending them to Kafka. Consumers deserialize those bytes back into usable data. Avro and Protobuf are common schema-based formats.


✦ A Schema Registry stores and versions schemas that define the structure of records. It runs as a separate service and checks compatibility as schemas change, helping producers and consumers evolve safely.


✦ There are several APIs available in Kafka out of the box, including:

  • Producer (to produce records)
  • Consumer (to consume records)
  • Connect (to connect with ecosystem i.e. search engine)
  • Streams (to continuously process events, think process layer)


Topics

A topic is a logical group of same type of events, such as orders or payments. When you publish to Kafka, you need to specify which topic you want to publish your records to and the same goes for consumers. All records published to a topic are immutable and append only. Reading a record does not delete it, Kafka’s retention and cleanup settings determine how long it stays. Here is how to create a topic1:

kafka-topics.sh 
  --bootstrap-server localhost:9092
  --create --topic orders


✦ Since Kafka is designed as a distributed system, we need a way to distribute data within each topic into different computing nodes (brokers) in a Kafka cluster. That is achieved using partitions.

Each partition is an ordered log where new records are appended to its end. Each partition can be accessed independently and in parallel by producers to publish and consumers to consume.

Each record within the partition has a unique id, called offset, a monotonically increasing number and once given out it is never reused. With that, ordering is guaranteed within each partition but the topic as a whole has no single ordering across all its partitions.

The partition count can be specified when creating the topic:

kafka-topics.sh 
  ...
  --partitions 3 

If omitted, Kafka can use the configured defaults. You can increase a topic’s partition count later but Kafka does not support reducing it. Increasing it does not redistribute existing records.


✦ Each partition can have multiple replicas. Replication means keeping copies of a partition on different brokers.

kafka-topics.sh 
  ...
  --replication-factor 3 

The replication factor is the total number of assigned copies including the leader:

  • Factor 1: one copy.
  • Factor 2: one leader and one follower.
  • Factor 3: one leader and two followers.

Each partition has its own leader. Producers send writes to that leader, and followers replicate its log. If the leader fails, Kafka can elect an eligible replica to take over.


Brokers

A broker is a Kafka server that receives records from producers, stores them, and serves them to consumers. Multiple brokers work together as a Kafka cluster, allowing data and traffic to be distributed across different machines.

Kafka cluster =>
  Broker 1
  Broker 2
  Broker 3


✦ Each broker stores a set of partition replicas (discussed above), potentially belonging to many different topics. A broker can be the leader for some partitions and a follower for others. For example, an orders topic with 3 partitions and a replication factor of 2 could be distributed like this:

Broker 1 => orders-0 (leader),  orders-2 (follower)
Broker 2 => orders-1 (leader),  orders-0 (follower)
Broker 3 => orders-2 (leader),  orders-1 (follower)

Here, each partition has copies on two different brokers. If one broker fails, an eligible replica on another broker can take over leadership for its affected partitions.


✦ Inside a broker, network threads handle communication with clients and other brokers, while I/O threads process requests, including appending records to partition logs.

Request =>
  Network thread
  Request queue
  I/O thread

Response =>
  Response queue
  Network thread
  Client

Separating these responsibilities allows the broker to handle many connections while processing reads and writes efficiently.

For partitions where the broker is the leader, it accepts writes from producers and serves replication fetch requests from followers. For partitions where it is a follower, background replica fetcher threads fetch new records from the leader and append them to its local log. Replication is therefore pull based where followers request records from the leader.


✦ To connect to a cluster, producers and consumers are given a list of broker addresses called bootstrap servers:

bootstrap.servers = broker1:9092, broker2:9092, broker3:9092

The client connects to an available broker to discover the cluster’s metadata, including which broker leads each partition. It then connects directly to the appropriate brokers to produce or consume records.