Kafka in distributed system
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Apache Kafka is an open-source stream-processing software platform developed by LinkedIn and donated to the Apache Software Foundation, written in Scala and Java. It is designed to handle data feeds with high throughput and low latency. Kafka is widely used for building real-time streaming data pipelines and applications that adapt to data streams.
Understanding Kafka Architecture
Kafka operates on a cluster of one or more servers (known as brokers), and it stores records in categories known as topics. Each record consists of a key, a value, and a timestamp. Key components of Kafka include:
- Topic: A stream of records. It acts as a category or feed name to which records are published.
- Producer: Any entity that publishes data to a Kafka topic.
- Consumer: An entity that subscribes to topics and processes the feed of published records.
- Broker: A Kafka server that stores data and serves clients.
- ZooKeeper: Manages and coordinates Kafka brokers. It's a centralized service for maintaining configuration information, naming, and providing distributed synchronization.
Kafka's Data Model
Data in Kafka is organized by topics. Within each topic, data is split into one or more partitions. Partitions allow Kafka’s data to be distributed over multiple nodes in the cluster to balance load and ensure redundancy. A partition is an ordered sequence of records that are immutable. Consumers read data from partitions.
Partitioning and Fault-Tolerance
Kafka replicates partitions across multiple brokers. This means that data sent to a partition can be duplicated across multiple brokers to protect against data loss in case of a broker failure. The degree of replication is configurable:
- Leader Partition: The partition that handles all read and write requests for the specific partition while replicating the data to the follower partitions.
- Follower Partition: The partition that passively replicates the leader.
If the leader fails, one of the followers will automatically become the new leader.
Key Features and Benefits of Kafka
- Scalability: Kafka can handle millions of messages per second. Scalability can be achieved by adding more brokers to a Kafka cluster and redistributing partitions on these new nodes.
- Performance: Consistent performance even with large volumes of data due to the distributed nature of its architecture.
- Fault Tolerance: Offers robust against node failure within the cluster. Messages are replicated across multiple nodes to prevent data loss.
- Flexibility: Allows you to publish and subscribe to streams of records. It also allows for processing streams in real-time.
Use Cases
- Real-Time Monitoring Systems
- Log Aggregation Solutions
- Stream Processing
- Event Sourcing
- Website Activity Tracking
Example: Setting Up a Simple Kafka Environment
Creating a Kafka Topic:
Writing Messages to Topic:
Reading Messages from Topic:
Conclusion
Apache Kafka is a powerful tool in the toolbox for developers and companies looking to process and analyze streaming data. By leveraging Kafka's capabilities, organizations can drive real-time insights and actions, which are crucial in today’s fast-paced digital environment.
| Feature | Description |
| Fault Tolerance | High availability and data redundancy through replication across multiple nodes. |
| Scalability | Horizontal scaling, which allows adding more brokers to the cluster to accommodate more clients and data. |
| Performance | High throughput for both publishing and subscribing. Handles large volumes of data efficiently. |
| Flexibility | Consumer and producer can operate independently. The system efficiently processes and manages simultaneous inputs and outputs of streams with different lifecycles. |
By understanding Kafka’s architecture and capabilities, developers can better harness its potential in building robust, scalable, and real-time applications.
Related reading
- Kafka in Kubernetes - Marking the coordinator dead for group
- Kafka in Kubernetes Cluster- How to publish/consume messages from outside of Kubernetes Cluster
- Kafka inaccessible once inside Kubernetes/Minikube
- Kafka incremental sticky rebalancing
- Kafka instead of Rest for communication between microservices
- Kafka isolation level implications
- Kafka input to logstash plugin
- Kafka integration tests in Gradle runs into GitHub Actions

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.