MongoDB Architecture
1. Executive Summary
MongoDB is a distributed document database storing BSON documents in collections, optimized for flexible schemas, rich query patterns on documents, and horizontal scale via sharding. A replica set provides high availability through primary-secondary replication with automatic leader election (Raft-inspired consensus via MongoDB's replication protocol). The default storage engine WiredTiger provides document-level concurrency, compression, and checkpoint-based durability.
Sharded clusters partition data by shard key across shards (each a replica set), coordinated by mongos routers and config servers (replica set holding metadata). MongoDB offers tunable write concern and read concern mapping to durability and consistency tradeoffs—from eventual secondary reads to linearizable reads on primary with appropriate concerns.
Principal architects evaluate MongoDB for document workloads, catalog/metadata, and operational simplicity at scale—while avoiding it for heavy multi-document ACID across shards without careful design, unbounded scatter-gather queries, or relational join-heavy analytics. Every sharded deployment review should open with shard key and write concern documentation.
2. Why This Topic Matters
MongoDB appears in principal interviews for:
- Replica set elections — failover timing and split-brain prevention.
- Shard key design — monotonic vs hashed keys, jumbo chunks.
- Write/read concern —
majority,w:1, causal consistency session. - Transactions — single-document atomicity vs multi-document (4.0+).
- vs DynamoDB/Cassandra — when document model wins.
Bad shard keys cause unmovable hotspots; misunderstanding write concern causes data loss on failover.
MongoDB interviews at principal level are shard-key design exercises first and feature-list recitations second.
3. Problems Being Solved
| Problem | MongoDB approach |
|---|---|
| Flexible evolving schema | Document model with optional validation |
| Rich queries on documents | Indexes, aggregation pipeline |
| High availability | Replica sets with automatic failover |
| Horizontal scale | Sharding by shard key |
| Geographic distribution | Replica set members in regions |
| Change streams | Real-time change notifications |
Workload fit matrix
| Workload | Fit | Caveat |
|---|---|---|
| Content management | Strong | Index design |
| User profiles / catalogs | Strong | Shard key for scale |
| IoT time series | Moderate | Consider TTL + shard key on time |
| Multi-tenant SaaS | Strong | Prefix shard key with tenant_id |
| Cross-shard JOIN analytics | Weak | ETL to warehouse |
| Ledger with strict serializability | Moderate | Transactions limited scope |
4. Assumptions and System Model
| Assumption | Implication |
|---|---|
| Replica set quorum | Majority for elections and majority writes |
| Shard key immutable per document | Choose key carefully at design time |
| Mongos routes by metadata | Config servers are critical path |
| WiredTiger MVCC | Document-level locking |
| Network partitions possible | Write concern determines safety |
Safety: writeConcern: majority survives primary failure without acknowledged loss. Liveness: Elections complete with majority reachable; minority partition unavailable for writes.
5. Essential Terminology
| Term | Definition |
|---|---|
| Replica set | Group of nodes with one primary |
| Oplog | Capped replication log of operations |
| Shard key | Indexed field(s) determining chunk distribution |
| Chunk | Range of shard key values on one shard |
| Mongos | Query router for sharded cluster |
| Config servers | Store cluster metadata |
| Write concern | Durability ack level (w, j, timeout) |
| Read concern | Consistency of read (local, majority, linearizable) |
| Jumbo chunk | Chunk too large to migrate—operational problem |
| Change stream | Tailable cursor on oplog-derived events |
6. Core Mechanism
6.1 Replica set architecture
Figure 1: Primary accepts writes; secondaries replicate via oplog; election on primary failure.
6.2 Sharded cluster
Figure 2: Mongos routes queries using chunk metadata from config servers.
6.3 Write path with majority concern
Figure 3: Majority write concern waits for replication to majority before ack.
7. Step-by-Step Walkthrough
Walkthrough A: Primary failover
- Primary stops heartbeating; secondaries detect timeout.
- Election: candidate requests votes; majority required.
- New primary elected; ~10-30s typical [deployment-dependent].
- Clients retry writes; drivers refresh topology from replica set.
- Rollback: if former primary had un-replicated writes, they are rolled back.
Walkthrough B: Sharded query (targeted)
- Query includes
user_idequality—part of shard key. - Mongos routes to single shard owning chunk.
- Shard replica set primary executes query.
- Results return through mongos.
Walkthrough C: Scatter-gather query
- Query on non-prefix field without index routing.
- Mongos fans out to all shards.
- Each shard returns partial results; mongos merges.
- Performance warning: does not scale linearly.
Walkthrough D: Multi-document transaction (sharded)
- Session starts transaction; mongos coordinates.
- Two-phase commit across involved shards.
- Higher latency; limited duration (default 60s)—use sparingly.
Walkthrough E: Change stream consumer
- Application opens change stream on
orderscollection with resume token. - Insert/update/delete events arrive with full document on update [configurable].
- Consumer projects to Elasticsearch for search index—at-least-once processing with idempotent upsert.
- Resume token persisted after successful ES ack—crash recovery from last token.
- Lag monitored; alert if oplog window risk for long outages.
Walkthrough F: Index strategy review
- Query pattern:
{ tenant_id: 1, status: 1, created_at: -1 }compound index created. - Explain plan shows IXSCAN instead of COLLSCAN—p95 drops 200ms → 8ms [illustrative].
- Review index bloat quarterly; drop unused indexes from
$indexStats. - Partial index on
status: "active"reduces size for hot path. - Document index ownership in catalog linked to query SLO.
Replica set election tuning (operations)
| Parameter | Consideration |
|---|---|
electionTimeoutMillis | Lower = faster failover, more sensitivity to latency |
| Priority | Prefer stable nodes as primary candidates |
| Arbiter | Vote only—no data; use only when cost-constrained |
| Write concern majority | Required for production durability |
| Read preference | primary for read-your-writes; secondaryPreferred for scale |
Walkthrough E: Resync after oplog window miss
- Secondary falls behind primary during network partition longer than oplog retention window.
- Secondary enters
RECOVERINGstate; requires initial sync or resync from another secondary. - Application reads from stale secondary fail or return old data if not using
readConcern: majority. - Operations increases oplog size and adds monitoring on
replLagwith page before window risk. - Architecture review documents minimum oplog hours = max maintenance duration + peak catch-up time + safety margin.
MongoDB principal interviews almost always include drawing a shard key for a given access pattern—practice on paper before the loop. State writeConcern majority before discussing read scaling. Elections and rollback behavior separate principal answers from developer trivia. Chunk migration and jumbo chunk monitoring belong in every sharded cluster runbook. Principal architects veto monotonic shard keys in architecture review before the first production write. Scatter-gather queries on hot paths are design failures, not tuning opportunities. Document read preference and write concern per API in the service catalog for every production MongoDB consumer. Oplog sizing is a capacity planning task, not an afterthought.
8. Invariants and Guarantees
| Configuration | Guarantee |
|---|---|
| w:1, no journal | Fast; may lose last writes on crash |
| w:majority | Acked writes survive single node failure |
| readConcern majority | Read data durable on majority |
| linearizable read (primary) | Strongest single-document read on primary |
| Causal sessions | Causal ordering across operations |
CAP: CP for majority writes in partition; minority partition loses liveness for writes.
9. Failure Scenarios
| Failure | Behavior | Mitigation |
|---|---|---|
| Bad shard key (monotonic) | Single hot shard | Hashed shard key or compound |
| Jumbo chunk | Cannot balance | Key redesign; manual intervention |
| Config server loss | Metadata unavailable | Config as 3-node replica set |
| Split brain (rare) | Prevented by majority elections | Odd member count; avoid arbiters misuse |
| Oplog window too small | Secondary cannot resync | Increase oplog size |
| Stepdown during index build | Operation impact | Rolling maintenance windows |
| Transaction timeout | Abort | Keep transactions short |
10. Performance Characteristics
| Dimension | Behavior |
|---|---|
| Single-doc write | Low ms on primary |
| Indexed query | Efficient with proper index |
| Aggregation pipeline | Powerful; memory limits apply |
| Scatter-gather | Poor at scale |
| Replication lag | Secondary reads may be stale |
WiredTiger cache sizing dominates working set fit—monitor cache eviction.
11. Scalability Limits
- Shard count practical limits—metadata and mongos fan-out.
- Chunk migration rate during balancing.
- Global indexes on sharded collections—each shard maintains index.
- Transaction cross-shard—latency and lock scope.
- Document size 16 MB limit.
12. Operational Considerations
- Shard key choice is permanent architecture decision—ADR required.
- Monitor replication lag, opcounters, queued readers, chunk counts.
- Rolling upgrades replica set members one at a time.
- Backup: filesystem snapshots consistent with
fsyncLockor cloud backup service. - Index builds online but resource-heavy.
- Connection pooling at application—avoid connection storms.
- Document read preference per API endpoint in service catalog—prevent accidental stale reads on money paths.
- Chunk migration monitoring during tenant onboarding; alert on jumbo chunk warnings.
- Oplog sizing calculator in runbook: peak write rate × maintenance window × safety factor.
- Quarterly shard key review for top 10 collections by storage growth.
13. Security Considerations
- Authentication (SCRAM-SHA) and role-based access control.
- TLS for all connections.
- Encryption at rest (WiredTiger encrypted storage engine).
- Field-level encryption client-side for sensitive fields.
- Network isolation—no public mongod without bastion.
14. Cost Considerations
- Atlas vs self-managed—ops labor tradeoff.
- Shard proliferation—each shard is full replica set.
- Storage growth from indexes duplicating shard key.
- Over-provisioned shards for future scale—delay sharding until needed.
- IO costs on cloud disks for WiredTiger.
Oplog and replication lag deep dive
Secondaries fall behind when primary write rate exceeds secondary apply capacity—common during bulk loads without writeConcern throttling. Lag > oplog window forces full resync (hours of downtime risk for that secondary). Monitor replLag and size oplog for maintenance window + peak load duration. Principal architects schedule bulk loads through mongodump/mongorestore or temporarily scale secondary tier—not blind insertMany at max rate.
Atlas vs self-managed decision
| Factor | Atlas | Self-managed |
|---|---|---|
| Ops burden | Lower | Higher |
| Custom tuning | Limited | Full |
| VPC peering | Supported | DIY |
| Compliance certs | Inherited | Your audit |
| Cost at scale | Premium | Engineering tradeoff |
Change streams vs Debezium
Change streams are simpler for Mongo-native apps—no separate connector JVM. Debezium better when unified CDC bus feeds Kafka for multiple consumers (warehouse, search, cache). Architecture choice depends on consumer count and existing Kafka investment, not Mongo capability alone.
15. Production Implementations
Case study: Multi-tenant SaaS catalog (illustrative)
Context
10M tenants; product catalog documents; 50k ops/sec peak.
Design
Sharded cluster; shard key {tenant_id: 1, product_id: 1}—tenant isolation, even distribution within tenant via product_id.
Read path
readPreference: secondaryPreferred for catalog browse; primary for inventory updates with writeConcern: majority.
Pitfall avoided
Initial {product_id: 1} only—hot shard on sequential IDs; redesigned before production.
Extended operations narrative
Production incident: election during index build extended write outage to 45s—now schedule heavy index builds off-peak. Change stream consumer lagged 20 minutes during oplog window scare—oplog resized 3×. Sharded cluster balancer migrated 400 chunks during tenant onboarding without app impact—validated shard key {tenant_id, product_id} design. Atlas performance advisor recommended compound index saving 30% read CPU [illustrative].
16. Alternatives and Tradeoffs
| System | Comparison |
|---|---|
| DynamoDB | Managed KV; less flexible ad hoc query |
| Cassandra | Wide-column; tunable consistency per query |
| PostgreSQL JSONB | ACID relational + JSON; vertical scale limits |
| Couchbase | Similar document + KV |
Choose MongoDB for document model, aggregation, and mature sharding with operational familiarity.
17. Common Misconceptions
| Misconception | Reality |
|---|---|
| "MongoDB is schemaless" | Schema validation recommended |
| "Sharding is automatic magic" | Shard key design is manual and critical |
| "Secondary reads always OK" | Stale reads without majority concern |
| "Transactions free" | Cross-shard transactions costly |
| "Embed everything" | Document size and update amplification |
18. Principal Architect Perspective
- Shard key workshop before first sharded deploy.
- Default writeConcern majority for production durability.
- Avoid scatter-gather in hot paths—design queries with shard key.
- Atlas unless strong self-host ops team.
- Sync analytics to warehouse—not heavy aggregation on primaries.
MongoDB at scale is shard-key architecture first, database second. Principal sign-off on any sharded deployment should include query pattern analysis and write concern documentation in the service catalog. Run election and rollback drills before declaring HA complete.
Operating playbook (first 90 days)
Days 1–30: Verify all prod replica sets use writeConcern: majority. Document shard key ADR before any sharded deploy.
Days 31–60: Enable query profiling on slow operations; review indexes via $indexStats. Size oplog for peak load + maintenance window.
Days 61–90: Chaos test primary failover; measure election time vs app retry config. Change stream or CDC path documented for search/analytics consumers.
19. Architecture Review Exercise
Scenario: Shard key created_at timestamp; insert rate 50k/sec.
Findings: Single hot chunk; monotonic key anti-pattern. Migrate to hashed _id or compound {tenant_id, hashed_id}.
20. Whiteboard Explanation
"MongoDB stores BSON documents. A replica set has one primary accepting writes and secondaries tailing the oplog. Writes use a write concern—majority waits for replication to most nodes before ack. Sharding splits data into chunks by shard key ranges; mongos routers direct queries using metadata in config servers. Targeted queries including the shard key hit one shard; others scatter-gather. WiredTiger provides MVCC and compression. Elections pick a new primary on failure in seconds if majority exists. The architectural crux is shard key choice—it determines balance and query locality."
Principal addendum: Walk through bad key created_at creating hot chunk; fix with {tenant_id, hashed_id}. Stress writeConcern majority and that scatter-gather on hot paths is a design failure.
21. Interview Questions
- Replica set purpose? — HA and read scaling.
- Oplog role? — Replication log for secondaries.
- Shard key importance? — Data distribution and query routing.
- Mongos function? — Query router in sharded cluster.
- writeConcern majority? — Durability across majority nodes.
- Jumbo chunk problem? — Cannot migrate; hotspot.
- Scatter-gather query? — Fan-out to all shards.
- Election requirement? — Majority votes.
- WiredTiger? — Default storage engine with MVCC.
- Change streams? — Real-time data change feed.
- Causal consistency sessions? — Ordered reads across ops.
- When not MongoDB? — Heavy relational joins at scale.
- Arbiter tradeoff? — Vote without data—use carefully.
- Rollback on failover? — Unreplicated writes lost on old primary.
Scoring rubric (principal)
| Dimension | Strong | Weak |
|---|---|---|
| Sharding | Key design, chunks | "Auto scales" |
| Consistency | write/read concern | Ignores durability |
| Failover | Elections, rollback | "Replicas sync instantly" |
| Anti-patterns | Monotonic keys, scatter | Generic NoSQL |
Extended scoring notes
Principal bar: Candidate names oplog, elections, and chunk migration without prompting. Bonus for discussing rollback of uncommitted writes on failed primary. Staff bar: Designs shard key for given SaaS schema with tradeoff narration. Weak hire: Treats MongoDB as "schemaless JSON in cloud" with no durability discussion.
- Change stream vs polling? — Push, resume token, lower latency.
- readConcern majority use? — Avoid stale secondary reads after failover.
- When GridFS? — Documents >16 MB binaries.
22. Interview Follow-Ups
- Design shard key for multi-tenant SaaS. —
tenant_idprefix + hashed secondary. - Primary dies mid w:1 write. — Write may be lost; client unaware.
- Balance chunks after tenant growth. — MongoDB balancer migrates chunks.
- Read from secondary stale—fix? —
readConcern: majorityor primary read. - 16 MB document limit hit. — GridFS or normalize schema.
Additional principal scenarios
Scenario: App team wants transactional guarantees across 10 shards. Answer: Multi-document transactions possible but high latency—redesign document boundaries or use saga pattern at application layer.
Scenario: Secondary reads show stale inventory sold out. Answer: Inventory checkout must use readPreference: primary with readConcern: majority; secondaries only for browse.
Scenario: Atlas auto-scaling shards during steady state. Answer: Review if shard count exceeds need; each shard is replica set cost; right-size keys before horizontal scale.
22A. Production Low-Level Design (Interview + Game Day)
Interview + ops bar per Production LLD and Game Day Standard. Whiteboard mongos + chunk routing vs replica set elections + writeConcern.
C4 context
Step-by-step flow:
| Step | Mechanism | Explanation |
|---|---|---|
| 1 | Targeted query | Includes shard key |
| 2 | Chunk routing | Range on shard key |
| 3 | Config cache | mongos metadata |
| 4 | Replication | Oplog to secondaries |
| 5 | Balancer | Rebalance hot chunks |
| 6 | Ops | Election alerts, replication lag |
Production stack
| Layer | Component | Purpose |
|---|---|---|
| Sharding | mongos + config servers | Horizontal scale |
| Storage | Replica set per shard | WiredTiger MVCC |
| Cloud | MongoDB Atlas | Managed ops |
| Driver | MongoDB driver | writeConcern / readConcern |
| Obs | Atlas metrics + profiler | Slow queries, hot shards |
Data model (snippet)
// Multi-tenant SaaS — even distribution + targeted queries
db.orders.createIndex({ tenant_id: 1, order_id: 1 });
sh.shardCollection("app.orders", { tenant_id: 1, order_id: 1 });
// writeConcern: { w: "majority", j: true } for checkout
Happy-path sequence
Failure / election + stale secondary read
Step-by-step flow:
| Step | Failure | Mitigation |
|---|---|---|
| w:1 write | Unacknowledged loss | majority for money paths |
| Failover | Brief unavailability | Retry writes |
| Stale secondary | Oversell risk | primary + majority read |
| Bad shard key | Hot chunk | Reshard / new key design |
SLOs (one-liner)
| SLO | Target (illustrative) | How achieved |
|---|---|---|
| Targeted query p99 | < 50 ms | Shard key in filter |
| Write durability | majority + journal | writeConcern |
| Failover RTO | < 30 s | Replica set elections |
| Chunk balance | No jumbo chunks | Balancer + key design |
| Checkout correctness | No stale reads | primary + majority |
22B. Game Day Playbooks
Catalog
| # | Drill | Frequency | Primary SLO under test |
|---|---|---|---|
| GD-1 | Primary kill + election | Quarterly | RTO |
| GD-2 | Stale secondary read on inventory | Monthly | Correctness |
| GD-3 | Hot shard / monotonic key | Quarterly | Distribution |
| GD-4 | Chunk migration during peak | Semi-annual | Latency |
| GD-5 | Scatter-gather query storm | Quarterly | Load |
| GD-6 | w:1 vs majority write loss | Annual | Durability education |
GD-1 — Primary failover
GD-2 — Stale inventory read
Pass criteria: Financial paths never use secondary without majority; runbook for elections.
23. Strong Answer Example
Question: "Explain MongoDB sharding and shard key selection."
Answer using PRACTICE. Strong voice: chunks + mongos + immutable shard key, not "auto scales."
Student interview script (PRACTICE)
P — Problem framing
"Sharding splits data into chunks (ranges of shard key) across replica sets. mongos routes queries—scatter-gather if shard key missing from filter. Shard key is immutable and determines hot shards. Monotonic keys (timestamp) create a single hot chunk."
R — Requirements
| Type | Requirement |
|---|---|
| FR | Multi-tenant queries by tenant_id |
| NFR | Even distribution; targeted queries |
| Ops | Balancer; chunk migrations |
A — Assumptions
| Assumption | Why |
|---|---|
| SaaS multi-tenant | tenant_id in key |
| Range queries needed | Compound key not hashed-only |
| Atlas managed | Ops via console |
C — Capacity
Growing tenant → chunk splits; jumbo chunk blocks migration.
T — Topology
mongos → N shards (each RS) + config servers.
I — Invariants
"Every hot-path query includes shard key; checkout uses majority; shard key chosen upfront."
C — Constraints
| Constraint | Response |
|---|---|
| Immutable shard key | Careful upfront design |
| 16 MB doc limit | GridFS / normalize |
| Cross-shard txn | High latency — avoid |
E — Evolution
RS → sharded cluster → resharding if key wrong (expensive).
Closing: "Compound tenant_id + order_id for distribution and targeting; avoid monotonic keys; mongos routes chunks; majority writes for durability."
Scoring signals
| Strong | Weak |
|---|---|
| Chunk + mongos | "Auto shards" |
| Shard key tradeoffs | Ignores hot shard |
| writeConcern majority | w:1 everywhere |
| Scatter-gather cost | Missing key OK |
Compact outline (2 min)
"Chunks on shard key; mongos routes; compound tenant+id key; no monotonic timestamp alone; majority for checkout; primary read for inventory; balancer migrates chunks."
24. Weak Answer Example
Weak: "MongoDB shards automatically when data grows; you don't need to think about keys."
Red flags: No shard key, mongos, or chunk concepts.
25. Hands-On Exercise
- Deploy 3-node replica set locally; kill primary; observe election.
- Insert with
w:1vsmajority; compare failover behavior discussion. - Create sharded cluster; test targeted vs scatter query explain plans.
- Watch chunk migration during balancer.
- Design shard keys for three sample schemas.
26. Knowledge Check
- Primary role? (Accept writes.)
- Oplog is? (Replication operation log.)
- Mongos routes? (Sharded queries.)
- Majority write survives? (Single node failure without ack loss.)
- Jumbo chunk? (Too large to migrate.)
- WiredTiger provides? (MVCC storage engine.)
- Config servers store? (Sharding metadata.)
- Hot shard cause? (Poor shard key.)
- Change streams source? (Oplog-based.)
- Document size limit? (16 MB.)
- Majority write survives? (Single node failure without ack loss.)
- Scatter-gather hits? (All shards.)
- Arbiter provides? (Vote without data.)
27. Flashcards
| Front | Back |
|---|---|
| Replica set | HA group with one primary |
| Oplog | Capped replication log |
| Shard key | Determines data distribution |
| Chunk | Shard key range on one shard |
| Mongos | Sharded cluster query router |
| writeConcern | Write durability acknowledgment |
| readConcern | Read consistency level |
| WiredTiger | Default MongoDB storage engine |
| Jumbo chunk | Unmigratable oversized chunk |
| Change stream | Real-time change notification |
28. Cheat Sheet
REPLICA SET
Primary + secondaries + oplog | elections need majority
SHARDING
mongos + config servers + shard replica sets | KEY DESIGN CRITICAL
DURABILITY
writeConcern: {w: "majority", j: true} for production
AVOID
Monotonic shard keys | scatter-gather hot paths | huge documents
PRINCIPAL ANCHORS
Shard key ADR required
writeConcern majority prod
Oplog sized for maintenance
Primary for money reads
Chunk migration watch
Index from $indexStats
Change streams need resume token
Elections need majority
29. Related Concepts
- Primary-Secondary Replication — replication pattern
- Quorum Systems — majority semantics
- LSM Trees — WiredTiger contrast with LSM
- Amazon DynamoDB — alternative NoSQL
- Leader Election — failover theory
30. References
Primary sources
- MongoDB Manual — replica sets, sharding, read/write concerns.
- MongoDB architecture guides — WiredTiger, replication protocol.
Related
- Chodorow, K. MongoDB: The Definitive Guide — operational depth.
- Original MongoDB design docs — document model rationale.
Principal study path
Review Primary-Secondary Replication, Quorum Systems, Amazon DynamoDB for alternative NoSQL, and Leader Election for failover theory connecting to replica set elections. Whiteboard shard key design for every MongoDB interview—you will be asked.
Distinction
| Claim | Type |
|---|---|
| writeConcern semantics | MongoDB manual |
| Election timing | Deployment-dependent |
| Transaction limits | Version-specific—verify docs |