Episode 10 : Sharding Ka Jaadu: MySQL Aur MongoDB

Pooja ki curiosity ko dekhte hue, Mongo Baba ne MySQL aur MongoDB ke sharding ke concepts ko explain kiya. Sharding ka jaadu MySQL mein bhi hota hai, lekin MongoDB aur MySQL mein isse implement karne ka tareeka thoda alag hota hai. Chalo, Pooja ko samjhate hain:

Define oplog(operational log)?

How do we create a database in MongoDB?

Sharding Ka Jaadu: MySQL Aur MongoDB


**1. MongoDB Sharding:

How it Works:

  • Sharding: MongoDB mein sharding ek built-in feature hai jo data ko multiple servers (shards) par distribute karta hai. Yeh technique large datasets aur high throughput requirements ko handle karne ke liye use hoti hai.
  • Sharding Key: Data ko distribute karne ke liye ek sharding key select kiya jata hai, jo decide karta hai ki data kis shard par store hoga.

Example:

javascript

// Enabling sharding on a database db.adminCommand({ enableSharding: "myDatabase" }); // Sharding a collection db.adminCommand({ shardCollection: "myDatabase.myCollection", key: { _id: 1 } });

Key Concepts:

  • Shards: Individual databases that store parts of the data.
  • Config Servers: Maintain metadata and configuration settings.
  • Query Routers (mongos): Route queries to appropriate shards.

**2. MySQL Sharding:

How it Works:

  • Sharding: MySQL mein sharding ko manual approach se handle kiya jata hai. Isme aap apne data ko multiple MySQL servers par distribute karte hain. Sharding implementation ke liye application-level logic use ki jati hai.
  • Sharding Key: Sharding key select karna hota hai, jo decide karta hai ki data ko kis shard par store karna hai.

Example:

  • Application-Level Sharding:

    • Suppose aapke paas ek users table hai, aur aap chahte hain ki data ko multiple servers par distribute karein. Aap user_id ko sharding key ke roop mein use kar sakte hain aur users ko different shards par store kar sakte hain.
  • Manual Sharding Example:

    • Shards Setup: Aap 3 different databases setup kar sakte hain, jaise users_shard1, users_shard2, users_shard3.
    • Data Distribution: Application logic ko modify karke decide karein ki user_id ke basis par data ko kis shard (database) par insert karna hai.
    sql

    -- Example of distributing data INSERT INTO users_shard1 (user_id, user_name) VALUES (1, 'Alice'); INSERT INTO users_shard2 (user_id, user_name) VALUES (2, 'Bob'); INSERT INTO users_shard3 (user_id, user_name) VALUES (3, 'Charlie');

Key Concepts:

  • Shards: Separate MySQL databases that store parts of the data.
  • Application Logic: Controls how data is distributed and accessed across shards.
  • Query Routing: Application logic must route queries to the appropriate shard.

Summary:

  • MongoDB: Sharding is a built-in feature with automatic data distribution and management.
  • MySQL: Sharding is manual, requiring custom application logic to distribute and query data across multiple databases.

Sharding dono hi systems mein important hai for scaling and handling large datasets, lekin implementation ka tareeka alag hota hai.

Pooja ko sharding ke concepts clear ho gaye hain! Agar aur questions hain ya kisi aur topic pe discussion karna hai, toh batao!

Mongo Baba ke paas ek naya jadu bhari kahani hai jo oplog aur database creation ke concepts ko samjhata hai. Chalo, Mongo Baba ke sath ek magical journey par chalte hain!

Mongo Baba Ki Kahani: Oplog Aur Database Creation Ka Jadoo


Ek Gaav Mein: Mongo Nagar

Background: Mongo Nagar ek magical gaav hai jahan har koi data aur databases ke jaadu mein vishwas rakhta hai. Gaav mein ek magical librarian hai, jiska naam Mongo Baba hai. Uske paas sabhi data aur databases ke secrets hain. Aaj Mongo Baba ke paas ek zaroori mission hai—gaav ke bachon ko oplog aur database creation ka jaadu dikhana.


Chapter 1: Oplog Ka Jaadu

Setting: Mongo Nagar ka Library, jahan har chiz ek magical book ki tarah chalti hai.

Story:

  1. Oplog Ka Mool Mantra: Mongo Baba ne gaav ke bachon ko bataya ki MongoDB mein "oplog" ek special magical log hai jo sabhi changes ko record karta hai. Oplog ka use replica sets mein hota hai taaki primary aur secondary nodes ke beech synchronization ho sake.

    Example:

    • Primary Node: Jaise ek magic wand hai jo sabhi changes perform karta hai.
    • Oplog: Magic wand ke actions ka ek detailed diary hai jo record karta hai.
    • Secondary Nodes: Jaise magic mirrors hain jo oplog ko dekh kar apne actions update karte hain.
  2. Oplog Ka Jadoo: Oplog automatically har ek operation (insert, update, delete) ko record karta hai. Isse replication process smooth aur accurate hota hai.

    Example Command:

    javascript

    // To view the oplog db.local.oplog.rs.find().pretty();

    Magic: Agar kisi node par koi change hota hai, oplog usko record karta hai aur baaki nodes apne data ko update kar leti hain.


Chapter 2: Database Creation Ka Jadoo

Setting: Mongo Nagar ke ek naya magical library branch mein, jahan naye databases create kiye jate hain.

Story:

  1. Database Creation Ka Tarika: Mongo Baba ne dikhaya ki MongoDB mein naya database create karna simple hai. Jaise ek nayi kitaab ko library mein add karna.

    Steps:

    • Create Database: Bas ek new database ka naam define karke, MongoDB us database ko create kar deta hai jab pehli baar data insert hota hai.

    Example Command:

    javascript

    // Create a new database use newDatabaseName;
    • Create Collection: Database ke andar ek collection (jaise ek section of books) create karna hota hai.

    Example Command:

    javascript

    // Create a new collection in the database db.createCollection("myCollection");
  2. Magic of Database Creation: Jaise hi aap database create karte hain aur usmein data insert karte hain, MongoDB automatically us database ko initialize kar deta hai aur aapke data ko store karta hai.

    Example:

    javascript
    // Insert data into the collection db.myCollection.insert({ name: "Alice", age: 25 });

    Magic: Database tab tak create nahi hota jab tak data insert nahi kiya jata. Data insert karte hi, MongoDB apne magical library mein ek naya section add kar deta hai.


Chapter 3: The Magical Conclusion

Story Summary: Mongo Baba ne gaav ke bachon ko oplog aur database creation ke magic ke baare mein sikhaya. Oplog se replication aur data synchronization smooth hota hai, aur database creation se naye data sections create kiye ja sakte hain.

Magic Lessons:

  1. Oplog: Ek magical diary jo data changes ko record karta hai aur replication ko manage karta hai.
  2. Database Creation: Simple aur magical process jo naye databases aur collections ko create karta hai jab pehli baar data insert hota hai.

Bacche Khush: Bacche Mongo Baba ki kahani se bahut khush hue aur unhone oplog aur database creation ka jaadu samajh liya. Unhone bhi Mongo Nagar ke magical secrets seekh liye.

Agar aapko aur kahaniyan chahiye ya kisi aur topic pe baat karni hai, toh batao!

Pooja ki curiosity ko dekhte hue, Mongo Baba ne MySQL aur MongoDB ke sharding ke concepts ko explain kiya. Sharding ka jaadu MySQL mein bhi hota hai, lekin MongoDB aur MySQL mein isse implement karne ka tareeka thoda alag hota hai. Chalo, Pooja ko samjhate hain:

Sharding Ka Jaadu: MySQL Aur MongoDB


**1. MongoDB Sharding:

How it Works:

  • Sharding: MongoDB mein sharding ek built-in feature hai jo data ko multiple servers (shards) par distribute karta hai. Yeh technique large datasets aur high throughput requirements ko handle karne ke liye use hoti hai.
  • Sharding Key: Data ko distribute karne ke liye ek sharding key select kiya jata hai, jo decide karta hai ki data kis shard par store hoga.

Example:

javascript
// Enabling sharding on a database db.adminCommand({ enableSharding: "myDatabase" }); // Sharding a collection db.adminCommand({ shardCollection: "myDatabase.myCollection", key: { _id: 1 } });

Key Concepts:

  • Shards: Individual databases that store parts of the data.
  • Config Servers: Maintain metadata and configuration settings.
  • Query Routers (mongos): Route queries to appropriate shards.

**2. MySQL Sharding:

How it Works:

  • Sharding: MySQL mein sharding ko manual approach se handle kiya jata hai. Isme aap apne data ko multiple MySQL servers par distribute karte hain. Sharding implementation ke liye application-level logic use ki jati hai.
  • Sharding Key: Sharding key select karna hota hai, jo decide karta hai ki data ko kis shard par store karna hai.

Example:

  • Application-Level Sharding:

    • Suppose aapke paas ek users table hai, aur aap chahte hain ki data ko multiple servers par distribute karein. Aap user_id ko sharding key ke roop mein use kar sakte hain aur users ko different shards par store kar sakte hain.
  • Manual Sharding Example:

    • Shards Setup: Aap 3 different databases setup kar sakte hain, jaise users_shard1, users_shard2, users_shard3.
    • Data Distribution: Application logic ko modify karke decide karein ki user_id ke basis par data ko kis shard (database) par insert karna hai.
    sql
    -- Example of distributing data INSERT INTO users_shard1 (user_id, user_name) VALUES (1, 'Alice'); INSERT INTO users_shard2 (user_id, user_name) VALUES (2, 'Bob'); INSERT INTO users_shard3 (user_id, user_name) VALUES (3, 'Charlie');

Key Concepts:

  • Shards: Separate MySQL databases that store parts of the data.
  • Application Logic: Controls how data is distributed and accessed across shards.
  • Query Routing: Application logic must route queries to the appropriate shard.

Summary:

  • MongoDB: Sharding is a built-in feature with automatic data distribution and management.
  • MySQL: Sharding is manual, requiring custom application logic to distribute and query data across multiple databases.

Sharding dono hi systems mein important hai for scaling and handling large datasets, lekin implementation ka tareeka alag hota hai.

Pooja ko sharding ke concepts clear ho gaye hain! Agar aur questions hain ya kisi aur topic pe discussion karna hai, toh batao!

Post a Comment

0 Comments