Database Manual / Self-Managed Deployments / Storage / Storage Engines

In-Memory Storage Engine for Self-Managed Deployments用于自我管理部署的内存存储引擎

The in-memory storage engine is part of general availability (GA) in 64-bit builds. Other than some metadata and diagnostic data, the in-memory storage engine does not maintain any on-disk data, including configuration data, indexes, user credentials, etc.内存存储引擎是64位版本中通用可用性(GA)的一部分。除了一些元数据和诊断数据外,内存中的存储引擎不维护任何磁盘上的数据,包括配置数据、索引、用户凭据等。

By avoiding disk I/O, the in-memory storage engine allows for more predictable latency of database operations.通过避免磁盘I/O,内存存储引擎允许更可预测的数据库操作延迟。

Specify In-Memory Storage Engine指定内存存储引擎

To select the in-memory storage engine, specify:要选择内存中的存储引擎,请指定:

  • inMemory for the --storageEngine option, or the storage.engine setting if using a configuration file.inMemory中的--storageEngine选项,或使用配置文件时的storage.engine设置。
  • --dbpath, or storage.dbPath if using a configuration file. Although the in-memory storage engine does not write data to the filesystem, it maintains in the --dbpath small metadata files and diagnostic data as well temporary files for building large indexes.--dbpathstorage.dbPath(如果使用配置文件)。虽然内存中的存储引擎不向文件系统写入数据,但它在--dbpath中维护了小型元数据文件和诊断数据,以及用于构建大型索引的临时文件。

For example, from the command line:例如,在命令行中:

mongod --storageEngine inMemory --dbpath <path>

Or, if using the YAML configuration file format:或者,如果使用YAML配置文件格式

storage:
engine: inMemory
dbPath: <path>

See inMemory Options for configuration options specific to this storage engine. 有关此存储引擎特有的配置选项,请参阅内存选项Most mongod configuration options are available for use with in-memory storage engine except for those options that are related to data persistence, such as journaling or encryption at rest configuration.除了与数据持久性相关的选项(如日志记录或静态加密配置)外,大多数mongod配置选项都可用于内存存储引擎。

Warning

The in-memory storage engine does not persist data after process shutdown.进程关闭后,内存中的存储引擎不会持久化数据。

Transaction (Read and Write) Concurrency事务(读写)并发

Starting in version 7.0, MongoDB uses a default algorithm to dynamically adjust the maximum number of concurrent storage engine transactions, or read and write tickets. The dynamic concurrent storage engine transaction algorithm optimizes database throughput during cluster overload.从7.0版本开始,MongoDB使用默认算法动态调整并发存储引擎事务或读写票证的最大数量。动态并发存储引擎事务算法在集群过载期间优化了数据库吞吐量。

Note

The dynamic algorithm also results in lower overall ticket usage, even under normal conditions, because the algorithm starts with a much lower baseline number of available tickets. As a result, when upgrading to MongoDB 7.0, you may observe a significant drop in ticket usage, which is expected behavior.即使在正常情况下,动态算法也会导致较低的总票使用率,因为该算法从较低的可用票基线数量开始。因此,当升级到MongoDB 7.0时,您可能会观察到票证使用率显著下降,这是预期的行为。

The maximum number of concurrent storage engine transactions, or read and write tickets, never exceeds 128 read tickets and 128 write tickets and may differ across nodes in a cluster. The maximum number of read tickets and write tickets within a single node are always equal.并发存储引擎事务或读写票证的最大数量永远不会超过128个读票证和128个写票证,并且可能因集群中的节点而异。单个节点内读取票和写入票的最大数量始终相等。

To specify a maximum number of read and write transactions, or read and write tickets, that the dynamic maximum can not exceed, use storageEngineConcurrentReadTransactions and storageEngineConcurrentWriteTransactions.要指定动态最大值不能超过的最大读写事务数或读写票证数,请使用storageEngineConcurrentReadTransactionsstorageEngineConcurrentWriteTransactions

If you want to disable the dynamic concurrent storage engine transactions algorithm, file a support request to work with a MongoDB Technical Services Engineer.如果你想禁用动态并发存储引擎事务算法,请提交支持请求,与MongoDB技术服务工程师合作。

Document Level Concurrency文档级并发

The in-memory storage engine uses document-level concurrency control for write operations. As a result, multiple clients can modify different documents of a collection at the same time.内存中的存储引擎对写入操作使用文档级并发控制。因此,多个客户端可以同时修改集合的不同文档。

Memory Use内存使用

In-memory storage engine requires that all its data (including indexes, oplog if mongod instance is part of a replica set, etc.) must fit into the specified --inMemorySizeGB command-line option or storage.inMemory.engineConfig.inMemorySizeGB setting in the YAML configuration file.内存存储引擎要求其所有数据(包括索引、oplog(如果mongod实例是副本集的一部分)必须符合指定的--inMemorySizeGB命令行选项或YAML配置文件中的storage.inMemory.engineConfig.inMemorySizeGB设置。

By default, the in-memory storage engine uses 50% of physical RAM minus 1 GB.默认情况下,内存存储引擎使用50%的物理RAM减去1GB。

If a write operation would cause the data to exceed the specified memory size, MongoDB returns with the error:如果写操作会导致数据超过指定的内存大小,MongoDB将返回错误:

"WT_CACHE_FULL: operation would overflow cache"

To specify a new size, use the storage.inMemory.engineConfig.inMemorySizeGB setting in the YAML configuration file format:要指定新大小,请使用YAML配置文件格式storage.inMemory.engineConfig.inMemorySizeGB设置:

storage:
engine: inMemory
dbPath: <path>
inMemory:
engineConfig:
inMemorySizeGB: <newSize>

Or use the command-line option --inMemorySizeGB:或者使用命令行选项--inMemorySizeGB

mongod --storageEngine inMemory --dbpath <path> --inMemorySizeGB <newSize>

Durability耐久性

The in-memory storage engine is non-persistent and does not write data to a persistent storage. Non-persisted data includes application data and system data, such as users, permissions, indexes, replica set configuration, sharded cluster configuration, etc.内存中的存储引擎是非持久性的,不会将数据写入持久性存储。非持久化数据包括应用程序数据和系统数据,如用户、权限、索引、副本集配置、分片集群配置等。

As such, the concept of journal or waiting for data to become durable does not apply to the in-memory storage engine.因此,日志或等待数据变得持久的概念不适用于内存中的存储引擎。

If any voting member of a replica set uses the in-memory storage engine, you must set writeConcernMajorityJournalDefault to false.如果副本集的任何投票成员使用内存中的存储引擎,则必须将writeConcernMajorityJournalDefault设置为false

Note

Starting in version 4.2 (and 4.0.13 and 3.6.14 ), if a replica set member uses the in-memory storage engine (voting or non-voting) but the replica set has writeConcernMajorityJournalDefault set to true, the replica set member logs a startup warning.从版本4.2(以及4.0.13和3.6.14)开始,如果副本集成员使用内存中的存储引擎(投票或非投票),但副本集的writeConcernMajorityJournalDefault设置为true,则副本集成员会记录启动警告。

With writeConcernMajorityJournalDefault set to false, MongoDB does not wait for w: "majority" writes to be written to the on-disk journal before acknowledging the writes. writeConcernMajorityJournalDefault设置为false时,MongoDB不会在确认写入之前等待w: "majority"写入写入到磁盘日志中。As such, "majority" write operations could possibly roll back in the event of a transient loss (e.g. crash and restart) of a majority of nodes in a given replica set.因此,在给定副本集中的大多数节点暂时丢失(例如崩溃和重启)的情况下,"majority"写操作可能会回滚。

Write operations that specify a write concern journaled are acknowledged immediately. 指定日志记录的写入关注journaled的写入操作会立即得到确认。When an mongod instance shuts down, either as result of the shutdown command or due to a system error, recovery of in-memory data is impossible.mongod实例因shutdown命令或系统错误而关闭时,无法恢复内存中的数据。

Transactions事务

Transactions are supported on replica sets and sharded clusters where:在以下情况下,副本集和分片集群支持事务:

  • the primary uses the WiredTiger storage engine, and主要使用WiredTiger存储引擎,以及
  • the secondary members use either the WiredTiger storage engine or the in-memory storage engines.次要成员使用WiredTiger存储引擎或内存中存储引擎。

Note

You cannot run transactions on a sharded cluster that has a shard with writeConcernMajorityJournalDefault set to false, such as a shard with a voting member that uses the in-memory storage engine.您无法在具有writeConcernMajorityJournalDefault设置为false的分片的分片集群上运行事务,例如具有使用内存存储引擎的投票成员的分片。

Deployment Architectures部署架构

In addition to running as standalones, mongod instances that use in-memory storage engine can run as part of a replica set or part of a sharded cluster.除了作为独立运行外,使用内存存储引擎的mongod实例还可以作为副本集的一部分或分片集群的一部分运行。

Replica Set副本集

You can deploy mongod instances that use in-memory storage engine as part of a replica set. For example, as part of a three-member replica set, you could have:您可以将使用内存存储引擎的mongod实例部署为副本集的一部分。例如,作为三成员副本集的一部分,您可以有:

  • two mongod instances run with in-memory storage engine.两个mongod实例使用内存存储引擎运行。
  • one mongod instance run with WiredTiger storage engine. 一个mongod实例使用WiredTiger存储引擎运行。Configure the WiredTiger member as a hidden member (i.e. hidden: true and priority: 0).将WiredTiger成员配置为隐藏成员(即hidden: truepriority: 0)。

With this deployment model, only the mongod instances running with the in-memory storage engine can become the primary. 使用此部署模型,只有与内存中存储引擎一起运行的mongod实例才能成为主实例。Clients connect only to the in-memory storage engine mongod instances. 客户端仅连接到内存中的存储引擎mongod实例。Even if both mongod instances running in-memory storage engine crash and restart, they can sync from the member running WiredTiger. 即使在内存存储引擎中运行的两个mongod实例都崩溃并重新启动,它们也可以从运行WiredTiger的成员同步。The hidden mongod instance running with WiredTiger persists the data to disk, including the user data, indexes, and replication configuration information.与WiredTiger一起运行的隐藏mongod实例将数据持久化到磁盘,包括用户数据、索引和复制配置信息。

Note

In-memory storage engine requires that all its data (including oplog if mongod is part of replica set, etc.) fit into the specified --inMemorySizeGB command-line option or storage.inMemory.engineConfig.inMemorySizeGB setting. 内存存储引擎要求其所有数据(包括oplog,如果mongod是副本集的一部分,等等)都符合指定的--inMemorySizeGB命令行选项或storage.inMemory.engineConfig.inMemorySizeGB设置。See Memory Use.请参阅内存使用

Sharded Cluster分片集群

You can deploy mongod instances that use an in-memory storage engine as part of a sharded cluster. 您可以部署使用内存中存储引擎的mongod实例作为分片集群的一部分。The in-memory storage engine avoids disk I/O to allow for more predictable database operation latency. 内存中的存储引擎避免了磁盘I/O,以允许更可预测的数据库操作延迟。In a sharded cluster, a shard can consist of a single mongod instance or a replica set. For example, you could have one shard that consists of the following replica set:在分片集群中,分片可以由单个mongod实例或副本集组成。例如,您可以有一个由以下副本集组成的分片:

  • two mongod instances run with in-memory storage engine两个mongod实例使用内存存储引擎运行
  • one mongod instance run with WiredTiger storage engine. 一个mongod实例使用WiredTiger存储引擎运行。Configure the WiredTiger member as a hidden member (i.e. hidden: true and priority: 0).将WiredTiger成员配置为隐藏成员(即hidden: truepriority: 0)。

To this shard, add the tag inmem. For example, if this shard has the name shardC, connect to the mongos and run sh.addShardTag().taginmem添加到此分片中。例如,如果此分片名为shardC,请连接到mongos并运行sh.addShardTag()

For example,例如,

sh.addShardTag("shardC", "inmem")

To the other shards, add a separate tag persisted .对于其他分片,添加一个单独的persisted(持久)标签。

sh.addShardTag("shardA", "persisted")
sh.addShardTag("shardB", "persisted")

For each sharded collection that should reside on the inmem shard, assign to the entire chunk range the tag inmem:对于应该驻留在inmem分片上的每个分片集合,将标签inmem分配给整个块范围

sh.addTagRange("test.analytics", { shardKey: MinKey }, { shardKey: MaxKey }, "inmem")

For each sharded collection that should reside across the persisted shards, assign to the entire chunk range the tag persisted:对于应该驻留在persisted分片上的每个分片集合,将持久化标签分配给整个块范围

sh.addTagRange("salesdb.orders", { shardKey: MinKey }, { shardKey: MaxKey }, "persisted")

For the inmem shard, create a database or move the database.对于inmem分片,创建一个数据库或移动数据库。

Note

Read concern level "snapshot" is not officially supported with the in-memory storage engine.内存存储引擎不支持读取关注级别"snapshot"