The three popular databases of NoSQL are memcached, redis and mongodb


NoSQL generally refers to non relational databases. With the continuous development of the Internet, the traditional relational database has been unable to cope with the websites of the new Internet model, especially the super large-scale and highly concurrent SNS type pure dynamic websites, which has exposed many insurmountable problems, while the non relational database has developed very rapidly due to its own characteristics. The generation of NoSQL database is to solve the challenges brought by large-scale data sets and multiple data types, especially the problems of big data application. Today, we mainly talk about three nosqls that are used more: memcached, redis and mongodb.



1. Memcached can take advantage of multi-core, and the throughput of a single instance is very high, reaching hundreds of thousands of QPS (depending on the byte size of key and value and the performance of server hardware. The peak QPS in daily environment is about 4-6w). Suitable for maximum carrying capacity.

2. Support direct configuration as session handle.


1. Only simple key / value data structures are supported, unlike redis, which supports rich data types.

2. The data cannot be persisted, cannot be backed up, can only be used for caching, and all data is lost after restart.

3. Data synchronization cannot be performed, and data in MC cannot be migrated to other MC instances.

4. For memcached memory allocation, the slab allocation mechanism is used to manage memory. Large differences in value size distribution will reduce memory utilization and cause problems such as kickout when the utilization is low. Users need to pay attention to value design.



1. Support a variety of data structures, such as string, list, dict, set, Zset and hyperloglog.

2. It supports persistence operation. AOF and RDB data can be persisted to disk for data backup or data recovery, which is a better means to prevent data loss.

3. Support data replication through replication. Through the master slave mechanism, you can perform synchronous replication of data in real time, and support multi-level replication and incremental replication. The master slave mechanism is an important means for redis to perform ha.

4. Single thread request, all commands are executed serially, and data consistency does not need to be considered in case of concurrency.

5. Support pub / sub message subscription mechanism, which can be used for message subscription and notification.

6. Support simple transaction requirements, but there are few use scenarios in the industry and they are not mature.


1. Redis can only use a single thread, and the performance is limited by the CPU performance. Therefore, the maximum single instance CPU can reach 5-6W QPS per second (depending on the data structure, data size and server hardware performance. The QPS peak in the daily environment is about 1-2W).

2. It supports simple transaction requirements, but there are few use scenarios in the industry and is not mature, which is both an advantage and a disadvantage.

3. Redis consumes a lot of memory on string types. You can use dict (hash table) to compress storage to reduce memory consumption.



1. With higher write load, mongodb has higher insertion speed.

2. Handle large-scale single tables. When the data table is too large, it can be easily divided.

3. High availability. Setting up M-S is not only convenient but also fast. Mongodb can also realize node (Data Center) failover quickly, safely and automatically.

4. Fast query. Mongodb supports two-dimensional spatial indexes, such as pipes, so it can quickly and accurately obtain data from specified locations. After mongodb is started, the data in the database will be loaded into memory in the form of file mapping. If memory resources are abundant, this will greatly improve the query speed of the database.

5. With the explosive growth of unstructured data, adding columns may lock the whole database or increase the load in some cases, resulting in performance degradation. Due to the weak data structure mode of mongodb, adding a new field will not have any impact on the old table, and the whole process will be very fast.


1. Transaction is not supported.

2. Mongodb occupies too much space.

3. Mongodb does not have mature maintenance tools.

Differences among memcached, redis and mongodb

1. Performance

The performance of the three is relatively high. Generally speaking, memcached is similar to redis and higher than mongodb.

2. Convenience

Memcached has a single data structure.

Redis is richer. In terms of data operation, redis is better and has less network IO times.

Mongodb supports rich data expression and index, which is most similar to relational database, and supports very rich query languages.

3. Storage space

Memcached can modify the maximum available memory using LRU algorithm.

Redis has added its own VM features after version 2.0, breaking through the limitation of physical memory; You can set the expiration time for key value (similar to memcached).

Mongodb is suitable for the storage of large amounts of data. It relies on the operating system VM for memory management. It also eats memory badly. The service should not be together with other services.

4. Availability

Memcached itself has no data redundancy mechanism and is unnecessary; For fault prevention, it relies on mature hash or ring algorithm to solve the jitter problem caused by single point fault.

Redis relies on the client to realize distributed reading and writing; During master-slave replication, every time the slave node reconnects to the master node, it depends on the whole snapshot without incremental replication. Due to performance and efficiency problems, the single point problem is more complex; Automatic sharding is not supported. You need to rely on the program to set a consistent hash mechanism. An alternative is to use active replication (multiple copies of storage) instead of redis’s own replication mechanism, or change to incremental replication (which needs to be implemented by yourself), so as to balance consistency and performance.

Mongodb supports master slave, replicaset (Paxos election algorithm is adopted internally for automatic fault recovery) and auto sharding mechanism, shielding the failover and segmentation mechanism for the client.

5. Reliability

Memcached is not supported. It is usually used for caching to improve performance.

Redis support (snapshot and AOF): it relies on snapshot for persistence. AOF not only enhances reliability, but also affects performance.

Since version 1.8, mongodb has adopted binlog to support the reliability of persistence.

6. Consistency

In concurrent scenarios, memcached uses CAS to ensure consistency.

Redis transaction support is weak, which can only ensure the continuous execution of each operation in the transaction.

Mongodb does not support transactions.

7. Data analysis

Mongodb has built-in data analysis function (MapReduce), which is not supported by the other two.

8. Application scenarios

Memcached: used to reduce database load and improve performance in dynamic systems; Cache and improve performance (sharding is suitable for more reads and less writes. Sharding can be used for a large amount of data).

Redis: high performance operations and operations with small amount of data.

Mongodb: it mainly solves the problem of access efficiency of massive data.

Summary and analysis

1. For simple access to key value (mainly read) data, memcached is better. If you want to support data persistence, the amount is small and the operation is very frequent. Multiple data types (such as collection and hash) and list types are used for advanced applications such as queues, redis is used. However, if the amount of data is large, mongodb is used.

2. Many memcached clients are more mature and stable, and redis protocol is more complex than memcached. Redis can’t be faster than memcached? However, the test results basically show that redis has an absolute advantage.

3. The data in the apsaradb for memcached instance is stored in memory. In case of downtime, power failure in the machine room, or normal upgrade and maintenance of the apsaradb for memcached instance, the data in memory will be lost. Therefore, apsaradb for memcached cannot be used as a persistent data storage service. Redis data is stored in memory. If persistence is not configured, all data will be lost after redis is restarted. Therefore, it is necessary to turn on the persistence function of redis and save the data to the disk. After redis is restarted, data can be recovered from the disk to achieve persistence.

4. Redis and mongodb are generally called redis cache and mongodb database. Redis mainly stores data in memory, and its “cache” nature is much greater than its “data storage” nature. The addition, deletion, modification and query of data is just as simple as variable operation; mongodb is a “data storage” system, which can add many conditions, just as flexible as SQL database.

5. Both mongodb and redis are NoSQL and adopt structured data storage. There are some differences between the two in usage scenarios, which is mainly due to the different processing methods of persistence in the processing process of memory mapping. Mongodb recommends cluster deployment. More consideration should be given to the cluster scheme. Redis pays more attention to process sequential writing. Although it supports clustering, it is only limited to the master-slave mode

recommend:Makeup group words