Open-Source Wikis

/

MongoDB

/

Systems

/

Transport

mongodb/mongo

Transport

The transport layer accepts client connections, frames messages, and hands them to the service entry point. The implementation under src/mongo/transport/ supports three kinds of session: ASIO TCP/TLS, gRPC, and named pipes (used for in-tree fixtures and replay tools).

Purpose

For each accepted client:

  1. Negotiate the transport (TCP+optional TLS, gRPC channel, or named pipe).
  2. Frame each incoming message into a Message containing one or more BSON sections.
  3. Deliver the Message to the per-binary ServiceEntryPoint (src/mongo/db/service_entry_point_shard_role.cpp on mongod, src/mongo/s/service_entry_point_router_role.cpp on mongos).
  4. Pump replies back to the client.

The system is driven by an OperationContext-aware reactor that routes I/O completions onto the right thread or executor.

Directory layout

src/mongo/transport/
├── transport_layer.h            # The abstract TransportLayer interface.
├── transport_layer_manager.cpp  # Hosts multiple TransportLayer impls in one process.
├── session_manager.h            # Tracks per-session state, hooks for SEP delivery.
├── service_entry_point.h        # Abstract base implemented by each role.
├── asio/                        # The default ASIO-based TCP/TLS transport.
├── grpc/                        # The gRPC transport.
└── named_pipe/                  # In-process named-pipe transport.

Connection lifecycle

sequenceDiagram
    participant Client
    participant TL as TransportLayer
    participant Session
    participant SEP as ServiceEntryPoint
    participant Cmd as Command

    Client->>TL: TCP connect
    TL->>Session: accept -> Session(socket)
    Session->>Session: TLS handshake (if enabled)
    Session->>SEP: hello / isMaster
    SEP-->>Session: handshake reply
    Client->>Session: OP_MSG (command)
    Session->>SEP: handleRequest(opCtx, msg)
    SEP->>Cmd: dispatch
    Cmd-->>SEP: reply
    SEP-->>Session: serialize OP_MSG
    Session-->>Client: response
    Client->>Session: close
    Session->>TL: end()

Wire protocol

The over-the-wire format is the MongoDB Wire Protocol: a 16-byte message header followed by an opcode-specific payload. Modern releases use one opcode for almost everything:

  • OP_MSG — a flexible message carrying one or more "sections" (kind 0 BSON document, kind 1 sequence of documents). Used for all commands.

Older opcodes (OP_QUERY, OP_GET_MORE, OP_KILL_CURSORS, OP_INSERT, OP_UPDATE, OP_DELETE) are deprecated and not emitted by current drivers, though the server retains parsing for compatibility. The framing code lives in src/mongo/db/dbmessage.h and src/mongo/rpc/message.h. The OP_MSG section format is in src/mongo/rpc/op_msg.h.

ASIO (TCP/TLS)

The default transport is an ASIO reactor. Notable features:

  • Per-thread reactors — by default each worker thread has its own reactor, scaling cleanly up to many cores.
  • TLS — handled by Boost.ASIO's OpenSSL adapter (or Apple SecureTransport / Schannel on macOS / Windows, depending on the build).
  • Egress connection pool — the executor connection pool (src/mongo/executor/connection_pool*) maintains pooled outbound connections to other servers.

The implementation is in src/mongo/transport/asio/.

gRPC

A gRPC transport lives at src/mongo/transport/grpc/. It plumbs MongoDB messages through gRPC streams, primarily as an alternative for cross-process communication where gRPC's tooling (load balancing, mTLS, observability) is preferable. The gRPC dependency is vendored at src/third_party/grpc/.

Egress / outbound

Egress (server-to-server traffic for replication, sharding, and inter-process commands) goes through the executor connection pool, which:

  • Maintains a per-host pool of egress_connections.
  • Refreshes idle connections.
  • Honors deadlines from the OperationContext.
  • Reports metrics for connection pool exhaustion.

The pool is driven by the executor framework and is described in docs/egress_networking.md.

Priority and load-balancer support

  • The priority port (docs/priority_port.md) is a separate listening port reserved for admin commands so they can be served even when the main port is saturated.
  • Load balancer support (docs/load_balancer_support.md) negotiates session affinity tokens with an upstream load balancer so that retryable writes find their way to the right mongos.

Key source files

File Purpose
src/mongo/transport/transport_layer.h Abstract transport interface.
src/mongo/transport/asio/asio_transport_layer.cpp ASIO TCP/TLS implementation.
src/mongo/transport/grpc/grpc_transport_layer.cpp gRPC implementation.
src/mongo/transport/session.h The per-connection state.
src/mongo/transport/session_manager.h Tracks open sessions, hooks for hello/handshake.
src/mongo/rpc/op_msg.h OP_MSG parser and serializer.
src/mongo/rpc/message.h Message header and length framing.

Integration points

  • The service entry point (SEP) consumes parsed messages and produces replies.
  • The executor framework owns the egress connection pool.
  • Auth runs as part of the handshake (hello/saslStart/saslContinue).
  • Audit hooks fire on connect, disconnect, and command dispatch.
  • The hello command (src/mongo/db/repl/hello/) is the primary entry point for topology discovery.

Entry points for modification

Adding a new transport (e.g. a unix socket implementation) means writing a new TransportLayer and registering it with the TransportLayerManager. Wire-protocol changes start in src/mongo/rpc/. Most contributor-visible work is in tweaking timeouts, connection pool behavior, or the egress connection lifecycle, which is concentrated in src/mongo/executor/connection_pool* and the ASIO implementation.

Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.

Transport – MongoDB wiki | Factory