Apache Kafka Streaming (50 Qs)•Part 3: Advanced Internals, Scale & Production Architecture (Senior / Lead)
Mitigating Hot Partitions and Data Skew.
senior LevelSystem Design
In Simple Words: If 1 partition receives 100,000 messages/sec while 9 other partitions receive 1,000 messages/sec, the broker hosting that partition experiences disk and CPU saturation, and the assigned consumer falls behind.
Architecture Solutions:
- Key Salting (Compound Keys):
Instead of using just
userId, append a randomized suffix:userId + "_" + random(1, 5). Spreads the user's records across 5 partitions. - Custom Partitioner:
Implement Kafka's
Partitionerinterface in the producer to route high-volume tenants to dedicated isolated topics or partitions, while pooling smaller tenants.