Skip to main content

Command Palette

Search for a command to run...

Apache Kafka Fundamentals: Creating Scalable and Dependable Event-Driven Systems

Updated
4 min read
Apache Kafka Fundamentals: Creating Scalable and Dependable Event-Driven Systems

In our previous blog, we covered the basic building blocks like producers, consumers, and topics that help send messages from one service to another. To build a truly scalable app, we need to go beyond these basics. Let's look at some other Kafka concepts that help us make communication production-ready.


Partitions

Imagine a topic in Kafka like a big folder where all messages about the same thing go. Now picture 1000 orders coming in at once — all those order messages go into this folder. The problem is, they get handled one by one, like people standing in a single line. That can be slow when there are lots of orders.

Partitions fix this by splitting the big folder into smaller pieces called partitions. Think of partitions like separate checkout lines at a grocery store — multiple lines help more people check out quickly.

Kafka makes sure messages stay in order within each partition, because sometimes order really matters. But messages in different partitions don't have to be in order.

So partitions help Kafka work on many messages at the same time, speeding things up and making them smoother.


Message Key

Keys play an important role in deciding which partition a message goes to. Kafka uses the key to figure this out by running it through a process called hashing (think of it like a recipe that always gives the same result for the same key). This makes sure all messages with the same key end up in the same partition.

If a message doesn't have a key, Kafka spreads those messages evenly across different partitions, like taking turns, to keep things balanced.

Using keys lets us group related messages together in one partition. For example, all events related to a single user will always go to the same partition, so they stay in order.


Consumer Group

Partitions let us split events into multiple streams so consumers can read them faster. But what if you have two partitions and two consumers? How do we make sure each consumer reads different partitions without both reading the same messages?

That's where consumer groups come in. You can create multiple consumers that share the same group ID. Kafka then assigns partitions to each consumer in the group, making sure each partition is read by only one consumer. This way, consumers work in parallel without duplicate messages.

So, consumer groups help spread the work and make event processing faster and more efficient.

Consumer Group Scenarios:

  • Fewer consumers than partitions: Some consumers will read multiple partitions.
  • Equal consumers and partitions: Each consumer reads exactly one partition.
  • More consumers than partitions: Some consumers will be idle, waiting for partitions to become available.

Offset

How do consumers know which message to read next? And if they stop for a while, how do other consumers in the group know where to continue?

That's where offsets come in. Every message in a partition gets a unique number called an offset — it's like a simple count starting at 0. Kafka keeps track of offsets separately for each partition to make sure messages stay in the right order.

Consumers "commit" their current offset to Kafka to remember which messages they've read. This way, if they stop or another consumer takes over, they know exactly where to pick up without missing or repeating messages.


Replication

We know a Kafka cluster has multiple brokers so if one broker goes down, others can keep things running. But how do those other brokers get the events they need to continue without missing anything?

That's where replication comes in. Kafka makes copies of each partition and stores them on different brokers in the cluster. This means if one broker fails, the data is still safe and available from the other brokers.

When you create a topic, you decide the replication factor — basically how many copies of each partition you want. The more copies, the safer your data is.

A few other things to remember:

  • The replication factor can't be bigger than how many brokers you have, because each copy lives on its own broker.
  • Every partition has a "leader" broker responsible for handling all reads and writes.
  • If the leader broker crashes, Kafka automatically switches leadership to another broker that has a copy—making sure your data stream never misses a beat.

Replication Scenarios:

  • Equal brokers and replication factor: Each broker holds one copy of each partition.
  • More brokers than replication factor: Some brokers won't hold copies of certain partitions, providing flexibility for scaling.

Summary

Together, these concepts form the core of Kafka's ability to handle large-scale event streaming, making sure your app can process messages quickly, reliably, and without losing data.

Key takeaways:

  • Partitions enable parallel processing by splitting topics into smaller streams.
  • Message keys ensure related messages are grouped together in the same partition.
  • Consumer groups coordinate multiple consumers to process partitions in parallel without duplication.
  • Offsets track consumer progress and enable reliable message delivery.
  • Replication provides fault tolerance by maintaining multiple copies of data across brokers.

These essential concepts work together to make Kafka a powerful platform for building scalable and reliable event-driven applications.

More from this blog

Go & DevOps Blog

24 posts

Backend Developer | Python | Go | gRPC | Kubernetes | Ansible | IaC