Apache Kafka डिज़ाइन गाइड: partition, consumer group और offset चुनना
Apache Kafka एक event-streaming platform है जो events को topic के partition log में लिखता है और consumers को offset आगे बढ़ाकर पढ़ने देता है। कोई record केवल consume हो जाने से हटता नहीं है। Topic retention तय करता है कि डेटा कितने समय तक रहेगा; इसलिए throughput के साथ partition key, ordering का दायरा, consumer group, offset commit और retention सीमा भी डिज़ाइन करें।
Apache Kafka एक event-streaming platform है जो events को topic के partition log में लिखता है और consumers को offset आगे बढ़ाकर पढ़ने देता है। कोई record केवल consume हो जाने से हटता नहीं है। Topic retention तय करता है कि डेटा कितने समय तक रहेगा; इसलिए throughput के साथ partition key, ordering का दायरा, consumer group, offset commit और retention सीमा भी डिज़ाइन करें।
Kafka का storage और read मॉडल
Producer, key, value, timestamp और वैकल्पिक headers वाले records topic में लिखता है। Topic कई partitions में बँटा होता है और हर record किसी एक partition log के अंत में जुड़ता है। Partitions distributed storage और parallel processing की सुविधा देते हैं। समान key वाले events को एक ही partition में भेजा जा सकता है; क्रम केवल उस partition में सुनिश्चित है, पूरे topic में नहीं।
Kafka record को पढ़ने पर हटाता नहीं है। Topic retention सेटिंग अंततः पुराने records हटाती है और अलग-अलग consumers उसी डेटा को स्वतंत्र रूप से फिर पढ़ सकते हैं। Replay इसलिए retention और उपलब्ध records पर निर्भर है, न कि केवल इस पर कि कोई दूसरा consumer उन्हें पढ़ चुका है।
| ज़रूरत | Kafka डिज़ाइन विकल्प | जाँचने की सीमा |
|---|---|---|
| ऊँचा throughput और क्षैतिज parallelism | Topic को partitions में बाँटें | Partition संख्या और key वितरण parallelism को कैसे प्रभावित करते हैं? |
| ग्राहक या खाते के events का क्रम | Business key को record key बनाएँ | क्रम partition के भीतर है; असमान key एक partition पर दबाव डाल सकती है |
| कई सेवाएँ एक ही डेटा स्वतंत्र रूप से पढ़ें | हर सेवा का अलग consumer group | हर group के offset, replay और retention कैसे चलेंगे? |
| कई worker काम बाँटें | एक ही consumer group रखें | हर partition समूह के एक सदस्य को मिलता है; partitions से अधिक consumers parallelism नहीं बढ़ाते |
| विफलता के बाद फिर शुरू करना | Processing result के अनुरूप offset commit करें | पहले commit करने से काम छूट सकता है; बाद में करने से दोहराव हो सकता है |
Consumer group और offset साथ डिज़ाइन करें
एक ही group.id वाले consumers partitions बाँटते हैं। हर partition समूह के एक consumer को मिलता है, इसलिए group साझा काम करने वाले worker pool की तरह होता है। अलग groups सेवाओं को topic records स्वतंत्र रूप से पढ़ने देते हैं। Consumer के जुड़ने/छूटने या partition assignment बदलने पर rebalance होता है, जिससे assignment और throughput बदल सकते हैं।
हर partition में record offset होते हैं। Consumer की अगला record पढ़ने की current position और restart पर लौटने की committed position होती है। Automatic commit समय-समय पर position सहेजता है; manual commit में application तय करती है कि record कब processed माना जाए।
यदि application record process करके offset commit करती है, तो बीच की failure restart के बाद वही record दोबारा process करा सकती है। यदि पहले commit किया और परिणाम सहेजने से पहले failure हुई, तो record छूट सकता है। External database update के लिए परिणाम और offset को एक transaction में सहेजने पर विचार करें; वरना idempotency या outbox/inbox pattern अपनाएँ।
“Exactly-once” की सीमा समझें
Kafka retry के दौरान log में duplicate entries रोकने के लिए idempotent producer और output records तथा consumed offsets को Kafka में atomically लिखने के लिए transactions देता है। सही configuration और processing pattern से Kafka के भीतर input-to-output flow atomic हो सकता है। इससे external database, payment API या email system में प्रभाव अपने-आप सिर्फ एक बार नहीं होता। External state को offset के साथ समन्वित करें या duplicates को सुरक्षित संभालें।
Producer durability आवश्यक acknowledgements, replication की स्थिति और अन्य settings पर भी निर्भर है। “Kafka durable है” को failure में शून्य loss या हर consumer के सफल processing की सार्वभौमिक गारंटी न मानें। डेटा के महत्व के अनुसार replication, producer ACK, consumer commit, retention और recovery की पुष्टि करें।
अपनाने से पहले
**Event contract तय करें।** Topic, key, value schema, timestamp, compatibility rules, owner और personal data की मौजूदगी लिखें।
**Partition और key डिज़ाइन करें।** पहले आवश्यक ordering scope चुनें, फिर key skew, hot partition, लक्ष्य parallelism और partition संख्या बदलने का प्रभाव देखें।
**Business responsibility के अनुसार groups अलग रखें।** स्वतंत्र subscriber को अलग group दें; एक group केवल उसी काम को बाँटने वाले workers में साझा करें।
**Processing और commit का क्रम तय करें।** Automatic या manual commit चुनें और धीमी processing, rebalance और failure में skip या duplicate रोकने का तरीका लिखें।
**Retention और replay की गणना करें।** Replay अवधि, consumer का अधिकतम downtime, डेटा आकार और retention limit मिलाएँ। Record expire होने पर offset उसे वापस नहीं ला सकता।
**Failure path जाँचें।** Processing से पहले और बाद में consumer रोककर restart position और duplicates देखें। Rebalance और external store में commit failure अलग से जाँचें।
ये डिज़ाइन चरण हैं; Kafka cluster परीक्षण का दावा नहीं। Deployed broker और client version के दस्तावेज़ों से settings और guarantees सत्यापित करें।
स्रोत और दायरा
Apache Kafka 4.1, [Introduction](https://kafka.apache.org/41/getting-started/introduction/)
Apache Kafka 4.1, [Design](https://kafka.apache.org/41/design/design/)
Apache Kafka 4.1.2, [KafkaConsumer API](https://kafka.apache.org/41/javadoc/org/apache/kafka/clients/consumer/KafkaConsumer.html)
Topic, partition, retention और delivery के विवरण यहाँ पढ़े गए Kafka 4.1 docs और client 4.1.2 API तक सीमित हैं। Ordering, replay और delivery configuration, partitions और consumer व्यवहार पर निर्भर करते हैं; अन्य versions या distributions पर लागू करने से पहले जाँचें।