MongoDB has a cool design decision, that is, it can use memory-mappedfile to process read/write requests to data in disk files. This is
MongoDB has a cool design decision, that is, it can use memory-mapped file to process read/write requests to data in disk files. This is
Basic Ideas
MongoDB is used as an in-memory database, that is, it does not allow MongoDB to save data to a disk at all, which has aroused more and more people's interest. This method is extremely useful for the following applications:
If all this can be achieved, it is really elegant: We can skillfully use the MongoDB query/retrieval function without involving disk operations. You may also know that in 99% cases, disk I/O (especially random I/O) is the bottleneck of the system, and disk operations cannot be avoided if you want to write data.
MongoDB has a cool design decision, that is, it can use memory-mapped file to process read/write requests to data in disk files. That is to say, MongoDB does not treat RAM and disk differently. It only regards files as a huge array and then accesses the data in bytes, the rest are handled by the operating system (OS! This design decision allows MongoDB to run in RAM without any modifications.
Implementation Method
All of this is achieved by using a special type of file system called tmpfs. In Linux, it looks like a conventional File System (FS), but it is completely in RAM (unless its size exceeds the RAM size, it can also perform swap, this is very useful !). My server has 32 gb ram. Let's create a 16 GB tmpfs:
# Mkdir/ramdata # mount-t tmpfs-o size = 16000 M tmpfs/ramdata/# dfFilesystem1K-blocksUsed Available Use % Mounted on/dev/xvde15905712 4973924 871792 86%/none153449360 15344936/dev/ shmtmpfs163840000 16384000 0%/ramdata
Next, use the appropriate settings to start MongoDB. To reduce the number of wasted RAM resources, set smallfiles and noprealloc to true. Since it is now based on RAM, this will not reduce performance at all. Using journal is meaningless, so set nojournal to true.
Dbpath =/ramdatanojournal = truesmallFiles = truenoprealloc = true
After MongoDB is started, you will find that it runs very well and the files in the file system appear as expected:
# MongoMongoDB shell version: 2.3.2connecting to: test> db. test. insert ({a: 1})> db. test. find () {"_ id": ObjectId ("51802115eafa5d80b5d2c145"), "a": 1} # ls-l/ramdata/total 65684-rw -------. 1 root 16777216 Apr 30 :52 local.0-rw -------. 1 root 16777216 Apr 30 :52 local. ns-rwxr-xr-x. 1 root root5 Apr 30 :52 mongod. lock-rw -------. 1 root 16777216 Apr 30 :52 test.0-rw -------. 1 root 16777216 Apr 30 :52 test. nsdrwxr-xr-x. 2 root root40 Apr 30 :52 _ tmp
Now let's add some data to verify that it runs completely normally. Now let's add some data and make sure it behaves properly. First we create a 1KB document and then add it to MongoDB for 4 million times:
> Str = ""> aaa = "aaaaaaaaaa" aaaaaaaaaa> for (var I = 0; I <100; ++ I) {str ++ = aaa;}> for (var I = 0; I <4000000; ++ I) {db. foo. insert ({a: Math. random (), s: str}) ;}> db. foo. stats () {"ns": "test. foo "," count ": 4000000," size ": 4544000160," avgObjSize ": 1136.00004," storageSize ": 5030768544," numExtents ": 26," nindexes ": 1, "lastExtentSize": 536600560, "paddingFactor": 1, "systemFlags": 1, "userFlags": 0, "totalIndexSize": 129794000, "indexSizes ": {"_ id _": 129794000}, "OK": 1}