Distributed Databases
A distributed database spreads data and compute across many machines — often many regions — to deliver scale, availability and low latency that no single machine can.
Introduction
A distributed database spreads data and compute across many machines — often many regions — to deliver scale, availability and low latency that no single machine can. MongoDB is distributed by design: replica sets for HA, sharded clusters for horizontal scale, and Atlas Global Clusters for geo-distribution.
The trade-off is governed by the CAP theorem (Consistency, Availability, Partition tolerance — pick two) and the PACELC extension. MongoDB tunes the dial per-operation via write/read concerns: you choose strict consistency or lower latency on every call.
Learning objective: Apply Distributed Databases correctly in a TechLearningPRO MongoDB workload — with clear data-model choices, safe writes, and failure modes you can explain in review.
Treat every example as production-shaped MongoDB: explicit projections, indexes that match the access path, and Atlas-safe defaults. Prefer mongosh / Node driver shapes used in SaaS backends.
Purpose of this lesson
Apply Distributed Databases correctly in a TechLearningPRO MongoDB workload — with clear data-model choices, safe writes, and failure modes you can explain in review.
Understanding the topic
Distribution primitives in MongoDB:
- Replica set — same data on multiple nodes. Solves availability & durability.
- Sharded cluster — data partitioned across shards by a shard key. Solves scale.
- mongos router — stateless query router that hides shards from applications.
- Config servers — store cluster metadata (chunk → shard mapping).
- Zone sharding / Global Clusters — pin data to specific regions for residency & latency.
- Causal consistency sessions — read-your-writes guarantees across nodes.
Step-by-step explanation
- State when Distributed Databases is the right MongoDB tool versus an alternative.
- Map the diagram boxes to driver, mongod, WiredTiger, and your collection.
- Run the smallest correct mongosh / driver example against a TechLearningPRO learners collection.
- Name two failure modes (missing index, bad shard key, unbounded scan) and how you detect them.
Syntax reference
Sharded cluster anatomy:
┌────────────────┐ ┌────────────────┐│ Application(s) │ → │ mongos router │ ← stateless, scale horizontally└────────────────┘ └───────┬────────┘│ routes by shard key┌────────────────────┼────────────────────┐▼ ▼ ▼┌──────────┐ ┌──────────┐ ┌──────────┐│ Shard A │ │ Shard B │ │ Shard C ││ (rs0) │ │ (rs1) │ │ (rs2) │└──────────┘ └──────────┘ └──────────┘▲│┌──────────────┐│ Config RS │ ← chunk map, balancer state└──────────────┘
Informative example
Worked mongosh sketch:
// TechLearningPRO mongosh sketch — Distributed Databasesdb.learners.find({ status: "ACTIVE" },{ email: 1, plan: 1, updatedAt: 1 }).sort({ updatedAt: -1 }).limit(50).explain("executionStats");
Real-world use
Coinbase, Sega, Toyota and SAP run sharded MongoDB clusters with hundreds of shards serving petabytes of data. Atlas Global Clusters enable GDPR data residency: EU users' data physically stays in EU shards, US users' in US shards — transparent to the application.
Security implications
MongoDB is a trust boundary. Drivers must not interpolate untrusted input into query operators.
- Never build queries by concatenating user strings into operator objects.
- Use least-privilege Atlas database users; apps should not have root.
- Enable TLS and avoid exposing mongod on public networks.
Best practices
- Pick the shard key like you pick a primary key — it's nearly impossible to change later.
- Avoid monotonic shard keys (timestamps) — hot-spotting kills throughput.
- Use hashed shard keys for write distribution, ranged for locality queries.
- Start without sharding; add it only when a single replica set hits limits.
Common mistakes
- Sharding too early — operational complexity dwarfs the benefit for sub-TB workloads.
- Forgetting that secondary indexes on non-shard-key fields require scatter-gather queries.
- Ignoring chunk balancer alerts — imbalanced shards mean uneven CPU & disk.
Advanced interview questions
Interview Prep
Practice concise answers, then expand each card for the explanation.
1BeginnerQuestionWhat is Distributed Databases and when do you use it?+
Answer
2IntermediateQuestionHow would you verify Distributed Databases is working in production?+
Answer
3AdvancedQuestionWhat breaks if you misuse Distributed Databases?+
Answer
Hands-on exercise
Hands-on drill:
- Implement the smallest correct example for Distributed Databases on a learners/orders-like collection.
- Capture
explain('executionStats')and confirm an index was used when expected.
Summary
Distributed Databases is a production MongoDB skill: model for your read path, prove it with explain(), and ship with indexes and least-privilege roles.