redis & memcache 性能比较

redismemcache很是像的,都是key,value的方式,将数据存放内存中。最近在学习redis,在网上看了一些这方面的资料,有三种观点:php

  1. redis读写内存比memcache
  2. memcache读写内存比redis
  3. memcache读写内存比redis快,可是redis总体性能优于memcache
    因此我作了一下测试.

redis的测试结果

第一次

root@ubuntu:/home/yamatamain/download/webbench-1.5# webbench -c 10000 -t 30 http://localhost/php-redis/test_redis.php
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.
Benchmarking: GET http://localhost/php-redis/test_redis.php
10000 clients, running 30 sec.
Speed=48324 pages/min, 40318471 bytes/sec.
Requests: 22599 susceed, 1563 failed.
telnet 127.0.0.1 6379

telnet登陆一下,把test对应的值清除掉,保重测试的公平性web

del test

第二次

root@ubuntu:/home/yamatamain/download/webbench-1.5# webbench -c 10000 -t 30 http://localhost/php-redis/test_redis.php
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.
Benchmarking: GET http://localhost/php-redis/test_redis.php
10000 clients, running 30 sec.
Speed=53570 pages/min, 41217689 bytes/sec.
Requests: 23106 susceed, 3679 failed.
telnet 127.0.0.1 6379
del test

第三次

root@ubuntu:/home/yamatamain/download/webbench-1.5# webbench -c 10000 -t 30 http://localhost/php-redis/test_redis.php
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.
Benchmarking: GET http://localhost/php-redis/test_redis.php
10000 clients, running 30 sec.
Speed=49450 pages/min, 39694073 bytes/sec.
Requests: 22301 susceed, 2424 failed.
telnet 127.0.0.1 6379
del test

memcache测试结果

第一次
root@ubuntu:/home/yamatamain/download/webbench-1.5# webbench -c 10000 -t 30 http://localhost/php-redis/test_memcache.php
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.
Benchmarking: GET http://localhost/php-redis/test_memcache.php
10000 clients, running 30 sec.
Speed=61632 pages/min, 52228667 bytes/sec.
Requests: 29205 susceed, 1611 failed.
telnet 127.0.0.1 11211

telnet登陆一下,把test1对应的值清除掉,保重测试的公平性redis

delete test1

第二次

root@ubuntu:/home/yamatamain/download/webbench-1.5# webbench -c 10000 -t 30 http://localhost/php-redis/test_memcache.php
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.
Benchmarking: GET http://localhost/php-redis/test_memcache.php
10000 clients, running 30 sec.
Speed=64160 pages/min, 52601449 bytes/sec.
Requests: 29426 susceed, 2654 failed.
telnet 127.0.0.1 11211
delete test1

第三次

root@ubuntu:/home/yamatamain/download/webbench-1.5# webbench -c 10000 -t 30 http://localhost/php-redis/test_memcache.php
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.
Benchmarking: GET http://localhost/php-redis/test_memcache.php
10000 clients, running 30 sec.
Speed=65190 pages/min, 52506614 bytes/sec.
Requests: 29348 susceed, 3247 failed.
telnet 127.0.0.1 11211
delete test1

从上面比较结果,能够看出,memcacheredis快的。rediskey,value的管理,更灵活。有不少人把redis归于nosql的范围,细细想,还真是那么一回事。redis还能够把内在中的数据,放到磁盘中,这一点上,redis更像memcachedb。那么问题来了,究竟是用redis仍是memcached呢?
查到下面对应的资料,是来自redis做者的说法(stackoverflow上面)。sql

You should not care too much about performances. Redis is faster per corewith small values, but memcached is able to use multiple cores with a single executable and TCP port without help from the client. Also memcached is faster with big values in the order of 100k. Redis recently improved a lot about big values (unstable branch) but still memcached is faster in this use case.The point here is: nor one or the other will likely going to be your bottleneck for the query-per-second they can deliver. You should care about memory usage. For simple key-value pairs memcached is more memory efficient. If you use Redis hashes, Redis is more memory efficient. Depends on the use case.ubuntu

You should care about persistence and replication, two features only available in Redis. Even if your goal is to build a cache it helps that after an upgrade or a reboot your data are still there.缓存

You should care about the kind of operations you need. In Redis there are a lot of complex operations, even just considering the caching use case, you often can do a lot more in a single operation, without requiring data to be processed client side (a lot of I/O is sometimes needed). This operations are often as fast as plain GET and SET. So if you don’t need just GEt/SET but more complex things Redis can help a lot (think at timeline caching).服务器

翻译以下[1]:

  • 没有必要过多的关注性能。因为Redis只使用单核,而Memcached可使用多核,因此在比较上,平均每个核上Redis在存储小数据时比Memcached性能更高。而在100k以上的数据中,Memcached性能要高于Redis,虽然Redis最近也在存储大数据的性能上进行优化,可是比起Memcached,仍是稍有逊色。说了这么多,结论是,不管你使用哪个,每秒处理请求的次数都不会成为瓶颈。数据结构

  • 你须要关注内存使用率。对于key-value这样简单的数据储存,memcache的内存使用率更高。若是采用hash结构,redis的内存使用率会更高。固然,这些都依赖于具体的应用场景。nosql

  • 你须要关注关注数据持久化和主从复制时,只有redis拥有这两个特性。若是你的目标是构建一个缓存在升级或者重启后以前的数据不会丢失的话,那也只能选择rediside

  • 你应该关心你须要的操做。redis支持不少复杂的操做,甚至只考虑内存的使用状况,在一个单一操做里你经常能够作不少,而不须要将数据读取到客户端中(这样会须要不少的IO操做)。这些复杂的操做基本上和纯GETPOST操做同样快,因此你不仅是须要GET/SET而是更多的操做时,redis会起很大的做用。

对于二者的选择仍是要看具体的应用场景,若是须要缓存的数据只是key-value这样简单的结构时,我在项目里仍是采用memcache,它也足够的稳定可靠。若是涉及到存储,排序等一系列复杂的操做时,毫无疑问选择redis

redismemecache的不一样在于:

  • 存储方式
  • memecache 把数据所有存在内存之中,断电后会挂掉,数据不能超过内存大小
  • redis有部份存在硬盘上,这样能保证数据的持久性,支持数据的持久化(笔者注:有DUMPAOF日志两种持久化方式,在实际应用的时候,要特别注意配置文件快照参数,要不就颇有可能服务器频繁满载作dump)。
  • 数据支持类型:
    • redis在数据支持上要比memecache多的多。
  • 使用底层模型不一样:
    • 新版本的redis直接本身构建了VM 机制 ,由于通常的系统调用系统函数的话,会浪费必定的时间去移动和请求。
  • 运行环境不一样:
    redis目前官方只支持LINUX,从而省去了对于其它系统的支持,这样的话能够更好的把精力用于本系统环境上的优化,虽而后来微软有一个小组为其写了补丁。可是没有放到主干上

我的总结一下,有持久化需求或者对数据结构和处理有高级要求的应用,选择redis,其余简单的key/value存储,选择memcache。

相关文章
相关标签/搜索