Comparison of redis, memcache, mongoDB

From the following dimensions, we compared redis, memcache, and mongoDB. (reproduced by others)
1. The performance is
relatively high, and performance should not be a bottleneck for us.
In general, redis is similar to memcache in terms of TPS, but larger than mongodb.
2. Convenience of operation
Memcache has a single data structure. (key-value)
Redis is richer. In terms of data operation, redis is better, with fewer network IO times, and also provides
storage of data structures such as list, set, and hash.
mongodb supports rich data expression, indexing, most similar to relational database, and supports very rich query languages.
3. The size of the memory space and the size of the data volume
Redis added its own VM features after version 2.0 to break through the limitations of physical memory; the
expiration (similar to memcache)
Memcache can modify the maximum available memory, using LRU algorithm. Memcached proxy software magent, for example, establishing
10 4G Memcache clusters is equivalent to having 40G. magent -s 10.1.2.1 -s 10.1.2.2:11211 -b
10.1.2.3:14000 mongoDB is suitable for storage of large amounts of data, relying on the operating system VM for memory management, and eating memory is also more powerful, services
should not be with other services .
4. Availability (single point problem)
For single point problems,
Redis relies on the client to realize distributed read and write; during master-slave replication, every time the slave node reconnects to the master node
, snapshot, and there is no incremental replication. Due to performance and efficiency issues,
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 not use the replication mechanism of redis itself, use its own active replication (multiple storage), or change it to
incremental replication (you need to implement it yourself), the balance between consistency issues and performance
Memcache itself has no data redundancy . There is no need for redundant mechanisms; 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.
5. Reliability (persistence)
For data persistence and data recovery,
redis supports (snapshots, AOF): relying on snapshots for persistence, while
aof
Memcache does not support, usually use Cache is used to improve performance;
MongoDB has used binlog since version 1.8 to support the reliability of persistence
6. Data consistency (transaction support)
Memcache In concurrent scenarios, using cas to ensure consistency Redis transaction support is relatively weak, and can only guarantee Each operation in the transaction is executed continuously
mongoDB does not support transactions
7, data analysis
mongoDB has built-in data analysis function (mapreduce), others do not support
8. Application scenarios
redis: higher performance operations and operations with smaller data volume
memcache: used to reduce database load and improve performance in dynamic systems; do caching, improve performance Performance (suitable for reading more and writing
less , for large amounts of data, sharding can be used)
MongoDB: It mainly solves the problem of access efficiency of massive data.
Table comparison:
memcache redis type in-memory database in-memory database The
data type needs to be fixed when defining the value. The data type is not required
. There are strings, linked lists, sets and ordered sets.
Virtual memory does not support support.
Expiration policy supports support.
Distributed magent master-slave, One master one slave or one master multiple slaves
Storage data security does not support using save to store in dump.rdb
Disaster recovery does not support append only file (aof) for data recovery
Performance
1. Type - memcache and redis both store data In memory, so it is an in-memory database. Of course, memcache can also be used to cache other things, such as pictures and so on.
2. Data type - Memcache needs to specify the byte length of the data when adding data, but redis does not need it.
3. Virtual memory - when physical memory is used up, some values ​​that have not been used for a long time can be swapped to disk.
4. Expiration strategy - memcache is specified when set, such as set key1 0 0 8, that is, it will never expire. Redis can be set
by e.g. expire, e.g. expire name 10.
5. Distributed - set up memcache cluster, use magent to be one master and multiple slaves; redis can be one master and multiple slaves. All can
be one master and one slave.
6. Safe storage of data - memcache is interrupted when the power is turned off, and the data is gone; redis can periodically save to disk.
7. Disaster recovery - memcache is the same as above, after redis is lost, it can be recovered through aof.
Memecache port 11211
yum -y install memcached
yum -y install php-pecl-memcache
/etc/init.d/memcached start memcached -d -p 11211 -u memcached -m 64 -c 1024 -P /var/run/memcached/ memcached.pid
-d start a daemon
-p port
-m allocated memory is M
-c maximum running concurrency -P pid of memcache
//0 compression (whether MEMCACHE_COMPRESSED) 30 seconds expiration time
//delete 5 is timeout <? php
$memcache = new Memcache; $memcache -> connect('127.0.0.1', 11211); $memcache -> set('name','yang',0,30);
if(!$memcache->add(' name','susan',0, 30)) {
//echo 'susan is exist'; }$memcache -> replace('name', 'lion', 0, 300); echo $memcache -> get(' name');
//$memcache -> delete('name', 5);
printf "stats\r\n" | nc 127.0.0.1 11211
telnet localhost 11211 stats quit Exit
the Redis configuration file port 6379
/etc/redis. conf Start Redis
redis-server /etc/redis.conf Insert a value
redis-cli set test "phper.yang" Get the key value
redis-cli get test Shut down Redis
redis-cli shutdown Shut down all
redis-cli -p 6379 shutdown <? php
$redis=new Redis(); $redis->connect('127.0.0.1',6379); $redis->set('test', 'Hello World'); echo $redis->get('test') ; Mongodb
apt-get install mongo mongo can enter the shell command line
pecl install mongo Mongodb is similar to phpmyadmin operating platform RockMongo

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326996151&siteId=291194637