Change Data Capture (CDC): डेटाबेस के बदलाव दूसरे सिस्टम तक कैसे पहुँचाएँ
Change Data Capture (CDC, डेटा बदलाव कैप्चर) उन insert, update और delete कार्रवाइयों को पहचानता है जिन्हें डेटाबेस ने commit किया है, और फिर उन्हें दूसरे सिस्टम तक पहुँचाता है। PostgreSQL में एक तरीका write-ahead log (WAL) को logical रूप से decode करना और replication slot से पढ़ने की स्थिति बनाए रखना है। Debezium ऐसा एक कार्यान्वयन है जो इस तरीके को शुरुआती snapshot और Kafka Connect event stream के साथ जोड़ता है। CDC, केवल मौजूदा rows को बार-बार query करने के बजाय बदलाव भेजकर search index या analytics store को अपडेट कर सकता है। लेकिन duplicate events, schema changes और log retention की योजना न हो तो downstream data स्रोत से अलग हो सकता है या स्रोत की disk भर सकती है।
यह लेख PostgreSQL 18 के logical decoding और Debezium के वर्तमान PostgreSQL connector दस्तावेज़ों के आधार पर relational database CDC समझाता है; Kafka को उदाहरण के destination के रूप में लिया गया है। हर CDC उत्पाद PostgreSQL WAL, Debezium या Kafka का उपयोग नहीं करता। नीचे बताए गए व्यवहार और सीमाएँ उद्धृत दस्तावेज़ों के दायरे तक सीमित हैं।
CDC किस समस्या को हल करता है?
मान लें कि orders database को search service और analytics store भी अपडेट करना है। यदि application code सीधे हर store में लिखता है, तो उसे partial success संभालना होगा: एक write सफल हो सकता है और दूसरा विफल। पूरी orders table को नियमित रूप से query करना, data बढ़ने के साथ महँगा होता जाता है और query interval जितनी देरी जोड़ता है। केवल last-modified time के आधार पर polling करने से बीच के बदलाव या deletions भी छूट सकते हैं, जब तक change history या deletion markers अलग से न रखे जाएँ।
CDC उन row changes को track करता है जिन्हें database ने commit किया है और उन्हें consumers के लिए events के रूप में उपलब्ध कराता है। Consumer search document अपडेट कर सकता है, reporting table भर सकता है या data को दूसरे store में replicate कर सकता है। बदलाव application की नियमित queries से स्वतंत्र होकर पहुँचते हैं। फिर भी CDC मुख्यतः data changes देता है; यह ज़रूरी नहीं कि वह “order approved” जैसी business event हो। यदि एक row कई बार बदली गई है, तो consumer को बदलावों के उस क्रम का अर्थ समझना होगा।
PostgreSQL का बदलाव event कैसे बनता है?
PostgreSQL storage layer के बदलाव WAL में दर्ज करता है। Logical decoding WAL से committed table changes निकालकर application के पढ़ने योग्य रूप में बदलता है, जैसे tuples की stream। Logical replication slot उस change stream की position संभालता है जिसे client पढ़ सकता है। PostgreSQL 18: Logical Decoding Concepts
Debezium PostgreSQL connector के साथ प्रवाह इस तरह हो सकता है:
Application transaction
→ PostgreSQL tables और WAL
→ Logical decoding और replication slot
→ Debezium connector / Kafka Connect
→ हर table के change events वाले Kafka topics
→ Search index, analytics store या अन्य consumersनया connector यह मानकर नहीं चल सकता कि पुराना WAL database का पूरा इतिहास रखता है। इसलिए Debezium पहले एक consistent initial snapshot लेता है, फिर उससे जुड़ी सटीक WAL position से stream जारी रखता है। यह सीमा snapshot बनते समय commit हुए बदलावों को छूटने से बचाती है। इसके बाद connector INSERT, UPDATE और DELETE के row-level events देता है। PostgreSQL 10 और बाद के संस्करणों में logical decoding plugin pgoutput शामिल है, इसलिए इस विकल्प के लिए अलग plugin library की ज़रूरत नहीं होती। Debezium: PostgreSQL Connector
Snapshot और streaming को साथ में क्यों डिज़ाइन करें?
Snapshot किसी एक समय की स्थिति बताता है, लेकिन उसे पढ़ते समय हुए बदलाव नहीं दिखाता। यदि consumer शुरुआती बदलावों के बाद शुरू हुआ, तो केवल stream से शुरुआती स्थिति फिर बनाना संभव नहीं भी हो सकता। दोनों को मिलकर एक consistent starting point तय करना चाहिए: snapshot आरंभिक rows देता है और stream उससे जुड़ी WAL position से आगे चलता है। तब consumer शुरुआती view बना सकता है और बाद में उसे अपडेट करता रहता है।
उदाहरण के लिए, कल्पना करें कि orders table को search index में copy किया जा रहा है। Snapshot के दौरान कोई order बदल सकता है। यदि consumer order या resume position को ध्यान में रखे बिना किसी नए event के बाद snapshot लागू करता है, तो पुरानी value वापस लिखी जा सकती है। इसलिए समझें कि connector snapshot को कैसे चिह्नित करता है, stream कहाँ से resume होती है और destination संबंधित events को कैसे संभालता है। यह केवल समझाने के लिए काल्पनिक स्थिति है, किया गया test नहीं।
CDC अपने आप schema compatibility की गारंटी भी नहीं देता। Consumers table structure में बदलाव सह पाएँगे या नहीं, यह connector के configuration और capabilities पर निर्भर करता है। Debezium PostgreSQL connector के दस्तावेज़ बताते हैं कि DDL changes को schema-change events के रूप में नहीं भेजा जाता। यदि टीम को schema changes आगे भेजने हैं, तो उन्हें अलग से संभालना होगा।
Duplicate और क्रम को कैसे संभालें?
Failure या resume के बाद capture system events को फिर से deliver कर सकता है। Debezium दस्तावेज़ “at least once” delivery बताते हैं, इसलिए पहले process हुआ event दोबारा आ सकता है। Consumer processing को idempotent बनाए — यानी एक ही event दो बार लागू करने पर वही अंतिम स्थिति मिले जो एक बार लागू करने पर मिलती — या स्थिर ID से duplicates पहचानकर अनदेखा करे। Debezium: PostgreSQL Connector
Search index के लिए एक तरीका है कि primary key के अनुरूप document को प्राप्त स्थिति से replace किया जाए और delete event आने पर document हटा दिया जाए। यह तब आसान होता है जब हर key के events लगातार एक ही क्रम में लागू हों। यदि कई consumers, partitions या recovery processes शामिल हों, तो global order मानने के बजाय वास्तविक ordering और partitioning guarantees जाँचें।
Deduplication और reconciliation अलग बातें हैं। Event ID एक ही event को दो बार process होने से रोक सकती है, पर पहले से drift हुए destination को अपने आप ठीक नहीं करती। तय करें कि destination की तुलना या पुनर्निर्माण कैसे होगा और failure के बाद बिना अपुष्ट बदलाव छोड़े कैसे resume करेंगे।
Replication slots और WAL की निगरानी क्यों करें?
Replication slot वह position रखता है जिसकी consumer को आगे जारी रखने के लिए ज़रूरत होती है। यदि consumer पीछे रह जाए या रुक जाए, तो PostgreSQL को वे WAL segments रोककर रखने पड़ सकते हैं जिनकी प्राप्ति की पुष्टि नहीं हुई है। इससे आगे जारी रखने की संभावना बनी रहती है, पर disk उपयोग बढ़ सकता है। PostgreSQL का pg_replication_slots view slot और retention की जानकारी दिखाता है, जिसमें restart_lsn, confirmed_flush_lsn, wal_status और configuration के अनुसार safe_wal_size शामिल हैं। PostgreSQL 18: pg_replication_slots View
जिम्मेदार operations में consumer lag, disk space, slots की स्थिति और गतिविधि तथा configured retention limits की निगरानी शामिल है। Logical decoding के लिए wal_level=logical आवश्यक है, लेकिन retention capacity बढ़ाना lag का कारण खोजने का विकल्प नहीं है। अनुपयोगी slot हटाने से संबंधित WAL retention मुक्त हो सकती है; लेकिन उस slot पर निर्भर consumer पुरानी position से resume नहीं कर पाएगा। नया slot बनाकर snapshot फिर से लेना पड़ सकता है। PostgreSQL 18: Logical Decoding Examples · PostgreSQL 18: WAL Runtime Configuration
Slot असीमित archive नहीं है। PostgreSQL के अनुसार slots client connection से स्वतंत्र रूप से बने रहते हैं और resources रोक सकते हैं। इसलिए छोड़े गए slots पर नज़र रखें और storage incident से पहले recovery प्रक्रिया तय करें।
CDC कब business events का विकल्प नहीं है?
CDC पूछता है, “कौन-सी rows बदलीं?” Business event पूछता है, “व्यावसायिक क्षेत्र में कौन-सी महत्वपूर्ण बात हुई?” Order row का update कई technical fields बदल सकता है, लेकिन वह किसी एक business decision के अनुरूप न हो। दूसरी ओर, “payment accepted” जैसी event के लिए ऐसा data या अर्थ चाहिए हो सकता है जो केवल दो rows के अंतर से नहीं मिलता।
यदि किसी service को database बदलते समय business event भी प्रकाशित करनी है, तो database और message broker में अलग-अलग लिखने से dual-write जोखिम बनता है: एक काम सफल हो सकता है और दूसरा विफल। Outbox pattern में application business change वाली उसी transaction के भीतर event को outbox table में लिखता है, फिर CDC उस row को भेजता है। Debezium का Outbox Event Router table changes को consumers के events में बदल सकता है। स्थिर key या event ID duplicates संभालने में मदद करती है। Debezium: Outbox Event Router
यह तरीका application में event को भरोसेमंद ढंग से दर्ज करने और बाद में उसे पहुँचाने के समय को अलग करता है। इससे destination के प्रभाव अपने आप atomic नहीं बनते: हर consumer को फिर भी retries, duplicates और failures संभालने होंगे। Secondary views को भरने के लिए business tables से CDC उपयोगी रहती है; स्पष्ट business facts का contract चाहिए तो outbox एक विकल्प है।
CDC अपनाने से पहले क्या जाँचें?
Implementation चुनने से पहले संबंधित environment में ये बातें जाँचें:
**Source और scope:** क्या database, उसका version और ज़रूरी tables या columns समर्थित हैं? Deletions और schema changes की नीति क्या है?
**शुरुआत:** क्या initial snapshot चाहिए? क्या टीम के पास उसे लेने और फिर सही position से stream जारी करने का समय और capacity है?
**Destination:** क्या events उपयुक्त key के आधार पर partition होते हैं? Consumer insert, update और delete को कैसे लागू करता है?
**Recovery:** क्या processing idempotent है या duplicates पहचाने जाते हैं? Data loss या बहुत अधिक lag के बाद destination का reconciliation कैसे होगा?
**Operations:** Slots, lag और disk की निगरानी कौन करता है? Invalid, deleted या abandoned slots के लिए क्या alerts और procedures हैं?
**Business meaning:** क्या rows replicate करनी हैं या स्थिर business facts प्रकाशित करने हैं? दूसरे मामले में outbox contract को बेहतर ढंग से परिभाषित कर सकता है।
Debezium दस्तावेज़ Kafka topics बनाने और उनसे जुड़ी settings का भी वर्णन करते हैं। Cluster configuration और permissions जाँचें; यह न मानें कि topics अपने आप इच्छित तरीके से बनेंगे। Kafka Connect: Topic Auto-Creation
दायरा और स्रोत
इस लेख का implementation विवरण PostgreSQL 18 logical decoding और 23 नवंबर 2026 को देखे गए Debezium PostgreSQL connector के stable दस्तावेज़ों तक सीमित है। Kafka उदाहरण destination है। बताए गए commands और settings दस्तावेज़ी संदर्भ हैं; इस लेख के लिए कोई database, connector या scenario चलाया या test नहीं किया गया। लागू करने से पहले अपने deployment के versions और parameters जाँचें।
[PostgreSQL 18: Logical Decoding Concepts](https://www.postgresql.org/docs/18/logicaldecoding-explanation.html)
[PostgreSQL 18: Logical Decoding Examples](https://www.postgresql.org/docs/18/logicaldecoding-example.html)
[PostgreSQL 18: WAL Runtime Configuration](https://www.postgresql.org/docs/18/runtime-config-wal.html)
[PostgreSQL 18: `pg_replication_slots` View](https://www.postgresql.org/docs/18/view-pg-replication-slots.html)
[Debezium: PostgreSQL Connector](https://debezium.io/documentation/reference/stable/connectors/postgresql.html)
[Debezium: Outbox Event Router](https://debezium.io/documentation/reference/transformations/outbox-event-router.html)
[Debezium: Kafka Connect Topic Creation](https://debezium.io/documentation/reference/stable/configuration/topic-auto-create-config.html)