In-Memory Storage Engine内存存储引擎
On this page本页内容
Changed in version 3.2.6.3.2.6版更改。
Starting in MongoDB Enterprise version 3.2.6, the in-memory storage engine is part of general availability (GA) in the 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.从MongoDB Enterprise 3.2.6版本开始,内存存储引擎是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 thestorage.engine
setting if using a configuration file.inMemory
用于--storageEngine
选项,或storage.engine
设置(如果使用配置文件)。--dbpath
, orstorage.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.--dbpath
或storage.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
配置选项都可用于内存存储引擎,例如日志记录或静态加密配置。
The in-memory storage engine does not persist data after process shutdown.进程关闭后,内存中存储引擎不会持久保存数据。
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 内存存储引擎要求其所有数据(包括索引、oplog(如果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.mongod
实例是副本集的一部分)等)必须符合YAML配置文件中指定的--inMemorySizeGB
命令行选项或storage.inMemory.engineConfig.inMemorySizeGB
设置。
By default, the in-memory storage engine uses 50% of physical RAM minus 1 GB.默认情况下,内存中存储引擎使用50%的物理RAM减去1 GB。
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 要指定新的大小,请使用YAML配置文件格式中的storage.inMemory.engineConfig.inMemorySizeGB
setting in the YAML configuration file format: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
.
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.
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. 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.
Write operations that specify a write concern journaled
are acknowledged immediately. 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.
Transactions事务
Starting in MongoDB 4.2, transactions are supported on replica sets and sharded clusters where:从MongoDB 4.2开始,在副本集和分片集群上支持事务,其中:
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存储引擎或内存中存储引擎。
In MongoDB 4.0, only replica sets using the WiredTiger storage engine supported transactions.在MongoDB 4.0中,只有使用WiredTiger存储引擎的副本集支持事务。
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. Configure the WiredTiger member as a hidden member (i.e.hidden: true
andpriority: 0
).
With this deployment model, only the mongod
instances running with the in-memory storage engine can become the primary. Clients connect only to the in-memory storage engine mongod
instances. Even if both mongod
instances running in-memory storage engine crash and restart, they can sync from the member running WiredTiger. The hidden mongod
instance running with WiredTiger persists the data to disk, including the user data, indexes, and replication configuration information.
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. See Memory Use.
Sharded Cluster分片集群
You can deploy mongod
instances that use an in-memory storage engine as part of a sharded cluster. The in-memory storage engine avoids disk I/O to allow for more predictable database operation latency. 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:
two两个mongod
instances run with in-memory storage enginemongod
实例使用内存存储引擎运行- one
mongod
instance run with WiredTiger storage engine. Configure the WiredTiger member as a hidden member (i.e.hidden: true
andpriority: 0
).
To this shard, add the 将tag
inmem
. tag
inmem
添加到这个分片中。For example, if this shard has the name 例如,如果这个分片的名称为shardC
, connect to the mongos
and run sh.addShardTag()
.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
:
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
分片,创建一个数据库或移动数据库。
Read concern level 内存存储引擎不正式支持读取关注级别的"snapshot"
is not officially supported with the in-memory storage engine."snapshot"
。