Kafka 101

Real-time event streaming at scale

Kafka
Author

Shane Oh

Published

June 7, 2025

There is a introductory youtube video on Kafka from ByteByteGo. You may also want to check out the tutorial videos from Confluent.

Kafka is a distributed event streaming platform that is used for building real-time data pipelines and streaming applications. It is designed to handle high throughput and low latency, making it suitable for processing large volumes of data in real-time.

It was first developed by LinkedIn and later open-sourced in 2011. Kafka is written in Scala and Java, and it is now maintained by the Apache Software Foundation.

The founder of the company is Jay Kreps, who was one of the original developers of Kafka at LinkedIn, co-founded Confluent in 2014 to provide a commercial offering around Kafka and to help organizations leverage its capabilities for real-time data processing.

Confluent offers cloud services for Kafka, including managed Kafka clusters, connectors, and tools for building and managing streaming applications.

Concepts

The big picture of Kafka

[producers] -> [topics] -> [consumers]
                  |
             [partitions] for parallelization
                  |
          [leader/followers] for fault tolerance
                  |
          [logs with offsets] stored on disk

Events

An event records the fact that “something happened” in the world or in your business. It is also called record or message. The key components of a message are as follows.

message
{
  key: "Alice",
  value: "{\"event\": \"login\"}",
  timestamp: "2025-06-20T14:00:00Z"
  headers: {
    event-type: "user-action",
    source: "web"
  },
}
  • key (optional): Later used for distributing messages into partitions, hash("Alice") % #partitions.
  • value: The actual message from the event source.
  • timestamp (optional; auto-generated if not provided): The time when the event was produced.
  • headers (optional): Contains metadata such as routing information, etc.

Producers and Consumers

Producers are those client applications that publish (write) events to Kafka, and consumers are those that subscribe to (read and process) these events by polling.

In modern architectures, it is common to have multiple producers and consumers interacting — producers are likely to be pumping data at a high rate, while consumers may handle data at their own pace. This makes it crucial to decouple the data producers from the consumers.

NoteKafka vs message queues

The key difference is that Kafka is a log-based system, whereas traditional message queues are queue-based, where consumed events are typically ephemeral.

Kafka takes a more durable and fault-tolerant approach compared to other message queues by using a file-based log system. Logs in Kafka are append-only, ordered, and durable, which makes them replayable.

Since logs do accumulate data over time, we can configure a retention period to control how long the data is kept.

Topics and Partitions

A Kafka cluster can have multiple brokers and controllers. Controller is elected among quorum using Kraft which was previously Zookeeper.

┌─Kafka cluster───────────────────────────────┐
│┌─node.id=1 process.roles=broker,controller─┐│ ╮
││            topic=A partition=1            ││ │ 
││            topic=A partition=2            ││ │
││            topic=B partition=1            ││ │
│└───────────────────────────────────────────┘│ │ quorum
│┌─node.id=2 process.roles=broker,controller─┐│ │
││            topic=B partition=2            ││ │
││            topic=B partition=3            ││ │
││            topic=C partition=1            ││ │
│└───────────────────────────────────────────┘│ ╯
└─────────────────────────────────────────────┘
  • Each topic has one or more partition for parallelism.
  • Each partition can be replicated.
  • Among replicated partitions, a leader is elected from the in-sync replicas (ISR).
                         #offset             ╭─ High water mark
             partition=X [0][2][3][4][5][6][7]       In sync follower ╮
Prod <-ack-- partition=X [0][2][3][4][5][6][7][8][9] Leader           │ In sync replicas (ISR)
             partition=X [0][2][3][4][5][6][7][8]    In sync follower ╯
             partition=X [0][2][3][4][5]             Out of sync follower
                                       ╰────Lag────╯

When a producer sends a message to Kafka, it receives an ack only after the message is successfully written to the leader.

acks Description Durability
acks=0 Producer does not wait for any broker ack Low (high risk of message loss)
acks=1 Only the leader broker acknowledges receipt to the producer Medium
acks=all or -1 Leader and all in-sync replicas acknowledge receipt High

Hands on

Single-node

A tutorial from the blog is good place to start.

$ docker run -d --name kafka apache/kafka:4.0.0
$ docker exec -it kafka /bin/sh

In the docker container

  • /opt/kafka/config: config files for Kafka
    • server.properties
  • /opt/kafka/bin: bins files directory
    • kafka-server-start.sh server.properties
    • kafka-server-stop.sh
    • kafka-topics.sh --list --bootstrap-server localhost:9092
    • kafka-topics.sh --create --topic X --bootstrap-server localhost:9092
    • kafka-topics.sh --delte --topic X --bootstrap-server localhost:9092
  • /tmp/kraft-combined-logs: log files from Kafka

Multi-node (with Kraft)

Check the docs from docker hub page for more.

  ┌─────Kafka cluster─────┐
  │      ┌controller-1~3┐ │
  │  9093:9093          │ │
  │      └──────────────┘ │
  │      ┌───broker-1───┐ │
  │ 19092:19092         │ │
 ───29092:9092          │ │
  │      └──────────────┘ │
  │      ┌───broker-2───┐ │
  │ 19092:19092         │ │
 ───39092:9092          │ │
  │      └──────────────┘ │
  │      ┌───broker-3───┐ │
  │ 19092:19092         │ │
 ───49092:9092          │ │
  │      └──────────────┘ │
  └───────────────────────┘
docker-compose.yaml
services:
  controller-1:
    image: apache/kafka:latest
    container_name: controller-1
    environment:
      KAFKA_NODE_ID: 1
      KAFKA_PROCESS_ROLES: controller
      KAFKA_LISTENERS: CONTROLLER://:9093
      KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT
      KAFKA_CONTROLLER_LISTENER_NAMES: CONTROLLER
      KAFKA_CONTROLLER_QUORUM_VOTERS: 1@controller-1:9093,2@controller-2:9093,3@controller-3:9093
      KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: 0

  controller-2:
    image: apache/kafka:latest
    container_name: controller-2
    environment:
      KAFKA_NODE_ID: 2
      KAFKA_PROCESS_ROLES: controller
      KAFKA_LISTENERS: CONTROLLER://:9093
      KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT
      KAFKA_CONTROLLER_LISTENER_NAMES: CONTROLLER
      KAFKA_CONTROLLER_QUORUM_VOTERS: 1@controller-1:9093,2@controller-2:9093,3@controller-3:9093
      KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: 0

  controller-3:
    image: apache/kafka:latest
    container_name: controller-3
    environment:
      KAFKA_NODE_ID: 3
      KAFKA_PROCESS_ROLES: controller
      KAFKA_LISTENERS: CONTROLLER://:9093
      KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT
      KAFKA_CONTROLLER_LISTENER_NAMES: CONTROLLER
      KAFKA_CONTROLLER_QUORUM_VOTERS: 1@controller-1:9093,2@controller-2:9093,3@controller-3:9093
      KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: 0

  broker-1:
    image: apache/kafka:latest
    container_name: broker-1
    ports:
      - 29092:9092
    environment:
      KAFKA_NODE_ID: 4
      KAFKA_PROCESS_ROLES: broker
      KAFKA_LISTENERS: 'PLAINTEXT://:19092,PLAINTEXT_HOST://:9092'
      KAFKA_ADVERTISED_LISTENERS: 'PLAINTEXT://broker-1:19092,PLAINTEXT_HOST://localhost:29092'
      KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT
      KAFKA_CONTROLLER_LISTENER_NAMES: CONTROLLER
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
      KAFKA_CONTROLLER_QUORUM_VOTERS: 1@controller-1:9093,2@controller-2:9093,3@controller-3:9093
      KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: 0
    depends_on:
      - controller-1
      - controller-2
      - controller-3

  broker-2:
    image: apache/kafka:latest
    container_name: broker-2
    ports:
      - 39092:9092
    environment:
      KAFKA_NODE_ID: 5
      KAFKA_PROCESS_ROLES: broker
      KAFKA_LISTENERS: 'PLAINTEXT://:19092,PLAINTEXT_HOST://:9092'
      KAFKA_ADVERTISED_LISTENERS: 'PLAINTEXT://broker-2:19092,PLAINTEXT_HOST://localhost:39092'
      KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT
      KAFKA_CONTROLLER_LISTENER_NAMES: CONTROLLER
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
      KAFKA_CONTROLLER_QUORUM_VOTERS: 1@controller-1:9093,2@controller-2:9093,3@controller-3:9093
      KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: 0
    depends_on:
      - controller-1
      - controller-2
      - controller-3

  broker-3:
    image: apache/kafka:latest
    container_name: broker-3
    ports:
      - 49092:9092
    environment:
      KAFKA_NODE_ID: 6
      KAFKA_PROCESS_ROLES: broker
      KAFKA_LISTENERS: 'PLAINTEXT://:19092,PLAINTEXT_HOST://:9092'
      KAFKA_ADVERTISED_LISTENERS: 'PLAINTEXT://broker-3:19092,PLAINTEXT_HOST://localhost:49092'
      KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT
      KAFKA_CONTROLLER_LISTENER_NAMES: CONTROLLER
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
      KAFKA_CONTROLLER_QUORUM_VOTERS: 1@controller-1:9093,2@controller-2:9093,3@controller-3:9093
      KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: 0
    depends_on:
      - controller-1
      - controller-2
      - controller-3