Comparison of memcache, redis, and mongodb

Introduction to NoSQL: Not Only Sql

  • 1. Storage method: The relational database is in the form of a table, which is stored in the rows and columns of the table. It is easy to associate between them to assist in storage, and it is very convenient to extract data. Nosql chunks are grouped together and stored in datasets, like documents, key-value pairs, or graph structures. -2. Storage structure: The relational database corresponds to structured data. The data table has a pre-defined structure (the definition of the column), and the structure describes the form and content of the data. This is critical for data modeling, and while predefined structures bring reliability and stability, modifying these data is difficult. Whereas Nosql database is based on dynamic structure and works with unstructured data. Because Nosql databases are dynamically structured, they can easily adapt to changes in data types and structures.

  • 3. Storage expansion: This may be the biggest difference between the two. Relational databases are vertical expansion, which means that if you want to increase processing power, you need to use faster computers. Because data is stored in relational tables, performance bottlenecks for operations may involve multiple tables and need to be overcome by improving computer performance. Although there is a lot of room for expansion, it will eventually reach the upper limit of vertical expansion. The Nosql database is horizontally scalable, its storage is naturally distributed, and the load can be shared by adding more common database servers to the resource pool.

  • 4. Query method: The relational database operates the database through structured query language (that is, what we usually call SQL). SQL is very powerful in supporting database CURD operations and is the standard usage in the industry. Nosql queries operate data in blocks, using Unstructured Query Language (UnQl), which is not standardized. The concept of a primary key in a relational database table corresponds to the ID of a document stored in Nosql. Relational databases use predefined optimizations (such as indexes) to speed up query operations, while Nosql has simpler and more precise data access patterns.

  • 5. Transactions: Relational databases follow ACID rules (Atomicity, Consistency, Isolation, Durability), while Nosql databases follow BASE principles (Basically Availble, Soft/soft transaction (Soft-state), eventual consistency (Eventual Consistency)). Due to the strong data consistency of relational databases, the support for transactions is very good. Relational databases support fine-grained control over transaction atomicity and are easy to rollback transactions. The Nosql database selects two items in CAP (consistency, availability, partition tolerance), because it is difficult to satisfy all of them in a node-based distributed system, so the support for transactions is not very good, although transactions can also be used , but not the shining point of Nosql.

  • 5. Performance: Relational databases pay a huge price to maintain data consistency, and the read and write performance is relatively poor. In the face of high concurrent read and write performance, the performance is very poor, and the efficiency is very low in the face of massive data. The Nosql storage format is of key-value type and stored in memory, which is very easy to store, and has weak requirements for data consistency. Nosql does not require sql parsing, which improves read and write performance. 6. Authorization method: Relational databases usually include SQL Server, Mysql, and Oracle. The mainstream Nosql databases include redis, memcache, and MongoDb. Most relational databases are paid and expensive, while Nosql databases are usually open source.

Features and Differences of Redis, Memcache, MongoDb Redis

  • Advantages: 1. Support a variety of data structures, such as string (string), list (doubly linked list), dict (hash table), set (collection), zset (sort set), hyperloglog (cardinality estimation) 2. Support persistence operation, the aof and rdb data can be persisted to the disk, so as to perform data backup or data recovery operations, which is a better means to prevent data loss. 3. It supports data replication through Replication. Through the master-slave mechanism, synchronous replication of data can be performed in real time, and multi-level replication and incremental replication are supported. The master-slave mechanism is an important means for Redis to perform HA (High Availability Clustering). 4. For single-threaded requests, all commands are executed serially, and data consistency issues do not need to be considered under concurrent conditions. 5. Support pub/sub message subscription mechanism, which can be used for message subscription and notification. 6. It supports simple transaction requirements, but there are few usage scenarios in the industry and it is not mature.

  • shortcoming:

  1. Redis can only use a single thread, and its performance is limited by CPU performance, so a single instance CPU may reach a maximum of 5-6wQPS per second (depending on the data structure, data size and server hardware performance, the QPS peak in a daily environment is about 1-2w about).
  2. It supports simple transaction requirements, but there are few usage scenarios in the industry and it is immature, which is both an advantage and a disadvantage.
  3. Redis consumes a lot of memory on the string type. You can use dict (hash table) to compress and store to reduce memory consumption.

Memcache

  • advantage:
  1. Memcached can take advantage of multi-core, and the throughput of a single instance is extremely high, which can reach hundreds of thousands of QPS (depending on the byte size of key and value and server hardware performance, the peak QPS in daily environment is about 4-6w). For maximum carrying capacity.
  • shortcoming:
  1. Only simple key/value data structures are supported, unlike Redis which can support rich data types.
  2. It cannot be persisted, the data cannot be backed up, it 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. Memcached memory allocation uses the Slab Allocation mechanism to manage memory. If the value size distribution is very different, the memory utilization will be reduced, and problems such as kicking will still occur when the utilization is low. Users need to pay attention to value design.

MongoDB

  • Advantages: 1. Higher write load, MongoDB has higher insertion speed.
  1. Deal with single tables of very large scale, and can easily split tables when the data table is too large.
  2. High availability, setting up MS is not only convenient and fast, MongoDB can also realize node (data center) failover quickly, safely and automatically.
  3. Fast query, MongoDB supports two-dimensional spatial indexes, such as pipelines, so it can quickly and accurately obtain data from the specified location. After MongoDB starts, the data in the database is loaded into memory in a file-mapped manner. If the memory resources are quite rich, this will greatly improve the query speed of the database.
  4. The explosive growth of unstructured data, adding a column may lock the entire database in some cases, or increase the load and cause performance degradation. Due to the weak data structure mode of MongoDB, adding a new field will not have any impact on the old table, the entire The process will be very fast.
  • shortcoming:
  1. Transactions are not supported.
  2. MongoDB takes up too much space.
  3. MongoDB does not have mature maintenance tools.

The difference between Redis, Memcache and MongoDB

    1. Performance The performance of the three is relatively high. Generally speaking: Memcache is similar to Redis, but higher than MongoDB.
    1. Convenience memcache data structure is single. Redis is richer. In terms of data operation, redis is better and has fewer network IO times. mongodb supports rich data expression, indexing, most similar to relational database, and supports very rich query languages.
    1. The storage space redis has added its own VM features after version 2.0 to break through the limitation of physical memory; the expiration time can be set for the key value (similar to memcache). Memcache can modify the maximum available memory, using the LRU algorithm. mongoDB is suitable for the storage of large amounts of data. It relies on the operating system VM for memory management, and it also consumes more memory. The service should not be combined with other services.
    1. Availability redis relies on the client to realize distributed read and write; when master-slave replication, every time the slave node reconnects to the master node, it depends on the entire snapshot, no incremental replication, due to performance and efficiency issues, so the single-point problem is more complicated; Does not support automatic sharding, you need to rely on the program to set a consistent hash mechanism. An alternative is to use its own active replication (multiple storage) without the replication mechanism of redis itself, or to change to incremental replication (which needs to be implemented by yourself), which is a trade-off between consistency issues and performance. Memcache itself does not have a data redundancy mechanism, nor is it necessary; for fault prevention, a mature hash or ring-based algorithm is used to solve the jitter problem caused by a single point of failure. mongoDB supports master-slave, replicaset (internally adopts paxos election algorithm, automatic failure recovery), auto sharding mechanism, which shields the client from failover and segmentation mechanism.
    1. Reliability redis support (snapshots, AOF): Rely on snapshots for persistence, while AOF enhances reliability, it has an impact on performance. Memcache does not support it, and is usually used for caching to improve performance. Since version 1.8, MongoDB has adopted the binlog method to support the reliability of persistence.
    1. Consistent Memcache In concurrent scenarios, use cas to ensure consistency. Redis transaction support is relatively weak, and can only guarantee that each operation in the transaction is executed continuously. mongoDB does not support transactions.
    1. Data analysis mongoDB has a built-in data analysis function (mapreduce), which is not supported by the other two.
    1. Application scenario redis: higher performance operations and operations with a smaller amount of data. memcache: 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, sharding can be used). MongoDB: mainly solves the problem of access efficiency of massive data.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325162428&siteId=291194637