MongoDB from One Machine to a Multi-Region Cluster
A beginner's guide to where data lives, how capacity grows, and what Atlas manages MongoDB can begin as one database process on one computer. As an application gets more important, you add copies for safety. As its data or traffic outgrows one machine, you split data across several groups of machines. When users are worldwide, you place those groups in more than one region. This article builds…
MongoDB can start as a single database process on one computer. As the application grows, copies are added for safety. When data or traffic outgrows a single machine, it is split across multiple groups of machines. These groups can be placed in different regions to accommodate users worldwide. This article explains this process using MongoDB Atlas and a self-managed MongoDB Community deployment.
In MongoDB, a node is a running program, usually one mongod or mongos process on a VM or container. In a simple production design, one data-bearing node gets its own VM or Kubernetes pod with a persistent disk. The application connects directly to mongod, which stores database documents, indexes, and runtime state.
A replica set is MongoDB's high-availability unit. It consists of multiple nodes holding the same logical data. The primary node accepts normal writes, and secondaries continuously copy the primary's operation log and apply the changes. If the primary fails, the remaining members elect a new primary. A common three-member AWS layout has one primary and two secondaries in different Availability Zones (AZs). If AZ A fails, B and C can elect a primary and continue operating.
Sharding is the process of splitting a collection into ranges based on a chosen field called the shard key. Each range, called a chunk, is placed on one of the shards. Shards can be replica sets, ensuring safety against node failures. A sharded cluster includes shards, a config server replica set, and mongos routers. Shards store the application documents and indexes, while the config servers store the cluster map, including shard locations and chunk placement.
The mongos routers, stateless request routers, use config server metadata to direct requests to the appropriate shard.
As data grows, MongoDB initially spreads chunks among existing shards. When a shard becomes too full or a new shard is added, the balancer can migrate chunks to spread the data and workload. This is horizontal growth, adding more machines to share the work. In contrast, vertical growth involves giving one machine a larger disk or more CPU.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.