MongoDB document database deep dive: replica set with oplog (1 primary + N secondaries with automatic Raft-like failover), sharded cluster (mongos routers + config server replica set + 3 shard replica sets), writeConcern levels (1, majority, all), readPreference (primary, secondary, nearest), multi-document transactions (4.0+), and change streams (CDC built-in). Four scenarios: write with writeConcern majority, primary failover with election, sharded query routing (targeted vs scatter-gather), and change stream tailing oplog. Includes ADR comparing MongoDB vs PostgreSQL JSONB vs DynamoDB for evolving-schema document workloads.
MongoDB — document database с replica sets и optional sharding. Schema flexibility не отменяет schema governance; sharding не делает любой query targeted; retryable writes не заменяют idempotency for arbitrary workflows.
| Компонент | Ответственность |
|---|---|
| MongoDB Client | Использует sessions, concerns и retry classifications. |
| mongos Router | Единственный application interface к sharded cluster; использует config metadata. |
| Config Server Replica Set | Хранит chunk/range metadata и cluster configuration. |
| Shard A Primary | Принимает routed writes для своей shard-key range. |
| Shard A Secondary | Реплицирует oplog и может обслуживать разрешённые reads. |
| Shard B Primary | Владеет другой range; участвует в distributed transaction при необходимости. |
| Dedicated Search Index | Опциональная relevance/full-text projection с отдельной freshness model. |
A document mutation routes through mongos to one shard primary. Majority acknowledgement and journal behavior follow deployment settings; subsequent read freshness follows read preference/read concern/session choices.
Проверяемый исход: The API states the chosen guarantee instead of promising every secondary is immediately current.
A secondary-preferred read may lag the primary or observe data with different rollback guarantees depending on read concern. Causal guarantees require the documented session/concern combination.
Проверяемый исход: The caller either accepts bounded/observed staleness or routes to a stronger read path.
A supported acknowledged write can be retried by the driver using session and transaction numbers. Multi-operation batches can still report partial/no-write distinctions, and writes inside a transaction are not individually retryable.
Проверяемый исход: The application retries only classified operations and keeps a business id for longer or cross-system ambiguity.
A poor shard key can cause scatter/gather or hot ranges. MongoDB supports resharding, which redistributes data and needs capacity/verification. A dedicated search projection has its own asynchronous freshness.
Проверяемый исход: Shard-key evolution is a planned migration; full-text search never masquerades as a transactionally current primary read.
Числа выше — учебные inputs или размерностные формулы. Их нельзя выдавать за benchmark или SLA конкретного продукта.
[CONCEPT]partitioning-strategies
Диаграмма показывает причинные границы и recovery contracts, а не скрытую реализацию конкретного managed-сервиса. Любая stronger guarantee действует только в явно названной transaction/checkpoint/acknowledgement boundary.
Введите числа или выберите пресет