redis & memcache 性能比較

redismemcache很是像的,都是key,value的方式,將數據存放內存中。最近在學習redis,在網上看了一些這方面的資料,有三種觀點:php

  1. redis讀寫內存比memcache
  2. memcache讀寫內存比redis
  3. memcache讀寫內存比redis快,可是redis總體性能優於memcache 因此我作了一下測試.

###redis的測試結果 ####第一次web

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對應的值清除掉,保重測試的公平性redis

del test

####第二次sql

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

####第三次ubuntu

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對應的值清除掉,保重測試的公平性服務器

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上面)。nosql

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.ide

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的內存使用率會更高。固然,這些都依賴於具體的應用場景。

  • 你須要關注關注數據持久化和主從複製時,只有redis擁有這兩個特性。若是你的目標是構建一個緩存在升級或者重啓後以前的數據不會丟失的話,那也只能選擇redis

  • 你應該關心你須要的操做。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。

相關文章
相關標籤/搜索