About MongoDB, redis,memcache between the confusion and confusion between the relationship and role

Source: Internet
Author: User
Tags explode

First of all, I use the situation:

The first used memcache, a server-side cache of key-value pairs, used to store some commonly used data that is not very large, but that requires rapid response

Then, in another place, Redis is used, and then the next Redis is studied. A look, shows that they installed the PHP extension, because there is a server on the Redis service side, their own local is not installed, in fact, the usage and memcache basically the same, may be a few parameters are different. Of course, the effect of their cache is not the same, specific where not the same, a bit of information, and their summary

1. Redis and memcache all store data in memory, which is the memory database. But memcache can also be used to cache other things, examples, videos, and so on.
2. Data type--memcache Specify the byte length of the data when adding data, for example:
Set Key3 0 0 8
Lxsymcto
STORED
Redis does not need, for example: Redis 127.0.0.1:6379>set key2 "Lxsymblog"
Ok
Redis 127.0.0.1:6379>get Key2
"Lxsymblog"
3. Virtual memory--redis When the physical memory is exhausted, you can swap some long-unused value to disk
4. Expiration policy--memcache is specified when set, such as set Key1 0 0 8, which never expires. Redis can be set by example expire, for example expire name 10
5, distributed-set memcache cluster, using Magent to do a master more from; Redis can do a master multi-slave. Can be a master one from
6, storage data security--memcache hang up, the data is gone; Redis can be saved to disk periodically (persisted)
7, after the disaster recovery--memcache hangs, the data cannot restore; Redis data can be recovered by aof after loss

From the following several dimensions, the Redis, Memcache, MongoDB made a comparison, welcome to shoot Bricks

1. Performance

are relatively high, performance is not a bottleneck for us

In general, the TPS is about the same as Redis and Memcache, more than MongoDB

2, the convenience of the operation

Memcache Data Structure Single

Redis is rich, data manipulation, Redis better, less network IO times

MongoDB supports rich data expression, index, most similar relational database, support query language is very rich

3. Size of memory space and amount of data

Redis has added its own VM features after the 2.0 release, breaking the limits of physical memory; You can set the expiration time for key value (similar to memcache)

Memcache can modify the maximum available memory, using the LRU algorithm

MongoDB is suitable for large data storage, depends on operating system VM to do memory management, eat memory is also very bad, service not with other services together

4. Availability (single point of issue)

For a single point of problem,

Redis, which relies on clients for distributed reads and writes, and master-slave replication relies on the entire snapshot every time the primary node is reconnected from the node, without incremental replication, due to performance and efficiency issues,

Therefore, the single point problem is more complicated, the automatic sharding is not supported, and the dependent program is required to set the consistent hash mechanism.

An alternative is to use your own proactive replication (multiple storage) instead of Redis's own replication mechanism, or change to incremental replication (you need to implement it yourself), consistency issues and performance tradeoffs

Memcache itself has no data redundancy mechanism, it is not necessary, for fault prevention, relying on mature hash or ring algorithm to solve the single point of failure caused by the jitter problem.

MongoDB supports Master-slave,replicaset (internal using Paxos election algorithm, automatic fault recovery), auto sharding mechanism, blocking the failover and segmentation mechanism to the client.

5. Reliability (persistent)

For data persistence and data recovery,

Redis Support (snapshot, AOF): dependent on snapshots for persistence, AOF enhances reliability while impacting performance

Memcache not supported, usually used in cache, improve performance;

MongoDB supports persistent reliability from the 1.8 release with the binlog approach

6. Data consistency (transactional support)

Memcache in concurrent scenarios, with CAS to ensure consistency

Redis transaction support is weak and can only guarantee continuous execution of each operation in a transaction

MongoDB does not support transactions

7. Data analysis

MongoDB has built-in data analysis capabilities (MapReduce), others do not support

8. Application Scenario

Redis: More performance operations and calculations with smaller data volumes

Memcache: Used to reduce database load in dynamic system, improve performance, cache, improve performance (suitable for read and write less, for a large amount of data, you can use sharding)

MongoDB: The main solution to the massive data access efficiency problem

I've been studying Key-value's storage for a while, and simply remember how it feels. The installation and use of some memcache and Redis will not be mentioned. Just simply say the difference between the two options. Some impressions and test results may not be enough to explain the problem, please correct me if there is anything wrong with you. Because these two days in the process of learning to find that they have been correcting the shortcomings of their own understanding, every day, will negate the idea of the morning before. All right, crap less.

A study of 500,000 data stores found that:

Number of single instruction executions per second

Memcache approx. 30,000 times

Redis approx. 10,000 times

Moreover, one of the great advantages of memcache is that it is possible to set the expiration time directly through a function, and redis needs two functions to set both the key value pair and the expiration time, that is, the efficiency of redis at this point becomes the original half, that is, 5,000 times, which for most of the demand, a bit too slow.

The test code for Memcache is as follows:

<?php

$mem = new Memcache;

$mem->connect ("127.0.0.1", 11211);

$time _start = Microtime_float ();

Save data

for ($i = 0; $i < 100000; $i + +) {

$mem->set ("key$i", $i, 0, 3);

}

$time _end = Microtime_float ();

$run _time = $time _end-$time _start;

echo "spents $run _time s \ n";

function Microtime_float ()

{

List ($usec, $sec) = Explode ("", Microtime ());

return (float) $usec + (float) $sec);

}

?>

The test code for Redis is as follows: redis1.php This code takes about 10 seconds

<?php

Connection

$redis = new Redis ();

$redis->connect (' 127.0.0.1 ', 6379);

$time _start = Microtime_float ();

Save data

for ($i = 0; $i < 100000; $i + +) {

$redis->sadd ("key$i", $i);

}

$time _end = Microtime_float ();

$run _time = $time _end-$time _start;

echo "spents $run _time s \ n";

Close connection

$redis->close ();

function Microtime_float ()

{

List ($usec, $sec) = Explode ("", Microtime ());

return (float) $usec + (float) $sec);

}

?>

If you need to set the expiration time while setting the key value, it takes about 20 seconds to execute, and the test code is as follows: redis2.php

<?php

Connection

$redis = new Redis ();

$redis->connect (' 127.0.0.1 ', 6379);

$time _start = Microtime_float ();

Save data

for ($i = 0; $i < 100000; $i + +) {

$redis->sadd ("key$i", $i);

$redis->expire ("Key$i", 3);

}

$time _end = Microtime_float ();

$run _time = $time _end-$time _start;

echo "spents $run _time s \ n";

Close connection

$redis->close ();

function Microtime_float ()

{

List ($usec, $sec) = Explode ("", Microtime ());

return (float) $usec + (float) $sec);

}

?>

Later on the internet discovered that Redis has a magical function called transactions, through the multi atom of a block of code executed sequentially, so as to achieve a complete function module execution. Unfortunately, the test found that the execution of the code in the multi way did not reduce the number of requests, instead of executing the multi instruction and the EXEC command to send the request, thus turning the run time to four times times the original, that is, four instructions run time. The test code is as follows: redis3.php

<?php

Connection

$redis = new Redis ();

$redis->connect (' 127.0.0.1 ', 6379);

$time _start = Microtime_float ();

Save data

for ($i = 0; $i < 100000; $i + +) {

$redis->multi ();

$redis->sadd ("key$i", $i);

$redis->expire ("Key$i", 3);

$redis->exec ();

}

$time _end = Microtime_float ();

$run _time = $time _end-$time _start;

echo "spents $run _time s \ n";

Close connection

$redis->close ();

function Microtime_float ()

{

List ($usec, $sec) = Explode ("", Microtime ());

return (float) $usec + (float) $sec);

}

?>

Problems have bottlenecks, there are a lot of companies need massive data processing, 5,000 times per second far from meeting demand, and then because the Redis master-slave server than memcache have a greater advantage, for the sake of future data, have to use Redis, this time there is a new way, That is, Phpredis provides the Pipline function, this function can truly encapsulate a few pieces of code as a request, which greatly improves the speed of operation, 500,000 times the data execution only 58 seconds. The test code is as follows: redis4.php

<?php

Connection

$redis = new Redis ();

$redis->connect (' 127.0.0.1 ', 6379);

$time _start = Microtime_float ();

Save data

for ($i = 0; $i < 100000; $i + +) {

$pipe = $redis->pipeline ();

$pipe->sadd ("key$i", $i);

$pipe->expire ("Key$i", 3);

$replies = $pipe->execute ();

}

$time _end = Microtime_float ();

$run _time = $time _end-$time _start;

echo "spents $run _time s \ n";

Close connection

$redis->close ();

function Microtime_float ()

{

List ($usec, $sec) = Explode ("", Microtime ());

return (float) $usec + (float) $sec);

}

?>

This operation can be very perfect to package the assignment operation and set expiration time operation to a request to execute, greatly improve the efficiency of operation.

Redis Installation: http://mwt198668.blog.163.com/blog/static/48803692201132141755962/

Memcache Installation: http://blog.csdn.net/barrydiu/article/details/3936270

Redis Settings Master-slave server: http://www.jzxue.com/fuwuqi/fuwuqijiqunyuanquan/201104/15-7117.html

Memcache setting up a master-slave server: http://www.cnblogs.com/yuanermen/archive/2011/05/19/2051153.html

Original address: http://blog.csdn.net/a923544197/article/details/7594814

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.