Back to MongoDB Expert
Advanced
25 min Read

Scaling & Transactions

Learning Objectives

  • Replication Sets
  • Sharding Strategy
  • ACID Transactions

Scaling & High Availability: Replica Sets and Sharding

Modern applications require 24/7 availability and the ability to handle massive traffic. MongoDB achieves this through Replication and Sharding.

1. Replica Sets (High Availability)

A Replica Set is a group of mongod instances that maintain the same data set.

  • Primary: Receives all write operations.
  • Secondaries: Replicate the Primary's data. If the Primary fails, an election occurs to choose a new Primary.
  • Read/Write Split: You can scale read traffic by allowing clients to read from Secondaries.

2. Sharding (Horizontal Scaling)

Sharding is the process of splitting a large dataset across multiple machines (shards).

  • Shard Key: The field used to distribute data. Choosing a good shard key is the most critical decision in Mongo architecture.
  • Config Servers: Store metadata about which data lives on which shard.
  • Mongos: The query router that directs your application's requests to the correct shard.

3. ACID Transactions

Since MongoDB 4.0, multi-document transactions are supported. While powerful, they carry a performance overhead and should be used sparingly compared to embedding.

javascript code
const session = db.getMongo().startSession();
session.startTransaction();
try {
   db.users.updateOne({ _id: 1 }, { $set: { balance: 50 } }, { session });
   db.orders.insertOne({ user_id: 1, amount: 50 }, { session });
   session.commitTransaction();
} catch (error) {
   session.abortTransaction();
}

The "Write Concern"

Control the level of acknowledgment you need for writes. w: 1 means acknowledge by primary; w: "majority" ensures it's written to most members of the replica set—safer but slower.

Confused about this chapter?

Ask our DevVault AI Assistant for instant clarification!

Ask DevVault AI