Episode 3 : DataGaav mein MongoDB ka Jaadu: Locking, Transactions, aur Replica Sets


DataGaav mein ab bacche thode aur samajhdar ho gaye the. Mongo Baba ne decide kiya ki ab unhe kuch aur advanced jaadu sikhaya jaaye. Aaj ka jaadu tha Locking, Transactions, aur Replica Sets ke concepts ko samajhna.

Episode 3 : DataGaav mein MongoDB ka Jaadu: Locking, Transactions, aur Replica Sets


1. Locking aur Transactions ka Jaadu

Mongo Baba ne sabse pehle bacchon ko Locking aur Transactions ka jaadu sikhaya. Yeh concepts un situations mein helpful hote hain jahan tumhe ensure karna hota hai ki data consistency maintained rahe.

Locking

Locking ka matlab hai ki jab tum kisi document ko modify karte ho, toh us document par ek lock lag jaata hai taaki koi aur us document ko simultaneously modify na kar sake. MongoDB mein locking at the document level hota hai, yaani:

  • Example: Mano ki tumhare paas ek "students" collection hai, aur tum ek document ko update kar rahe ho:

    db.students.updateOne( { name: "Ravi" }, { $set: { marks: 85 } } );
    Jab yeh update operation chal raha hoga, tab sirf "Ravi" ke document par lock lagega, baki saare documents freely accessible rahenge. Yeh ensure karta hai ki operations fast aur efficient rahen.

Transactions

Transactions ek aur powerful feature hai jo tumhe multiple operations ko ek saath group karne ki permission deta hai. Agar yeh saare operations successfully complete ho gaye, toh transaction commit ho jaati hai, warna rollback ho jata hai.

  • Example: Imagine karo tumhare paas "accounts" collection hai aur tumhe do accounts ke beech paise transfer karne hain:

    const session = db.getMongo().startSession(); session.startTransaction(); try { db.accounts.updateOne( { name: "Anu" }, { $inc: { balance: -100 } }, { session } ); db.accounts.updateOne( { name: "Ravi" }, { $inc: { balance: 100 } }, { session } ); session.commitTransaction(); } catch (e) { session.abortTransaction(); } finally { session.endSession(); }
    Is example mein, agar dono updates successfully complete ho jaate hain, toh transaction commit ho jaayegi, otherwise rollback ho jaayega, aur koi bhi changes nahi honge. Yeh ensure karta hai ki data hamesha consistent rahe.

2. MongoDB ke Primary aur Secondary Replica Sets ka Jaadu

Mongo Baba ne fir bacchon ko MongoDB ke Replica Sets ka jaadu dikhaya. Yeh feature data redundancy aur high availability ko ensure karta hai. MongoDB mein ek cluster hota hai jo multiple nodes se mil kar bana hota hai, aur in nodes mein primary aur secondary nodes hote hain.

Primary Replica Set

Primary node wo hoti hai jo saare write operations ko handle karti hai. Saare updates, inserts, aur deletes pehle primary node par hote hain. Primary node ke paas data ka latest version hota hai.

  • Example: Imagine karo tumhare paas ek MongoDB cluster hai jisme 3 nodes hain: Node A (Primary), Node B (Secondary), aur Node C (Secondary). Jab tum data insert karte ho:

    db.collection.insertOne({ name: "Anu", age: 25 });
    Yeh data pehle Node A (Primary) par store hota hai.

Secondary Replica Set

Secondary nodes primary node ka data replicate karte hain. In nodes ka kaam hota hai primary node ko continuously follow karna aur latest data ko sync karna. Secondary nodes ko tum read operations ke liye use kar sakte ho, lekin yeh operations sirf read-only hote hain.

  • Example: Agar tum Node B se read operation karte ho:

    db.collection.find({ name: "Anu" }).readPreference("secondary");
    Yeh query Node B (Secondary) se data fetch karegi.

Failover

Agar kabhi primary node fail ho jaaye, toh ek secondary node automatically primary node ban jaati hai. Is process ko failover kehte hain, aur yeh ensure karta hai ki system hamesha available rahe.


Climax: DataGaav ka Ekta ka Jaadu

Mongo Baba ne jab yeh concepts sikhaye, tab bacchon ko samajh aa gaya ki MongoDB ka system kaise strong aur reliable hai. Bacchon ne seekha ki kaise Locking aur Transactions data consistency ko maintain karte hain, aur kaise Replica Sets data ko secure aur highly available rakhte hain.

Is kahani ke baad DataGaav ke bacche aur bhi intelligent ho gaye. Ab unhe pata tha ki MongoDB ke jaadu se wo apne data ko har samay secure aur reliable rakh sakte hain, bina kisi risk ke.

Post a Comment

0 Comments