MongoDB Core Contributors: No no no, you don't understand!

Source: Internet
Author: User
Keywords Can then if in case

Recently MongoDB in hack news is frequently shot. Many people even claim to hate Mongodb,david Mytton on his blog to expose MongoDB many existing problems. But those who hate have a lot in common, as a comeback: Russell Smith brings a summary of years of work experience. Russell Smith has worked as an OPS and large website scaling advisor and has helped Guardian, Experian, and other companies, MongoDB the co-founder of the London User group. As MongoDB Master (MongoDB officially recognized MongoDB core contributor and sharing its expertise through the community), the infrastructure single server that participates in the work is queried more than 30,000 times per second, and daily active data is more than 1TB.

Below to see Russell to mongodb some common and uncommon problems to make analysis:

32-bit vs 64-bit

Most servers now support 32-bit operating systems, and many new hardware support 64-bit operating systems that allow more RAM.

The MONGODB also publishes 32-bit and 64-bit two-version databases. Due to the memory-mapped files used by MongoDB, the 32-bit version only supports storage of 2G data. The standard replica Set,mongodb only has a single processing strategy--mongod. If you want to store more than 2G of data in the future, please use the 64-bit version of MongoDB. If you have a fragmented installation, the 32-bit version is also available.

Summary: Use a 64-bit version or understand the limitations of a 32-bit version.

File size limit

Unlike RDBMS, where data is stored in rows and columns, MONGODB data is stored in a file. These files use binary storage in a format similar to the JSON-formatted Bson format.

As with other databases, the storage size of a single file is limited. In older versions of MongoDB, a single file is limited to 4M. The new version of the MongoDB file has been supported to a 16M size. Such restrictions may be annoying, but 10gen's opinion is: if this setting keeps bothering you, then whether your design patterns are problematic or if you can use Gridfs with file size limits.

It is generally recommended that you avoid storing too large files and update the various objects stored in the database at irregular intervals. Services such as Amazon S3 or Rackspace CloudFiles are often a better choice, rather than having to overload the infrastructure if necessary.

Summary: Keep each file below 16M, then everything is fine.

Write failed

MongoDB allows for high speed writes and updates by default, and the price is that there is no explicit error notification. By default, most drivers are doing asynchronous, "unsafe" writes-which means that the driver cannot immediately feedback the error message, similar to the MySQL insert delayed. If you want to know if something is successful, you must use GetLastError to manually check the error message.

In some cases, if you need to get an error message immediately after the error occurs, it is easy to implement a "secure" query in most drivers. This is going to kill MongoDB. Different from the advantages of traditional databases.

If you need more performance than a "fully secure" synchronous write, and you want a degree of security, then you can use GetLastError with ' J ' to have MongoDB submit only one log submission and then issue an error report notification. The log will be output to disk at a rate of 100 milliseconds, instead of 60 seconds.

Summary: If you have to write confirmation, you can use secure write or GetLastError.

The weakening of data structure model is not equal to no data structure model

An RDBMS typically has a predefined data structure model: rows and columns of tables, each with a name and data type. If you want to add a column to one of these lines, you must include a column for the entire table.

MongoDB removes this setting and does not have a mandatory model qualification for collection and files. This is beneficial for rapid development and easy modification.

This does not mean that you can disregard the design of the structural model, and a suitable structural model allows you to get the best performance of MongoDB. Read the MongoDB document quickly, or watch the video of these structural model designs.

Schema Design basics

Schema Design at Scale

Schema design principles and Practice

Summary: Design structure model and make full use of MongoDB features.

Modifying a statement by default is only a single file

In a traditional RDBMS, unless the limit clause is used, the modification of the statement will be all matches. However mongodb the equivalent "LIMIT 1" setting is used by default on each query. Although "LIMIT 5" is not possible, you can remove the entire restriction through the following statement:

Db.people.update ({age: {$gt:}}, {$set: {past_it:true}}, False, True)

There are similar options in the official drive-' multi '.

Summary: Multiple file modifications can be done by specifying multi of multiple files as true

Query is case-sensitive

String queries may not evolve as expected-this is due to the MongoDB default case sensitivity.

For example: Db.people.find ({name: ' Russell '}) is different from Db.people.find ({name: ' Russell '}). The ideal solution here is to confirm the data that needs to be queried. You can also query through regular expressions, such as: Db.people.find ({name:/russell/i}), but this can affect performance.

Summary: Queries are case-sensitive and can take advantage of regular expressions at the expense of speed.

No fault tolerance of input data

When you try to insert the wrong type of data into a traditional database, traditional databases typically convert data to predefined types. This is not feasible in MongoDB, however, because MongoDB files are not predefined data models. In this case MongoDB will insert any data you enter.

Summary: Use accurate data types.

About locks

When a resource is shared by more than one part of the code, you need to be sure that the lock must be able to be manipulated in one place.

The older version of MongoDB (PRE 2.0) has a global write lock. This means that there is only one place in the entire server that writes. This can cause the database to stall because of a place locking the overload. This problem has been significantly improved in version 2.0 and has been further enhanced in the current 2.2 release. MongoDB 2.2 uses database-level locks to step up the issue. Also expected collection level locks are scheduled to be launched in the next release.

Still, Russell that most of the applications subject to this limitation are more directly influenced by the MongoDB than by the program itself.

Summary: Use the latest stable version to achieve maximum performance.

About Packages

When installed on class Ubuntu and Debian systems, many people have problems with outdated versions. The solution is simple: using the 10gen official library, installing on Ubuntu and Debian will be as fluent as on Fedora and CentOS.

Summary: Use the official package with most recent versions.

Using even several replica set members

Replica set is an effective way to increase redundancy and improve the performance of MONGODB data cluster. The data is replicated in all nodes and a primary node is selected. If the primary node fails, the other node will be voted as the new primary node.

It is tempting to use two machines in the same replica set, which is cheaper than 3 machines and is the standard style of RDBMS.

But in MongoDB here, the number of members in the same replica set can only be an odd number. If you use an even number of members, the other nodes become read-only when the primary node fails. This occurs because the number of remaining nodes that are selected does not meet the requirements of the voting master.

If you want to save costs and also want to support failover and redundancy enhancements, you can use arbiter. Arbiter is a special replica set member that does not store any user data (which means they can use very small servers).

Summary: You can use even several replica set members, but you can use Arbitter to cut costs.

No join statement

MongoDB does not support join: If you want to retrieve data in multiple collection, you must do multiple queries.

If you think you've done a lot of manual queries, you can redesign your data model to reduce the overall number of queries. MongoDB files can be of any type, you can easily de-normalize the data. This allows it to remain consistent with your application.

Summary: No join may wish to look at how to design a data structure model.

Journaling

MongoDB uses a memory-mapped file and outputs a notification to the disk every 60 seconds, which means that you may lose 60 seconds plus all the data for the duration of the output notification to the hard drive.

To avoid data loss, MongoDB has added journaling from version 2.0 (opened by default). Journaling changes the time from 60 seconds to 100ms. If the database is unexpectedly shut down, it will be re-enabled before it is started to ensure that the database is in a consistent state. This is where MongoDB is closest to the traditional database.

Of course journaling will have a slight effect on performance, about 5%. But for most people the extra security is definitely worth it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.