Messaging and streaming

Apache Kafka

Kafka is a distributed event log for high-throughput data streaming with durable storage.

What it is

Kafka was built at LinkedIn, open sourced in 2011 and later became an Apache project. It is not a classic queue but a distributed log: messages are appended to a topic and kept for a set time or size, even after they are read.

A topic is split into partitions. Order is guaranteed within a partition, and consumers in one consumer group share partitions and scale in parallel. Each consumer tracks its own position (offset) and can re-read history.

Kafka is used for event streams, log and metric collection, CDC from databases, microservice integration and event sourcing. New clusters run in KRaft mode without ZooKeeper.

Use it when

  • You need high throughput: hundreds of thousands of messages per second.
  • Events must be stored and re-read by several independent consumers.
  • You build stream processing, CDC or event sourcing.
  • You need strict ordering of events per key.

Avoid it when

  • You need a simple task queue for a few workers: RabbitMQ or SQS are simpler.
  • Volume is small and the team cannot operate a cluster.
  • You need message routing by complex rules or priorities.

Key concepts

Topic / partition
A topic is a category of events, a partition is an ordered slice of it for parallelism.
Offset
The sequence number of a message in a partition, which tells a consumer where it stopped.
Consumer group
A group of consumers shares a topic's partitions: each partition goes to one group member.
Replication factor
How many copies of each partition are stored on different brokers.
Retention / log compaction
Storage policy: by time, by size, or only the latest value per key.

Example

Creating a topic with three partitions, writing an event and reading from the start.

Cheat sheet

Kafka cheat sheet
Command or codeWhat it does
kafka-topics.sh --create --topic orders --partitions 3 --bootstrap-server localhost:9092Create a topic with 3 partitions
kafka-topics.sh --list --bootstrap-server localhost:9092List topics
kafka-topics.sh --describe --topic orders --bootstrap-server localhost:9092Topic details: partitions and replicas
kafka-console-producer.sh --topic orders --bootstrap-server localhost:9092Send messages from the console
kafka-console-consumer.sh --topic orders --from-beginning --bootstrap-server localhost:9092Read a topic from the beginning
kafka-console-consumer.sh --topic orders --group billing --bootstrap-server localhost:9092Read as a consumer group member
kafka-consumer-groups.sh --describe --group billing --bootstrap-server localhost:9092Group lag per partition
kafka-consumer-groups.sh --group billing --topic orders --reset-offsets --to-earliest --execute --bootstrap-server localhost:9092Reset the group offset to the beginning
kafka-topics.sh --delete --topic orders --bootstrap-server localhost:9092Delete a topic

How to learn it

  1. Understand the model: topic, partition, offset, producer, consumer and consumer group, and how it differs from a queue.
  2. Run Kafka locally (KRaft mode, no ZooKeeper) in Docker and use the CLI scripts.
  3. Create a topic, send messages from the console producer and read them with a consumer.
  4. Write a producer and consumer in your language (kafkajs, confluent, the Java client) and watch offsets work.
  5. Learn message keys, ordering within a partition and how partitions are spread across a consumer group.
  6. Study delivery guarantees: acks, idempotent producer, at least once, exactly once, offset commits.
  7. Add error handling, retries, a dead letter topic and consumer lag monitoring.
  8. Read about Kafka Connect, Schema Registry and Streams for integrations and stream processing.

Interview questions

  1. 1. How does Kafka differ from a regular queue?

    In a queue a message disappears once consumed. In Kafka it stays in the log per the retention policy, and several consumers read it independently with their own offsets.

  2. 2. How does Kafka guarantee ordering?

    Only within a single partition. To keep events of one entity ordered, write them with the same key so they land in the same partition.

  3. 3. What are delivery semantics?

    At-most-once can lose messages, at-least-once can duplicate them, exactly-once is reached with an idempotent producer and transactions. In practice you make consumers idempotent.

  4. 4. How many consumers can read a partition?

    Within one consumer group, just one. So the partition count caps the group's parallelism. Another group reads the same data separately.

Interview questions from the bank

Comparisons with this technology

Logos belong to their owners and are used for identification only. Icons: Simple Icons (CC0), Devicon (MIT).