Kafka ordering key चुनना: एक entity के records को एक partition में भेजें
Kafka केवल एक partition के भीतर क्रम की गारंटी देता है, पूरे topic में नहीं। Default producer partitioner एक ही key वाले records को key के hash से assign करता है, इसलिए एक entity के updates एक partition में रहकर उसका क्रम बनाए रख सकते हैं। ऐसी key चुनें जो उस business entity को दर्शाए जिसके बदलाव क्रम में रहने चाहिए।
Kafka केवल एक partition के भीतर क्रम की गारंटी देता है, पूरे topic में नहीं। Default producer partitioner एक ही key वाले records को key के hash से assign करता है, इसलिए एक entity के updates एक partition में रहकर उसका क्रम बनाए रख सकते हैं। ऐसी key चुनें जो उस business entity को दर्शाए जिसके बदलाव क्रम में रहने चाहिए।
Key क्या करती है
Kafka partition एक ordered log है। Default रूप से key वाला record उसके hash से map होता है। Key न हो तो producer sticky partition चुनता है, जो batch सीमा पर बदल सकती है। अलग partitions के बीच कुल क्रम नहीं होता। Key records को partition assignment के लिए समूहित करती है; यह business causality, global order या deduplication की गारंटी नहीं देती। Order updates के लिए order_id उपयोगी है; event_id हर घटना में अलग हो सकती है और एक order के records बिखर सकते हैं।
सबसे छोटी ऐसी entity चुनें जिसके events का क्रम जरूरी है, जैसे order, account या device। एक वैश्विक क्रम Kafka की partitioning और parallelism के विपरीत हो सकता है।
सभी producers में stable key को एक जैसे serialize करें, जैसे tenant_id:order_id। Composite keys को अस्पष्टता के बिना encode करें।
Entity क्रम जरूरी हो तो null key से बचें, जब तक custom partitioner का व्यवहार जाँचा न गया हो।
Hot keys देखें। लोकप्रिय entity एक partition को bottleneck बना सकती है। Sharding throughput बढ़ाती है, लेकिन reordering के बिना single-partition order खो जाता है।
Partition संख्या बढ़ाने की योजना बनाएँ। Default hash mapping बदलने से एक key के नए records दूसरी partition में जा सकते हैं।
Consumer में भी क्रम बचाएँ। Parallel processing या out-of-order offset commit business effects को पुनःक्रमित कर सकता है।
| रणनीति | लाभ | लागत |
|---|---|---|
| Entity ID | Entity के भीतर क्रम, entities के बीच parallelism | Hot entity bottleneck हो सकती है |
| Event ID | Event पहचान वितरित होती है | एक entity के events सामान्यतः साथ नहीं आते |
| Key नहीं | सरल batching/distribution | Entity के लिए stable partition नहीं |
| Explicit partition | सीधा नियंत्रण | Client को distribution और scaling संभालनी पड़ती है |
डिज़ाइन जाँचें
Producer key और serialized bytes लॉग करें; पुष्टि करें कि एक entity के records एक ही partition में जाते हैं। एक key से क्रमबद्ध test records भेजें और offsets की तुलना करें। Staging में partition विस्तार, consumer parallelism, retries और offset commits देखें। ये सुझाए गए verification कदम हैं, इस लेख के लिए किए गए परीक्षण नहीं।
स्रोत और दायरा
[Apache Kafka Introduction](https://kafka.apache.org/documentation/) — partition क्रम, key partitioning और topic-व्यापी कुल क्रम का अभाव।
[Apache Kafka Producer Configs: partitioner.class](https://kafka.apache.org/41/configuration/producer-configs/) — key पर default hash और key न होने पर sticky partition।
[Apache Kafka Design: Producer load balancing](https://kafka.apache.org/44/design/design/#producer_loadbalancing) — semantic partition keys।
[Apache Kafka Basic Operations](https://kafka.apache.org/44/operations/basic-kafka-operations/) — partition विस्तार key distribution और क्रम की धारणाएँ बदल सकता है।
यह लेख Kafka के default partitioning पर है। Custom partitioner, client versions, producer transactions और consumer processing को deployment में जाँचें। Key अकेले exactly-once business effect या पूरे topic का क्रम सुनिश्चित नहीं करती।