mongos - MongoDB Manual v7.0

The Nugget

  • mongos is the critical routing component in a MongoDB sharded cluster, managing how queries and write operations are directed to different shards while using cached metadata for efficiency.
  • Applications interact solely with mongos, which minimizes system resource consumption by having no persistent state.

Make it stick

  • 🔄 mongos routes operations, redistributing tasks among connected shards using cached metadata.
  • Targeted Operations are quicker as they use the shard key to pinpoint exact data locations.
  • 🌐 Broadcast Operations query all shards, often leading to longer execution times due to waiting for all responses.
  • 📈 Hedged Reads send operations to two replica members, speeding up results collection by taking the first response.

Key insights

Routing Process

  • The routing process by mongos involves:
    1. Identifying shards relevant to a given query.
    2. Establishing cursors for those shards.
    3. Merging results from shards and returning a consolidated response.

Query Modifiers

  • Sorting and limits are handled by adjusting the operation on each shard, but skip() processing must occur post-query since skips cannot be sent to shards.
  • Read preferences dictate which replica to read from, evaluated for each operation.

Targeted vs. Broadcast Operations

  • Targeted Operations leverage shard keys to efficiently route queries directly to necessary shards.
  • Broadcast Operations mean no shard key was present, leading to queries sent to all shards, which can slow down response times.

Security and Authentication

  • Self-Managed Internal/Membership Authentication is required for intra-cluster communications to ensure security.
  • Role-Based Access Control (RBAC) restricts unauthorized access and requires proper authentication settings to enforce user control.

Aggregation Behavior

  • Queries with aggregation that require merging results may need to run on either the primary shard or one randomly selected, based on stages specified in the pipeline.

Key quotes

  • "mongos instances route queries and write operations to shards in a sharded cluster."
  • "Applications never connect or communicate directly with the shards."
  • "When a shard receives a query, it uses the most efficient index available to fulfill that query."
  • "For sharded clusters, mongos applies the read preference when reading from the shards."
  • "The performance of a broadcast operation depends on the overall load of the cluster, as well as variables like network latency."
This summary contains AI-generated information and may be misleading or incorrect.