ArticleDirectory
- Efficiency
- Transmission
- Performance
Today, I downloaded an electronic version of MongoDB the definitive guide and browsed the content in it, which is quite rich. Is a supplement to the practical application of the official website documentation. Similar to the official document, the internal principles of MongoDB are rarely described. Only the relevant content is described in the chapter in the appendix.
For most MongoDB users, MongoDB is like a big black box, but if you can understand some structures inside MongoDB, it will help you better understand and use MongoDB.
Bson
In MongoDB, a document is an abstraction of data. It is used in the interaction between the client and the server. All clients (drivers in various languages) use this abstraction, which is represented in bson (Binary JSON ).
Bson is a lightweight binary data format. MongoDB can use bson and store bson as data in disks.
When the client needs to write and query the file, it needs to encode the file in bson format and then send it to the server. Similarly, the results returned by the server are encoded in bson format and then returned to the client.
The bson format is used for the following purposes:
Efficiency
Bson is designed for efficiency and only requires a small amount of space. Even in the worst case, the bson format is more efficient than the JSON format.
Transmission
In some cases, bson sacrifices extra space to facilitate data transmission. For example, the prefix transmitted by a string identifies the length of the string, rather than marking the end at the end of the string. This transmission mode facilitates MongoDB to modify the transmitted data.
Performance
Finally, bson encoding and decoding are very fast. It uses a C-style data representation, which can be used efficiently in various languages.
For more information about bson, see: http://www.bsonspec.org.
Write Protocol
The client accesses the server using a lightweight TCP/IP write protocol. This Protocol is described in detail in MongoDB wiki. It is actually a simple package on bson data. For example, a Data Writing command contains a 20-byte message header (consisting of the message length and the write command ID), the collection name to be written, and the data to be written.
Data Files
In the data folder of MongoDB (the default path is/data/DB), all the files that constitute the database. Each database contains a. Ns file and some data files. The data files will increase as the data volume increases. So if there is a database named Foo, the file that makes up the foo database will be composed of foo. NS, foo.0, foo.1, foo.2, and so on.
Each time a data file is added, it is twice the size of the previous data file, and each data file is up to 2 GB. This design helps prevent databases with a small amount of data from wasting too much space, while ensuring that the databases with a large amount of data have the corresponding space to use.
MongoDB uses the pre-allocation method to ensure stable Write Performance (this method can be disabled using -- noprealloc ). Pre-distribution is performed in the background, and each pre-allocated file is filled with 0. This will allow MongoDB to maintain extra space and spare data files, thus avoiding the blocking caused by disk space allocation due to excessive data growth.
Namespace and disk Zone
Each database is composed of multiple namespaces, and each namespace stores the corresponding types of data. Each collection in the database has its own namespace, and the index file also has a namespace. Metadata of all namespaces is stored in the. Ns file.
The data in the namespace is divided into multiple intervals in the disk, which is called the disk area. In, the database Foo contains three data files, and the third data file is an empty pre-allocated file. The first two data files are divided into different namespaces for the corresponding disk areas.
Displays the characteristics of the namespace and disk area. Each namespace can contain multiple different disk areas, which are not consecutive. As data files grow, the size of the Disk Area corresponding to each namespace increases with the number of times allocated. This aims to balance the space wasted by the namespace and maintain data continuity in a namespace. There is also a namespace to note: $ freelist, which is used to record the disk areas that are no longer in use (the deleted collection or index ). Whenever the namespace needs to be allocated to a new disk area, you will first check whether $ freelist has a suitable Disk Area.
Memory ing storage engine
MongoDB currently supports the memory ing engine. When MongoDB is started, all data files are mapped to the memory, and the operating system hosts all disk operations. This storage engine has the following features:
* MongoDB Memory ManagementCodeAfter all, the related work has been managed by the operating system.
* The MongoDB server uses a huge amount of virtual memory and exceeds the size of the entire data file. Don't worry, the operating system will handle all this.
* MongoDB cannot control the order in which data is written to the disk. As a result, MongoDB cannot implement the writeahead log feature. So if MongoDB wants to provide a durability feature (this feature can be referred to my article on Cassandra: http://www.cnblogs.com/gpcuster/tag/Cassandra/), it needs to implement another storage engine.
* A 32-bit MongoDB server can only use 2 GB Data files for each mongod instance. This is because the address pointer can only support 32 bits.
Others
there are only so many internal structures in MongoDB described in MongoDB the definitive guide. If you really want to make it clear, you may need another book to talk about it. For example, internal JS parsing, query optimization, and index creation. For more information, see Source Code :)
.