Kafka Architecture
Kafka Architecture
Kafka is a distributed event streaming platform designed for high-throughput, fault-tolerant, durable messaging.
Core Components
Kafka Cluster:
┌─────────────────────────────────────────────────┐
│ Kafka Cluster │
│ ┌─────────┐ ┌─────────┐ ┌─────────┐ │
│ │ Broker 1│ │ Broker 2│ │ Broker 3│ │
│ └─────────┘ └─────────┘ └─────────┘ │
│ ↑ ↑ ↑ │
│ ┌─────────────────────────────────────┐ │
│ │ ZooKeeper / KRaft │ │
│ └─────────────────────────────────────┘ │
└─────────────────────────────────────────────────┘
↑ ↓
Producers Consumers
Key Concepts
| Component | Description |
|---|---|
| Broker | Kafka server storing data |
| Topic | Category/feed of messages |
| Partition | Topic subdivision for parallelism |
| Offset | Unique message ID within partition |
| Producer | Publishes messages to topics |
| Consumer | Reads messages from topics |
| Consumer Group | Group of consumers load-balancing reads |
Data Flow
Producer → Topic → Partition → Consumer Group → Consumer
1. Producer sends message to topic
2. Topic routes to partition (based on key hash)
3. Message appended to partition log
4. Consumer Group assigns partitions to consumers
5. Consumer reads messages in order
Topics and Partitions
Topics and Partitions
Topic Structure
Topic: user-events
Partition 0: [msg0, msg3, msg6, msg9, ...]
Partition 1: [msg1, msg4, msg7, msg10, ...]
Partition 2: [msg2, msg5, msg8, msg11, ...]
Each partition is an ordered, immutable sequence of messages
Partitioning Strategy
// Key-based partitioning (default)
ProducerRecord<String, String> record = new ProducerRecord<>(
"user-events", // topic
"user-123", // key (used for partitioning)
eventJson // value
);
// Partition = hash(key) % num_partitions
// Round-robin partitioning (no key)
producer.send(new ProducerRecord<>("logs", logMessage));
Partition Configuration
# server.properties
num.partitions=6
# Topic-specific
kafka-topics.sh --create --topic orders \
--partitions 12 \
--replication-factor 3
Partitioning Best Practices
| Consideration | Recommendation |
|---|---|
| Number of partitions | 2-3x target throughput |
| Key selection | Use related entity ID |
| Ordering | Guaranteed within partition |
| Scalability | Can add partitions (not reduce) |
Partition Limits
Partition constraints:
- Each partition on single broker
- Replication across brokers
- More partitions = more file handles
- More partitions = longer recovery time
Rule of thumb: < 4000 partitions per broker
Consumer Groups
Consumer Groups
How Consumer Groups Work
Consumer Group: order-processor
Topic: orders (3 partitions)
Partition 0 → Consumer 1
Partition 1 → Consumer 2
Partition 2 → Consumer 3
Each partition consumed by exactly ONE consumer in group
Scaling Consumers
3 Partitions, 3 Consumers (optimal):
P0 → C1
P1 → C2
P2 → C3
3 Partitions, 6 Consumers (over-provisioned):
P0 → C1
P1 → C2
P2 → C3
C4 → idle
C5 → idle
C6 → idle
Max parallelism = number of partitions
Consumer Group Example
Properties props = new Properties();
props.put("group.id", "analytics-group");
props.put("bootstrap.servers", "kafka:9092");
props.put("enable.auto.commit", "false");
KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
consumer.subscribe(Arrays.asList("user-events"));
while (true) {
ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100));
for (ConsumerRecord<String, String> record : records) {
processEvent(record.value());
}
consumer.commitSync(); // Commit offsets
}
Offset Management
Offset Tracking:
Partition 0: [msg0, msg1, msg2, msg3, msg4]
↑
Committed offset = 2
(msg0, msg1 processed)
(msg2, msg3, msg4 pending)
Commit strategies:
- Auto-commit: Periodic background commit
- Sync commit: Blocking commit after processing
- Async commit: Non-blocking commit with callback
Practice Problems
Design a scalable Kafka system. Cover high-level architecture, data model, and API design.
Solution
// Complete system design:
// - Functional + Non-functional requirements
// - Capacity estimation
// - Data model (SQL/NoSQL choice)
// - API endpoints
// - Component architecture
// - Scaling strategy
// - Monitoring & reliabilityHow would you scale Kafka to handle 10x the current load? Identify bottlenecks and solutions.
Solution
// Scaling approach:
// 1. Load balancing
// 2. Database sharding/replication
// 3. Cache layer (Redis)
// 4. CDN for static assets
// 5. Async processing (queues)
// 6. Microservices decompositionAnalyze potential failure modes for Kafka and design mitigation strategies.
Solution
// Failure mitigation:
// 1. Redundancy (multi-AZ)
// 2. Circuit breakers
// 3. Retry with backoff
// 4. Dead letter queues
// 5. Health checks
// 6. Graceful degradationQuiz
1. What is the main purpose of Kafka partitions?
2. In a consumer group, how many consumers can read from one partition?
3. What determines which partition a message goes to?
4. What happens if you have more consumers than partitions?
5. Why is ordering only guaranteed within a partition?
Flashcards
Question
What is a Kafka partition?
Click to reveal answer
Answer
A subdivision of a topic that stores messages in an ordered, immutable sequence, enabling parallel processing
Question
What is a consumer group?
Click to reveal answer
Answer
A group of consumers that load-balance message consumption, with each partition consumed by exactly one consumer
Question
How is message ordering guaranteed?
Click to reveal answer
Answer
Only within a partition. Messages with same key go to same partition, maintaining order for that key.
Question
What is a Kafka offset?
Click to reveal answer
Answer
A unique sequential ID for each message within a partition, used to track consumer progress
Question
Max parallelism in consumer group?
Click to reveal answer
Answer
Equal to number of partitions - more consumers than partitions leaves extras idle
Revision Notes
Key Takeaways
- 1.Kafka provides high-throughput, durable, distributed event streaming
- 2.Partitions enable parallelism - ordering guaranteed within partition only
- 3.Consumer groups load-balance: each partition consumed by one consumer
- 4.Max parallelism equals number of partitions
- 5.Message key determines partition routing
Interview Tips
- •Explain partition as unit of parallelism
- •Discuss consumer group rebalancing
- •Know when to increase partitions
- •Explain offset management strategies
Cheat Sheet
Cheat Sheet: Kafka
Architecture
- Broker: Kafka server
- Topic: Message category
- Partition: Topic subdivision
- Offset: Message sequence ID
Key Points
- Ordering guaranteed within partition
- Max parallelism = num partitions
- Consumer group balances load
- Messages immutable in log
Partitioning
- Key-based: hash(key) % partitions
- Same key → same partition
- Add partitions (can't reduce)
Consumer Groups
- Each partition → 1 consumer
- More consumers than partitions → idle
- Track offsets for progress