Interview question
How do you ensure message ordering in Kafka? What is partitioning strategy? Kafka में message ordering कैसे ensure करते हैं? Partitioning strategy क्या है?
Answer
Message ordering in Kafka depends on partitions - messages in same partition maintain order. Use message key for ordering guarantee. Partitioning strategy determines throughput vs order: single partition (strict order but slow) vs multiple partitions (parallel but order within partition only).
// Kafka Partitioning and Ordering
// Strategy 1: Use message key for ordering
@Service
public class OrderEventProducer {
@Autowired
private KafkaTemplate<String, OrderEvent> kafkaTemplate;
public void publishOrderEvent(OrderEvent event) {
// Key is customerId - ensures all orders for customer in same partition
kafkaTemplate.send(
'order-events',
event.getCustomerId(), // Key - orders from same customer ordered
event
);
}
}
// Strategy 2: Configure partition count
spring:
kafka:
topic:
order-events:
partitions: 3 # 3 partitions for parallel processing
replication-factor: 1
// Consumer with partition assignment
@Service
public class OrderEventConsumer {
@KafkaListener(
topics = 'order-events',
groupId = 'order-service',
concurrency = '3' # One thread per partition
)
public void consumeOrderEvent(OrderEvent event) {
System.out.println('Consumed: ' + event.getId() +
' from partition');
}
}
// Ordering guarantees in Kafka:
// 1. Single partition (0) -> strict ordering (bottleneck)
// 2. Multiple partitions -> ordering within partition
// 3. Key-based -> messages with same key go to same partition
public class OrderingExample {
public static void main(String[] args) {
// Scenario: Customer orders 3 items
// Order 1: Order -> Payment -> Shipping (must be in order)
// Use customerId as key
// customerId='C001' -> always same partition (e.g., partition 0)
// Order -> Payment -> Shipping guaranteed in order
//
// Meanwhile customerId='C002' -> partition 1
// Parallel processing, but C002's orders also ordered
}
}
// Partitioning best practices:
// 1. Choose key wisely (customer, tenant, account)
// 2. Balance partitions with consumers
// 3. Monitor partition lag
// 4. Consider rebalancing impact
// 5. Test throughput vs ordering requirements
// Tools for monitoring
@Component
public class KafkaMetricsMonitor {
@Autowired
private MeterRegistry meterRegistry;
public void trackConsumerLag(String topic, String group) {
// Monitor lag per partition
// Alert if lag exceeds threshold
}
}Kafka Message Ordering:
Partition में ordering guarantee होती है
Multiple partitions में parallel processing
Strategy:
1. Single partition:
- Strict ordering
- No parallelism (slow)
2. Multiple partitions + keys:
- Order within partition
- Parallel across partitions
- Best approach
Key Selection:
Order के लिए: customerId as key
Orders same customer का always same partition
Guaranteed ordering per customer
Configuration:
partitions: 3 (or more)
concurrency: 3 (matches partitions)
replication-factor: 2+ (HA)
Best Practice:
- Choose key = ordering entity
- Partition count = consumer count
- Monitor lag per partitionWas this answer clear?