Server Sessions

New in version 3.6.

Overview

MongoDB’s server sessions, or logical sessions, are the underlyingframework used by client sessions tosupport Causal Consistency and retryable writes.

Important

Applications use client sessions tointerface with server sessions.

Server sessions are available for replica sets and sharded clusters only.

Command Options

Starting in 3.6, MongoDB drivers associate all operations with a serversession, with the exception of unacknowledged writes. The followingoptions are available for all commands to support association with aserver session:

Important

The mongo shell and the drivers assign these optionsto the commands in the session.

OptionTypeDescription
lsidDocumentThe document that specifies the unique id of the sessionassociated with the command. If the txnNumber isspecified, the lsid is required.
txnNumber64-bit integerA strictly increasing non-negative number that uniquelyidentifies the command in the command’s session.If specified, the command must also include the lsidoption.

For the delete, insert, and updatecommands that take an array of statements, the following option is alsoavailable:

Important

Do not manually set stmtIds. MongoDB sets the stmtIdsto be strictly increasing non-negative numbers.

OptionTypeDescription
stmtIdsArray of 32-bit integersArray of numbers that uniquely identify their respectivewrite operations within the write command.

Sessions Commands

The following commands can be used to list, manage, and kill serversessions throughout MongoDB clusters:

CommandsDescriptions
endSessionsExpires specified server sessions.
killAllSessionsKills all server sessions.
killAllSessionsByPatternKills all server sessions that match the specified pattern.
killSessionsKills specified server sessions.
refreshSessionsRefreshes idle server sessions.
startSessionStarts a new server session.

Sessions and Access Control

If the deployment enforces authentication/authorization, the user mustbe authenticated to start a session, and only that user can use thesession.

Changed in version 3.6.3: To use sessions with $external authentication users (i.e.Kerberos, LDAP, x.509 users), the usernames cannot be greaterthan 10k bytes.

If the deployment does not enforce authentication/authorization, acreated session has no owner and can be used by any user on anyconnection. If a user authenticates and creates a session for adeployment that does not enforce authentication/authorization, that userowns the session. However, any user on any connection may use thesession.

If the deployment transitions to authentication without any downtime, anysessions without an owner cannot be used.

See also

maxSessions