The difference between Redis, MongoDB and Memcached Redis (memory database)

The difference between Redis, MongoDB and Memcached

Redis (in-memory database)

 

  It is a key-value storage system (distributed internal cache, high-performance key-value database). Similar to Memcached, it supports relatively more value types to be stored, including string (string), list (linked list), set (collection), zset (sorted set -- ordered collection) and hash (hash type). These data types all support push/pop, add/remove, intersection union and difference, and richer operations, and these operations are atomic. On this basis, redis supports sorting in various ways. Like memcached, data is cached in memory to ensure efficiency. The difference is that redis will periodically write updated data to disk or write modification operations to appended record files, and on this basis achieve master-slave (master-slave) synchronization.

 

MongoDB (NoSQL database)

 

   It is a product between relational databases and non-relational databases (databases based on distributed file storage), and is the most feature-rich and most like relational databases among non-relational databases. The data structure it supports is very loose and is a JSON-like bson format, so it can store more complex data types. The biggest feature of Mongo is that the query language it supports is very powerful. Its syntax is somewhat similar to the object-oriented query language. It can almost realize most functions similar to single-table query in relational databases, and it also supports indexing of data.

 

Memcached (Memory Cache)

 

  It is a high-performance distributed memory object caching system for dynamic web applications to reduce database load. It improves the speed of dynamic, database-driven websites by reducing the number of database reads by caching data and objects in memory. Memcached is based on a hashmap that stores key/value pairs. Its daemon (daemon) is written in C, but the client can be written in any language and communicate with the daemon through the memcached protocol.

 

 

 

1. Features

 

1.1 Redis

 

  Support a variety of data structures, such as string (string), list (doubly linked list), dict (hash table), set (set), zset (sort set), hyperloglog (cardinality estimation);

  Supports persistent operations, which can persist aof and rdb data to disk, so as to perform data backup or data recovery operations, which is a better means of preventing data loss;

  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;

  Single-threaded request, all commands are executed serially, and data consistency does not need to be considered under concurrent conditions;

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

  It supports simple transaction requirements, but there are few usage scenarios in the industry and it is not mature.

 

  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);

 

  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;

 

  Support (snapshot, AOF): Relying on snapshots for persistence, AOF enhances reliability and affects performance;

 

  Redis consumes a lot of memory on the string type, you can use dict (hash table) to compress storage to reduce memory consumption;

 

  Both MC and Redis are Key-Value types, which are not suitable for establishing relationships between different datasets, nor for querying and searching. For example, the matching operation of the keys pattern of redis is a disaster for the performance of redis;

 

  Redis 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);

 

  Redis transaction support is relatively weak, and it can only ensure that each operation in the transaction is executed continuously.

 

1.2 MongoDB

 

  Suitable for storage of large amounts of data, relying on the operating system VM for memory management, and eating memory is also relatively powerful, and services should not be combined with other services;

 

  Supports rich data expression, indexing, most similar to relational database, and supports very rich query languages;

 

  Support master-slave, replicaset (internal use of paxos election algorithm, automatic failure recovery), auto sharding mechanism, shielding the client from the failover and segmentation mechanism;

 

  Since version 1.8, the binlog method is used to support the reliability of persistence;

 

  MongoDB does not support transactions;

 

  MongoDB has a built-in data analysis function (mapreduce), others do not support

 

1.3 Memcached

 

  It 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 the daily environment is about 4-6w). Suitable for maximum carrying capacity;

  Supports direct configuration as session handle.

 

  Only supports simple key/value data structures, unlike Redis which can support rich data types;

  It cannot be persisted, the data cannot be backed up, it can only be used for caching, and all data is lost after restart;

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

  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.

 

  Memcached can modify the maximum available memory, using the LRU algorithm.

 

2. Application scenarios

 

2.1 Redis

  It is suitable for systems that have high requirements for reading and writing efficiency, complex data processing business and high security requirements (such as Sina Weibo's counting and Weibo publishing systems, which have high requirements for data security and reading and writing) .

2.2 MongoDB

  It mainly solves the problem of access efficiency of massive data.

2.3 Memcached

  In the dynamic system, the database load is reduced and the performance is improved; as a cache, it is suitable for more reading and less writing, and the large amount of data (such as Renren's large-scale query of user information, friend information, article information, etc.);

  It is 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 data volumes, sharding can be used).

Guess you like

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