Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the comparison between Memcached, Redis and MongoDB of NoSQL's three databases

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

In this issue, the editor will bring you how the three databases of NoSQL, Memcached and Redis, and MongoDB are compared. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.

Preface

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 pure dynamic websites of SNS type, which has exposed many insurmountable problems, while the non-relational database has developed rapidly because of its own characteristics. The emergence of NoSQL database is to solve the challenges brought by large-scale data collection and multiple data types, especially the application problem of big data. Today, we are mainly talking about the three NoSQL:Memcached, Redis and MongoDB that are widely used.

Memcached

Advantages

Memcached can take advantage of multi-cores, and the throughput of a single instance is extremely high, reaching hundreds of thousands of QPS (depending on the byte size of key and value and the hardware performance of the server, the peak QPS in daily environment is about 4-6w). It is suitable for the degree of carrying capacity.

Direct configuration of session handle is supported.

Shortcoming

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

Cannot be persisted, data cannot be backed up, it can only be used for cache use, and all data is lost after restart.

Data synchronization cannot be performed and data from MC cannot be migrated to another MC instance.

Memcached memory allocation uses Slab Allocation mechanism to manage memory. When there is a large difference in value size distribution, the memory utilization will be reduced, and problems such as kicking out will still occur when the utilization rate is low. Users are required to pay attention to value design.

Redis

Advantages

Support a variety of data structures, such as string (string), list (two-way linked list), dict (hash table), set (collection), zset (sorting set), hyperloglog (cardinality estimation).

Support persistence operation, aof and rdb data can be persisted to disk, thus data backup or data recovery and other operations, a better means to prevent data loss.

Support data replication through Replication, through master-slave mechanism, real-time synchronous data replication, support multi-level replication and incremental replication. Master-slave mechanism is an important means for Redis to carry out HA.

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

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

Simple transaction requirements are supported, but the industry uses few scenarios and is not mature.

Shortcoming

Redis can only use single thread, and its performance is limited by CPU performance, so it is possible for a single instance CPU*** to reach 5-6wQPS per second (depending on data structure, data size and server hardware performance, the peak QPS in daily environment is about 1-2w).

Simple transaction requirements are supported, but the industry uses few scenarios and is immature, which is both an advantage and a disadvantage.

Redis consumes more memory on the string type, so you can use dict (hash Table) compressed storage to reduce memory consumption.

MongoDB

Advantages

The higher the write load, the higher the insertion speed of MongoDB.

Dealing with large-scale single tables, when the data table is too large, you can easily split the table.

High availability, setting up Mmurs is not only convenient but also fast, and MongoDB can also realize node (data center) failover quickly, securely and automatically.

Fast query, MongoDB supports two-dimensional spatial indexes, such as pipes, so you can quickly and accurately retrieve data from specified locations. MongoDB loads the data from the database into memory as a file map after startup. If there are plenty of memory resources, this will greatly improve the query speed of the database.

The explosive growth of unstructured data, the increase in columns may in some cases lock the entire database, or increase the load, resulting in performance degradation, due to MongoDB's weak data structure pattern, adding a new field will not have any impact on the old table, the whole process will be very fast.

Shortcoming

Transactions are not supported.

MongoDB takes up too much space.

MongoDB does not have mature maintenance tools.

The difference between Memcached, Redis and MongoDB

1. Performance

The performance of the three is relatively high, generally speaking: Memcached is similar to Redis, but higher than MongoDB.

two。 Convenience

The data structure of Memcached is single.

Redis is richer, Redis is better in terms of data manipulation, and there are fewer network IO times.

MongoDB supports rich data representation, indexing, most similar to relational databases, and supports a wide range of query languages.

3. Storage space

Memcached can modify the available memory by using the LRU algorithm.

Redis has added its own VM feature since version 2.0 to break the physical memory limit; you can set the expiration time for key value (similar to memcached).

MongoDB is suitable for a large amount of data storage, rely on the operating system VM to do memory management, eat memory is also more powerful, services do not be together with other services.

4. Usability

Memcached itself has no data redundancy mechanism and is not necessary. For fault prevention, we use mature hash or ring algorithm to solve the jitter problem caused by single point of failure.

Redis relies on the client to achieve distributed read and write; in master-slave replication, each time the slave node reconnects the master node, it depends on the entire snapshot, and there is no incremental replication. Due to performance and efficiency problems, the single point problem is more complex; automatic sharding is not supported, and it needs to rely on the program to set a consistent hash mechanism. One alternative is to use your own active replication (multiple copies of storage) or incremental replication (which requires your own implementation) instead of redis's own replication mechanism, a tradeoff between consistency and performance.

MongoDB supports master-slave,replicaset (internal paxos election algorithm, automatic fault recovery), auto sharding mechanism, and shields the client from the failover and segmentation mechanism.

5. Reliability.

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

Redis support (snapshot, AOF): relying on snapshots for persistence, aof enhances reliability while having an impact on performance.

MongoDB has adopted binlog since version 1.8 to support the reliability of persistence.

6. Consistency

Memcached uses cas to ensure consistency in concurrent scenarios.

Redis transaction support is weak and can only guarantee 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), and the other two are not supported.

8. Application scenario

Memcached: used to reduce database load and improve performance in dynamic systems; do caching to improve performance (suitable for reading more and writing less; for large amounts of data, you can use sharding).

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

MongoDB: mainly solve the problem of access efficiency of massive data.

Summary and analysis

It is better to use Memcached to simply access data such as key-value (mainly read). If you want to support data persistence, the amount is not large, the operation is very frequent, multiple data types (such as collections, hashes, etc.), using list types for advanced applications such as queues, use Redis, but if the amount of data is relatively large, use MongoDB.

Many clients of Memcached are more mature and stable, and Redis protocol is more complex than Memcached. Redis can't be faster than Memcached? But the test result is that Redis is absolutely dominant.

The data in the Memcached instance of the cloud database is stored in memory, and the data in memory will be lost in the event of outage, power outage in the computer room, or normal upgrade and maintenance of the Memcached instance of the cloud database. Therefore, the Memcached version of cloud database cannot be used as a persistent data storage service. The data of Redis is stored in memory. If persistence is not configured, all data will be lost after Redis restart. Therefore, you need to enable the persistence feature of Redis to save the data to disk. When Redis restarts, you can recover the data from disk to achieve persistence.

For Redis and MongoDB, it is commonly referred to as Redis cache and MongoDB database. Redis mainly stores data in memory, and the nature of its "cache" is far greater than its "data storage" nature, in which the addition, deletion, modification and query of data is just as simple as variable operation; MongoDB is a "data storage" system, adding, deleting, changing and searching can add a lot of conditions, just like SQL database is flexible.

Both MongoDB and Redis are NoSQL and use structured data storage. There are some differences between the two in the use scene, which is mainly due to the different processing methods of memory mapping and persistence. MongoDB recommends cluster deployment and pays more attention to the cluster scheme. Redis pays more attention to process sequential writing. Although it supports clustering, it is only limited to master-slave mode.

This is how the three databases Memcached and Redis and MongoDB of NoSQL shared by Xiaobian are compared. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report