SproutStackSproutStack home

Kafka Intro (Beginner)

6 min readBeginner-friendlyFree to read

Before this, it helps to know: Queues & Pub/Sub.

Read a little, play a little. No scary maths, and no rush.

Share:

Scope: this is an overview, not a deep dive. It covers what you need to reason about a log-based pipeline — topics, partitions, offsets, consumer groups. Producer internals, replication and transactions are covered in Kafka in Depth. The authoritative reference is the official Apache Kafka documentation.

A queue that keeps a diary

A work queue hands each message to one worker and, done, forgets it. Kafka is a work queue that keeps the diary: messages stay on disk in order, each consumer tracks how far it has read, and you can move that position backwards. That single difference turns a pipeline into something you can replay.

The pieces

  • Topic — the named stream. You publish to it and subscribe to it; you never address partitions directly as a producer.
  • Partition — an ordered sub-log, and the unit of parallelism. Messages in one partition arrive in the order they were written.
  • Offset — a consumer's position within a partition, and the only state it has to keep.
  • Consumer group — a set of consumers sharing the work. Each partition in the group is assigned to exactly one member.
topic "orders"  →  [p0: 1,2,3,4]  [p1: 5,6]  [p2: 7,8,9]
consumer group      member A ←p0    member B ←p1,p2
Producerhash(key) → partitionPartition 01, 2, 3, 4Partition 15, 6Partition 27, 8, 9Member Aconsumer groupMember Bconsumer group
The key decides the partition, so order holds per key. Each partition goes to exactly one member of a group, and a member can own several.

Keys decide ordering, and that is the whole trick

Ordering is guaranteed within a partition and nowhere else. So if two messages must stay in order, they must land in the same partition — and you control that with the message key, because the partitioner hashes the key.

Same key → same partition → order preserved. No key → spread across partitions in batches → no ordering guarantee at all.

This is why "just add Kafka" is not free. Once events for one aggregate (one user, one order) share a key, adding partitions later will not reorder them, because that key still maps to a single partition. Choosing keys thoughtfully up front is what keeps a rebalance from silently reordering your business events.

Adding consumers has a ceiling

Partitions are the parallelism. A topic with 3 partitions and 9 consumers gives you 3 active consumers and 6 idle ones. To scale out further you add partitions — but each partition is a separate sub-log, so raising the partition count breaks key-to-partition stability for future messages and needs a plan for re-keying.

Delivery, honestly

Kafka gives at-least-once by default: a consumer can see the same message twice if it crashes after finishing the work but before committing its offset. So handlers must be idempotent. If you commit after processing you risk duplicates; if you commit before, you risk losing messages on a crash. Most systems accept duplicates and make handlers safe, because losing data is harder to notice and harder to recover than seeing one twice.

When a plain queue is enough

If each message only needs to be processed once by one worker, and nobody will ever need to replay history, a simpler work queue is easier to run and reason about. Choose Kafka when several independent consumers need the same stream, when ordering per key matters, or when being able to replay events is worth operating a log.

Remember this

  • Ordering is per partition. Use keys to put related messages together.
  • Partitions cap consumer parallelism; extra members idle.
  • At-least-once means handlers must be idempotent.
  • The retained log is the real reason to choose Kafka: you can replay it.

Check your understanding

5 questions · first-try answers earn the most XP

1. A Kafka topic is divided into partitions. What does a single partition guarantee?
2. What does a Kafka consumer group give you in practice?
3. A consumer crashes and restarts. How does it avoid reprocessing everything from the beginning?
4. What is the main practical reason to choose Kafka over a plain work queue?
5. Why does Kafka give at-least-once delivery by default, and what must a consumer do about it?

My notes

Saved in this browser. Highlight a line above and save it, or write it in your own words.

Nothing saved yet. Your highlights will live here.

References

Finished reading?

Ticking it here also ticks the chapter in the sidebar, the section count and your streak — it is all one number.

Practice this in an incident

Try a design drill

Related chapters

Spotted a mistake or want a topic covered? Report an issue

📬 An occasional email when new lessons land

New lessons and showdowns, no more than a few times a month. No spam, unsubscribe anytime. We store your email only to send the newsletter.