How partitioning works
When a producer sends a message, the partitioner decides which partition receives it based on:- Message key: If present, used to determine partition assignment
- Partition specification: Explicit partition number in ProducerRecord
- Partitioner logic: Default or custom partitioning algorithm

Partition assignment impactPartitioning affects message ordering, consumer distribution, load balancing, and data locality. Choose your partitioning strategy carefully based on your use case requirements.
Default partitioner behavior
With message keys
When messages have keys, the default partitioner uses a hash-based approach:- Same key always goes to same partition (within same topic configuration)
- Provides ordering guarantees per key
- Distributes load based on key distribution
- Changes when partition count changes
Without message keys
For messages without keys, behavior depends on Kafka version: Kafka < 2.4 (Round-robin partitioner):- Cycles through partitions sequentially
- Each message goes to next partition in sequence
- Poor batching efficiency due to partition spreading
- Sticks to one partition until batch is full
- Switches to different partition for next batch
- Improves batching efficiency and throughput
Sticky partitioner
The sticky partitioner, introduced in Kafka 2.4, improves performance for messages without keys.How sticky partitioner works
- Partition selection: Choose random available partition
- Batch filling: Send all messages to chosen partition until batch fills
- Partition switching: Switch to different partition for next batch
- Repeat process: Continue cycle for optimal batching
Benefits of sticky partitioner
Improved throughput:- Better batch utilization (more messages per batch)
- Reduced network requests
- More efficient compression
- Fewer partially filled batches
- Improved producer buffer usage
- Enhanced broker processing efficiency
- Over time, messages distribute evenly across partitions
- No hot partitions with sustained load
Configuration
Sticky partitioner is enabled by default in Kafka 2.4+:Round-robin partitioner
The original default partitioner (pre-2.4) that cycles through partitions.Configuration
When to use round-robin
- Legacy compatibility: Matching behavior of older Kafka versions
- Strict distribution: When perfectly even distribution is critical
- Partition-level processing: When downstream processing benefits from spread
Round-robin performance impactRound-robin partitioning creates smaller batches and reduces throughput compared to sticky partitioning for keyless messages.
Uniform sticky partitioner
An alternative sticky partitioner that considers partition leadership and availability.Configuration
Benefits
- Broker load balancing: Considers partition leader distribution
- Availability awareness: Avoids unavailable partitions
- Network optimization: Reduces cross-broker traffic
Custom partitioner implementation
Create custom partitioners for specific business requirements.Implementing a custom partitioner
Registering custom partitioner
Partitioning strategies
Key-based partitioning
Use case: When you need ordering guarantees per key- Ordering guarantees within key
- Data locality for key-based processing
- Predictable partition assignment
- Key distribution affects load balancing
- Partition count changes break key-to-partition mapping
- Hot keys can create hot partitions
Explicit partition assignment
Use case: When you need complete control over partition assignment- Complete control over placement
- Can implement complex routing logic
- Useful for testing and debugging
- Must handle partition availability
- Need to track partition count changes
- Bypasses partitioner logic entirely
Random/sticky partitioning
Use case: When you don’t need ordering and want optimal throughput- Optimal batching and throughput
- Even load distribution over time
- Simple implementation
- No ordering guarantees
- Random distribution per batch
Partition count considerations
Impact of partition changes
Adding partitions affects key-to-partition mapping:- Same key may go to different partition
- Breaks ordering guarantees temporarily
- May create temporary hotspots
Best practices for partition counts
- Plan ahead: Choose partition count based on expected load
- Consider consumers: Partition count limits consumer parallelism
- Think about keys: More partitions = better key distribution
- Monitor hotspots: Watch for uneven partition usage
- Avoid frequent changes: Partition changes disrupt key distribution
Performance optimization
Maximizing throughput
For highest throughput with keyless messages:Balancing load
For even load distribution:Custom business logic
For specific routing requirements:Monitoring partitioner behavior
Key metrics to track
Partition distribution:JMX metrics
Monitoring partition hotspots
Track broker-level metrics to identify hotspots:Common partitioning patterns
Geographic partitioning
Route messages based on geographic regions:Time-based partitioning
Route messages based on time periods:Priority-based partitioning
Route high-priority messages to specific partitions:Best practices
Production recommendations
- Use sticky partitioner: Default choice for keyless messages (Kafka 2.4+)
- Consider key distribution: Ensure keys distribute evenly across partitions
- Monitor partition usage: Watch for hotspots and uneven distribution
- Plan partition count: Choose based on expected throughput and consumer count
- Test custom partitioners: Validate custom logic under load
Troubleshooting partitioning issues
Uneven load distribution:- Check key distribution patterns
- Monitor per-partition message rates
- Consider custom partitioner for better distribution
- Switch from round-robin to sticky partitioner
- Increase
linger.ms
to allow batch filling - Monitor batch utilization metrics
- Verify key-based partitioning for ordering requirements
- Check if partition count changed recently
- Consider using single partition for strict ordering
- Analyze key distribution
- Implement custom partitioner for better balance
- Consider increasing partition count
Partition count changesChanging partition count breaks key-to-partition mapping and can disrupt ordering guarantees. Plan partition counts carefully and avoid frequent changes.
Testing partitionersAlways test custom partitioners with realistic data distributions and load patterns. Partition assignment affects performance significantly.