Publish-subscribe messaging डिज़ाइन: fanout और काम बाँटने में अंतर करें
Publish-subscribe (pub/sub) ऐसा पैटर्न है जिसमें publisher संदेश को सीधे एक recipient को भेजने के बजाय किसी topic या exchange पर प्रकाशित करता है और subscribers अपनी अलग subscription route से उसे पाते हैं। यह तब उपयोगी है जब स्वतंत्र प्रणालियों को एक ही event पर प्रतिक्रिया देनी हो। पहले तय करें कि हर subscriber को अपनी प्रति चाहिए या pool में से एक worker हर task संभालेगा; ये अलग delivery मॉडल हैं।
Publish-subscribe (pub/sub) ऐसा पैटर्न है जिसमें publisher संदेश को सीधे एक recipient को भेजने के बजाय किसी topic या exchange पर प्रकाशित करता है और subscribers अपनी अलग subscription route से उसे पाते हैं। यह तब उपयोगी है जब स्वतंत्र प्रणालियों को एक ही event पर प्रतिक्रिया देनी हो। पहले तय करें कि हर subscriber को अपनी प्रति चाहिए या pool में से एक worker हर task संभालेगा; ये अलग delivery मॉडल हैं।
उदाहरण के लिए, OrderPlaced event को order history, inventory reservation और analytics तक अलग-अलग पहुँचना पड़ सकता है। लेकिन अगर कई worker inventory reservation tasks बाँटते हैं, तो work queue हर task को एक worker को दे।
Pub/sub बनाम work queue
Pub/sub में एक event कई logical subscribers को भेजा जाता है। हर subscriber अपनी गति से process कर सकता है और failures का अलग retry कर सकता है। Work queue में कई consumers एक ही task set बाँटते हैं; consumers बढ़ाने से हर task सबको copy नहीं होता।
| आवश्यकता | उपयुक्त मॉडल | डिज़ाइन प्रश्न |
|---|---|---|
| Inventory, analytics और notifications हर order event process करें | प्रति subscriber queue या consumer group | क्या हर service स्वतंत्र रूप से देर या retry कर सकती है? |
| Pool का एक worker कोई खास email भेजे | Shared work queue या एक consumer group | अस्पष्ट acknowledgement के बाद duplicate task सुरक्षित कैसे होगा? |
| नया service पुराने events भी पढ़े | अपनी position वाला retained log | Retention और replay range क्या है? |
| Subscriber बंद होने पर भी events बचें | Durable queue या retained stream | Subscription से पहले प्रकाशित events भी चाहिए? |
Subscriber कहलाने से persistence की गारंटी नहीं होती। Broker अस्थायी subscription के संदेश सहेज न पाए या publish के समय route मौजूद न हो। RabbitMQ का fanout tutorial log broadcast दिखाता है जहाँ हर सक्रिय receiver queue bind करता है और कोई queue bound न हो तो संदेश हट सकता है। टिकाऊ business events के लिए product docs के अनुसार प्रति-subscriber durable queues, message persistence, publish confirms, permissions और retention जाँचें।
उत्पाद subscriber routes कैसे दिखाते हैं
**RabbitMQ:** Producers queue में सीधे नहीं, exchange पर publish करते हैं। Fanout exchange हर bound queue में भेजता है, इसलिए independent subscribers को अलग queues चाहिए। एक ही queue के consumers काम बाँटते हैं। Server द्वारा नामित temporary queue live log सुनने के लिए है, offline retention के लिए नहीं।
**Amazon SNS और SQS:** SNS topic पर प्रकाशित संदेश कई subscribed SQS queues में fanout हो सकता है। हर queue अपनी प्रति रखती है ताकि consumer बाद में polling करे। SNS push और SQS storage/polling की भूमिकाएँ अलग हैं; permissions और subscriptions configure करें तथा प्रत्येक consumer की failures और retries अलग संभालें।
**Apache Kafka 4.4:** कई consumer groups एक topic को स्वतंत्र रूप से subscribe कर सकते हैं। समान `group.id` वाले instances partitions बाँटते हैं, जो work sharing जैसा है; अलग groups logical subscribers की तरह records अलग पढ़ते हैं। नया consumer पुराने events पढ़ पाएगा या नहीं, यह शुरुआती offset, retention और topic में शेष records पर निर्भर करता है।
“Topic” नाम देखकर अर्थ न मानें। जाँचें subscriber कैसे पहचाना जाता है, messages copy होते हैं या बाँटे जाते हैं, subscriber अनुपस्थित हो तो क्या बचता है, replay कितना संभव है और acknowledgement failure के बाद duplicates कैसे सँभलते हैं।
लागू करने से पहले चेकलिस्ट
**Logical subscribers तय करें:** अलग जिम्मेदारी या retry policy वाले किन services को स्वतंत्र route चाहिए?
**Subscription lifecycle चुनें:** Live broadcast या outage में संदेश बचाने वाली durable subscription? बनने और हटने का समय लिखें।
**Failure boundaries तय करें:** Retention, backlog limits, ACK/offset समय, retry और DLQ नीति निर्धारित करें। हर subscriber का backlog अलग रखें।
**Duplicates सुरक्षित बनाएँ:** हर subscriber स्थिर event ID या business key से repeated delivery सँभाले। Pub/sub end-to-end duplicate-free processing की गारंटी नहीं है।
**Contract version करें:** Event names, field meanings, optional fields और compatibility rules दर्ज करें।
**Subscribers अलग monitor करें:** केवल publish volume नहीं, प्रति subscriber lag, oldest message, failures/redelivery, DLQ और replay state देखें। Personal data या secrets को logs में अनावश्यक रूप से न दोहराएँ।
Test में दो subscribers बनाएँ और एक को रोकें। फिर उसे चालू करके उसकी retention/replay नीति के अनुसार recovery जाँचें। यह भी देखें कि उसका failure दूसरे को नहीं रोकता और redelivery business result दोहराती नहीं। यह सुझाई गई प्रक्रिया है, किए गए test का दावा नहीं।
स्रोत और दायरा
RabbitMQ, “Publish/Subscribe tutorial”: https://www.rabbitmq.com/tutorials/tutorial-three-python
AWS, “Fanout Amazon SNS notifications to Amazon SQS queues for asynchronous processing”: https://docs.aws.amazon.com/sns/latest/dg/sns-sqs-as-subscriber.html
Apache Kafka 4.4 `KafkaConsumer` API, “Consumer Groups and Topic Subscriptions”: https://kafka.apache.org/44/javadoc/org/apache/kafka/clients/consumer/KafkaConsumer.html
उत्पाद व्यवहार उद्धृत दस्तावेज़ों तक सीमित है। Subscriber isolation और checklist delivery needs से जुड़ी design guidance है।