Kafka Basics
Overview
Kafka is a popular system for handling huge streams of events. Unlike a classic queue that deletes a message once read, Kafka keeps messages in an ordered log that many readers can replay. This lesson builds up the idea from scratch.
Detailed Notes
What is Kafka
Kafka is a distributed event streaming platform. At its heart is a log: an append-only list where new messages are added at the end and are never changed. Messages stay for a configured time (for example 7 days) whether or not anyone has read them.
The problem it solves
A classic queue removes a message after one consumer takes it. But what if a new service launches next month and wants last week's events, or you need to reprocess after a bug fix? Also, one queue on one server cannot handle millions of messages per second. Kafka keeps a replayable log and splits it across many servers.
How it works
- Topic: a named stream, like
paymentsorclicks. - Partition: a topic is split into several partitions, each its own log, spread across servers. This is how Kafka scales (the same idea as data partitioning). Within one partition, messages keep strict order; there is no order guarantee between partitions.
- Key: a message may have a key, such as an account ID. Messages with the same key always land in the same partition, so one account's events stay in order.
- Offset: each message in a partition has a number: 0, 1, 2... A consumer remembers its position as an offset. Reading does not delete anything; it just moves the position forward. To replay, reset the offset.
- Consumer group: consumers that share a name split the partitions between them, so each partition is read by only one member of the group. This shares the work like a queue. A different group reads the same topic independently with its own offsets, like pub/sub.
- Brokers and replication: a Kafka server is called a broker. Each partition is copied to several brokers: one is the leader that handles reads and writes, the others are followers that copy it. If the leader fails, a follower takes over, using the replication and leader ideas from earlier chapters.
Limits: a group cannot usefully have more consumers than partitions, so plan the partition count ahead. Pick keys carefully: one very popular key makes one partition a hot spot.
A simple example
Think of a shared notebook that people only ever add lines to. Each reader keeps a bookmark (offset). Two friends read the same notebook with their own bookmarks, and nobody removes pages. A new reader can start from page 1.
System design example
A payments platform publishes every transaction to a topic payments with 6 partitions, keyed by account ID so each account's events stay in order. A fraud group of three consumers reads two partitions each, in real time. A separate ledger group reads the same events for accounting, and an analytics group replays them later. If a broker dies, followers take over its partitions. Consumer lag (how far a group is behind the newest message) tells you when to add consumers.
Summary
- Kafka stores messages in a durable, ordered, replayable log; reading does not delete.
- A topic is split into partitions for scale; order is guaranteed only within a partition.
- Keys send related messages to the same partition.
- Consumers track their position with offsets; consumer groups split partitions among members.
- Partitions are replicated with a leader and followers for fault tolerance.
Architecture Diagram
Producers append to partitions; each group reads independently with its own offsets.
- 1Producers append messages; the key picks the partition.
- 2Each partition is an ordered log replicated across brokers.
- 3Each consumer group reads with its own offsets.
- 4Within a group, each partition goes to one consumer.
Common Interview Questions
Chapter quiz
Messaging Systems · 4 questions
1.What is the main benefit of putting a broker between two services?
2.A consumer crashes before acknowledging, and the message is redelivered. Which guarantee is this?
3.What is a dead-letter queue for?
4.In Kafka, where is message order guaranteed?
0 of 4 answered
Finished this lesson?
0 of 91 lessons done · 0% of the course

