Lesson 3.3:
Distributed Data Strategies
Sharding & Horizontal Scale
When a single database server cannot handle the work, we shard. MongoDB Atlas automates the partitioning of data across shards based on a critical shard key.
Multi-Document Transactions
Even in a sharded environment, MongoDB supports ACID transactions. Use these sparingly for critical operations like financial transfers.
const session = client.startSession();
session.startTransaction();
try {
await db.collection("accounts").updateOne({ _id: A }, { $inc: { balance: -100 } }, { session });
await db.collection("accounts").updateOne({ _id: B }, { $inc: { balance: 100 } }, { session });
await session.commitTransaction();
} catch (error) {
await session.abortTransaction();
} finally {
session.endSession();
}
Global Clusters & Low Latency
MongoDB Atlas Global Clusters allow you to place data near your users (e.g., European data in EU regions) for sub-millisecond response times across the globe.
Check Your Knowledge
What is the most important factor in a performant sharded cluster?