What Kafka is and how it works
Apache Kafka is a distributed event streaming platform: producers publish events to topics, Kafka stores them on disk, and consumers read them when they are ready. It suits exchanging events between services, collecting logs and metrics, and stream processing.
Kafka is a distributed system of servers and clients that talk over TCP. Some of the servers are brokers: together they form the event store. Clients are applications that write and read events. Producers and consumers know nothing about each other: a producer does not wait for a consumer.
An event (event, also record and message in the documentation) records the fact that something happened. An event has a key, a value, a timestamp and optional headers: the key “Alice”, the value “sent Bob 200 dollars”, the time of the payment.
How Kafka stores events
Events are gathered into topics — logical channels, similar to folders with files. A topic may have any number of consumers, and an event is not removed after reading: Kafka keeps it for a set time, say a week, and then discards it. So events can be re-read from any point.
Each topic is divided into partitions — “buckets” spread across different brokers. Events with the same key always land in the same partition, and ordering is guaranteed only within a partition. If event A and event B end up in different partitions, nobody promises the order in which they will be read.
The utilities that ship with Kafka do the same as client applications: create a topic, write events into it, read them back — a way to check the cluster before writing your own code:
bin/kafka-topics.sh --create \ --topic orders \ --partitions 3 \ --bootstrap-server localhost:9092bin/kafka-console-producer.sh \ --topic orders \ --bootstrap-server localhost:9092bin/kafka-console-consumer.sh \ --topic orders \ --from-beginning \ --bootstrap-server localhost:9092The --from-beginning flag reads partitions from the very beginning, not from the end. By setting --partitions 3 you set the number of topic partitions. See the section “Installation and first run” for installing and starting a broker.
Who works with Kafka and through what
Clients talk to brokers over the Kafka network protocol. Clients exist for Java and Scala, Go, Python, C/C++ and other languages. The core of Kafka is six APIs:
| API | What it is for |
|---|---|
| Producer | Writes a stream of events to one or more topics |
| Consumer | Reads events from topics and processes them |
| Share Consumer | Processes events cooperatively, from a share group |
| Streams | Turns streams from input topics into streams in output topics |
| Connect | Connects Kafka to external systems: source and sink connectors |
| Admin | Manages and inspects topics, brokers and other cluster objects |
Moving data from databases and files is usually not written by hand: Kafka Connect offers hundreds of ready connectors. For example, a PostgreSQL connector can pass every change of a table into Kafka. Moving changes this way is called change data capture (CDC).
Where Kafka fits and where it does not
Kafka is chosen when events flow between services as a stream, when every event may be read several times, and when losing an event is not an option. Typical cases from the documentation: replacing a classic message broker, tracking user actions on a website, collecting metrics and logs, multi-stage stream processing, event sourcing, and an external commit log for a distributed system.
Kafka is not needed when a synchronous request–response is enough for the services: an API call is simpler and clearer. It is also not needed at small loads, when a queue in a database or Redis Streams is enough: a cluster for a dozen events per minute is an extra system to operate.
How it differs from Redis Streams
Redis Streams is a Redis data type similar to an event log: records are appended and read, consumer groups distribute records among clients. The purpose is the same, the design is not: the comparison by key traits is in the table.
| Trait | Kafka | Redis Streams |
|---|---|---|
| Where events are stored | on the brokers’ disk, volume limited by time and policy | in the memory of a Redis node, old records are trimmed by MAXLEN or XTRIM |
| Write scaling | topic partitions are spread across brokers | one Redis key; distribution is manual, by keys or via Redis Cluster |
| Consumer groups | partitions are assigned to group members | all read one log: whoever is ready gets the record |
| Order | guaranteed within a partition | guaranteed within a single stream |
| Retention | configured per topic: hours, days, years | by default unlimited — until you trim it |
| Ecosystem | Connect, Streams, hundreds of clients and ready connectors | Redis clients and the XADD, XREAD, XREADGROUP commands |
Choose Kafka when events are the main data stream between services, the volumes grow, and history is needed for a long time with guaranteed order within a partition. Redis Streams is enough when the stream is small, the data fits in Redis memory, and a separate cluster is not wanted.
What to know in advance
- Containers and Docker Compose — the Docker reference: a local Kafka cluster is set up in containers.
- Java basics — client examples in the documentation are written in Java and Scala.
- Command line and bash — the Bash reference: Kafka utilities are run from the terminal.