data modeling
topics and partitions
system design
kafka
Data Modeling with Kafka? Topics and Partitions
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Data modeling in Kafka involves organizing your messages, topics, and partitions to support scalability, fault tolerance, and efficient data processing. Here's a detailed guide to understanding and designing Kafka topics and partitions:
Key Concepts in Kafka Data Modeling
- Topic:
- A logical channel to organize and group messages.
- Acts as the primary structure for separating different types of data.
- Topics can represent different datasets, workflows, or business events (e.g., "user-logins", "orders", "payments").
- Partition:
- Each topic is split into partitions, which are independent and ordered logs.
- Partitions allow Kafka to scale horizontally by distributing load across brokers.
- Kafka guarantees message ordering within a single partition but not across partitions.
- Message Key:
- Determines how messages are routed to partitions within a topic.
- Keys allow for logical grouping and ordering of related messages.
Data Modeling Principles
1. Topic Design
- Separate Concerns:
- Use separate topics for different types of data or events.
- Example: A "user-activity" topic for user interactions and an "error-logs" topic for system errors.
- Granularity:
- Topics should represent logical streams of data. Avoid topics that are too fine-grained (e.g., one topic per user) or too coarse-grained (e.g., a single topic for all data).
- Retention and Lifecycle:
- Define retention policies (
retention.ms,retention.bytes) based on how long the data will be needed.
2. Partition Design
- Number of Partitions:
- Determines scalability and parallelism.
- More partitions allow higher throughput but increase metadata overhead and complexity.
- Rule of thumb: Start with a number of partitions equal to 2–3 times the number of expected consumer instances.
- Data Distribution:
- Use keys to route related messages to the same partition.
- Without keys, Kafka uses round-robin partitioning.
- Ordering Requirements:
- If strict ordering is required, ensure all related messages are routed to the same partition by using keys.
3. Key Selection
- Purpose:
- Ensures related messages are sent to the same partition for grouping and ordering.
- Example: Use
userIdas the key in a "user-activity" topic to ensure all events for a single user are in the same partition.
- Avoid Hotspots:
- Poor key distribution can lead to uneven partition loads. Ensure keys are distributed uniformly.
Best Practices for Kafka Data Modeling
- Topic Design
- Use meaningful and self-descriptive topic names (e.g.,
user-signups,product-orders). - Avoid mixing unrelated data types in the same topic.
- Consider future scalability when defining topic structure.
- Partitioning Strategy
- Use keys to partition data logically.
- Choose an appropriate number of partitions based on consumer concurrency and expected throughput.
- Monitor partition usage and rebalance if necessary.
- Retention Policies
- Configure retention periods and sizes for topics based on use case requirements (e.g., event streaming vs. long-term storage).
- Use compacted topics for data deduplication and storing the latest key-value pairs.
- Compression
- Enable compression (
compression.type) for large messages to reduce storage and network overhead.
- Replication
- Use an appropriate replication factor to ensure fault tolerance.
- A higher replication factor increases data durability but adds storage overhead.
Example Data Modeling Use Cases
Use Case 1: User Activity Tracking
- Topic:
user-activity - Partitions: 10
- Key:
userId - Retention Policy: Retain for 7 days
- Reasoning:
- Using
userIdas the key ensures all events for a user are in the same partition, maintaining ordering for that user. - 10 partitions allow for parallel processing by consumers.
Use Case 2: Order Processing System
- Topic 1:
order-created- Events for when orders are created.
- Topic 2:
order-shipped- Events for when orders are shipped.
- Partitions: Based on the expected load (e.g., number of orders per second).
- Keys:
orderId - Retention:
- Long retention for audit logs or replayability.
- Compacted topics for storing the latest order status.
Monitoring and Rebalancing
- Monitoring:
- Use Kafka tools to monitor partition size, consumer lag, and broker load.
- Adjust partitions if there’s an imbalance.
- Rebalancing:
- Use
kafka-reassign-partitions.shto redistribute partitions if needed.
Common Pitfalls
- Too Many Partitions:
- Increases metadata overhead, slowing down Kafka operations.
- Solution: Start with fewer partitions and increase as needed.
- Poor Key Distribution:
- Skewed keys lead to uneven load across partitions.
- Solution: Choose a key with good distribution or use a custom partitioner.
- Improper Retention Settings:
- Retaining unnecessary data increases storage costs.
- Solution: Tailor retention policies to match use case requirements.
Summary
| Aspect | Recommendation |
| Topic Design | Use meaningful names, separate topics for different datasets, and configure appropriate retention. |
| Partitions | Choose based on scalability needs, ensure even distribution, and use keys for ordering. |
| Key Selection | Use logical identifiers for grouping (e.g., userId or orderId) and avoid skewed key distributions. |
| Retention Policies | Configure based on data lifecycle requirements (e.g., compacted topics for the latest state). |
| Monitoring | Regularly monitor broker and partition performance for bottlenecks and imbalances. |
This approach ensures efficient, scalable, and reliable Kafka data modeling tailored to your specific system requirements.
Related reading
- Difference between Redis and Kafka
- Difference between session.timeout.ms and max.poll.interval.ms for Kafka >= 0.10.1
- Difference between stream processing and message processing
- How can I delete a topic in Apache Kafka?
- Distributed System Design Patterns
- How can I send large messages with Kafka (over 15MB)?
- How to view kafka message?
- I am evaluating Google Pub/Sub vs Kafka. What are the differences?

Course
Beginner
27 lessons
10 hours
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.