2. Redis不僅僅支持簡單的k/v類型的數據,同時還提供list,set,hash等數據結構的存儲。
3. Redis支持數據的備份,即master-slave模式的數據備份。
4. Redis支持數據的持久化,可以將內存中的數據保持在磁盤中,重啟的時候可以再次加載進行使用。
Redis在很多方面具備數據庫的特征,或者說就是壹個數據庫系統,而Memcached只是簡單的K/V緩存
來看下Redis作者對比redis和memcache
來源:《Is memcached a dinosaur in comparison to Redis?》(相比Redis,Memcached真的過時了嗎?)
You
should not care too much about performances. Redis is faster per core
with 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.
沒
有必要過多的關心性能,因為二者的性能都已經足夠高了。由於Redis只使用單核,而Memcached可以使用多核,所以在比較上,平均每壹個核上
Redis在存儲小數據時Memcached性能更高。而在100k以上的數據中,Memcached性能要高於Redis,雖然Redis最近也在存儲
大數據的性能上進行優化,但是比起Memcached,還是稍有遜色。說了這麽多,結論是,無論妳使用哪壹個,每秒處理請求的次數都不會成為瓶頸。(比如
瓶頸可能會在網卡)
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.
如果要說內存使用效率,使用簡單的key-value存儲的話,Memcached的內存利用率更高,而如果Redis采用hash結構來做key-value存儲,由於其組合式的壓縮,其內存利用率會高於Memcached。當然,這和妳的應用場景和數據特性有關。
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.
如果妳對數據持久化和數據同步有所要求,那麽推薦妳選擇Redis,因為這兩個特性Memcached都不具備。即使妳只是希望在升級或者重啟系統後緩存數據不會丟失,選擇Redis也是明智的。
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).
當
然,最後還得說到妳的具體應用需求。Redis相比Memcached來說,擁有更多的數據結構和並支持更豐富的數據操作,通常在Memcached裏,
妳需要將數據拿到客戶端來進行類似的修改再set回去。這大大增加了網絡IO的次數和數據體積。在Redis中,這些復雜的操作通常和壹般的
GET/SET壹樣高效。所以,如果妳需要緩存能夠支持更復雜的結構和操作,那麽Redis會是不錯的選擇。
1、 Redis和Memcache都是將數據存放在內存中,都是內存數據庫。不過memcache還可用於緩存其他東西,例如圖片、視頻等等。
2、Redis不僅僅支持簡單的k/v類型的數據,同時還提供list,set,hash等數據結構的存儲。
3、虛擬內存--Redis當物理內存用完時,可以將壹些很久沒用到的value 交換到磁盤
4、過期策略--memcache在set時就指定,例如set key1 0 0 8,即永不過期。Redis可以通過例如expire 設定,例如expire name 10
5、分布式--設定memcache集群,利用magent做壹主多從;redis可以做壹主多從。都可以壹主壹從
6、存儲數據安全--memcache掛掉後,數據沒了;redis可以定期保存到磁盤(持久化)
7、災難恢復--memcache掛掉後,數據不可恢復; redis數據丟失後可以通過aof恢復
8、Redis支持數據的備份,即master-slave模式的數據備份。
==================
Redis和Memcache,測試服務器是壹個個人PC:配置壹般(2CPU\2G內存),操作系統安裝的CentOS。
1、redis memcache都是空的時候
redis設置100000個值開始時間:1311742106
redis設置100000個值時間:1311742123 耗時:16.8817
memcache設置100000個值開始時間:1311742123
memcache設置100000個值結束時間:1311742138 耗時:15.2574
2、redis和memcache有數據時:
redis設置100000個值開始時間:1311742721
redis設置100000個值時間:1311742738 耗時:17.3401
memcache設置100000個值開始時間:1311742738
memcache設置100000個值結束時間:1311742753 耗時:15.6892
3、分別測試redis和memcache獲取數據速度:
redis數組獲取100000個值開始時間:1311742144
redis數組獲取100000個值時間:1311742398 耗時:253.9285
redis逐個獲取100000個值開始時間:1311742398
redis逐個獲取100000個值時間:1311742414 耗時:16.339
memcache數組獲取100000個值開始時間:1311742414
memcache數組獲取100000個值結束時間:1311742415 耗時:0.8022
memcache逐個獲取100000個值開始時間:1311742415
memcache逐個獲取100000個值結束時間:1311742428 耗時:13.38
就以上測試結果,Redis和Memcache在寫入性能上面差別不大,讀取性能上面尤其是批量讀取性能上面Memcache全面勝出,當然Redis也有自己的優點:比如數據持久化、支持更多的數據結構(Set List ZSet)等。