Scaling MongoDB: Essential strategies explained

mongodb
mongodbscalingmongodb-sharding

Sharding

Sharding is a method for distributing data across multiple machines.
Database systesm with large datasets or high throughput appplication can challenge the capacity of a single server. For example, high query rates can exhaust the CPU capacity of the server. Working set sizes largen then the system's RAM stress the I/O capacity of disk drives. Organizations typically choose between vertical scaling(upgrading single server's resources) and horizontal scaling(distributing workloads across multiple machines)

It is recommanded starting with a one shard cluster when you are building a new application regardless of your need for multiple shard.

When to choose sharded clusters

MongoDB sharding delivers sustainable scaling without the complexity traditionally associated with distributed databases for;

  • Cost optimization: Vertical scaling(adding more resources CPU, RAM or storage to a single server) eventually becomes expensive compares to horizontal scaling
  • High concurrent collection access: When several collections compete for the same server resource, performance bottlenekcs can emerge even before any single collection becomes too large.
  • High-throughput workloads: Applications with high read/write volumes benefit from distributing the traffic across multiple machines, improving the performance and reducing bottlenecks
  • Large data sets:
  • Multitenant applications: Applications serving multiple customers benefit from having dedicatd shards per tenant, providing performance isolation
  • Global deployments: When your users are spread across different regions, distributing data acroos geographically positioned sharss reduced latency and enchanges the user experience

When your database reaches 60-70% of resource utilization(RAM, vCPUs or storage), adding more shards should be considered.

MongoDB Sharding Strategies

There are two primary ways to distrubute workloads in a sharded cluster.

  1. Moving collections on dedicated shards: Entire collections are assigned to specific shards,opzimzing the performance by distributing workloads strategically.

  2. Partitioning a collection acress multiple shards: A single collection is split across shards using a shard key, distributing data more evenly for scalability

These approached can be used independently or combined depending on your application's requirements.

MongoDB sharding architecture

A MongoDB sharded cluster consists of the following components:

  • A Shard: Each shard contains a subset of the sharded data. Each shard must be deployed as a replica set.
  • Mongos: The mongos acts as a query router prvoding an interface between client applications and sharded cluster
  • Config server replica set: Config servers stores the metadata and the configuration settings for the cluster

MongoDB shards data at the collection level, distributing the collection data acress the shards in the cluster