如何選擇redis和memcache

我這段時間在用redis,感覺挺方便的,但比較疑惑在選擇內存數據庫的時候到底什麼時候選擇redis,什麼時候選擇memcache,然後就查到下面對應的資料,是來自redis作者的說法(stackoverflow上面)。

    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.

    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.

    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操作)。這些複雜的操作基本上和純GET和POST操作一樣快,所以你不只是需要GET/SET而是更多的操作時,redis會起很大的作用。

    對於兩者的選擇還是要看具體的應用場景,如果需要緩存的數據只是key-value這樣簡單的結構時,我在項目裏還是採用memcache,它也足夠的穩定可靠。如果涉及到存儲,排序等一系列複雜的操作時,毫無疑問選擇redis。

   

    關於redis和memcache的不同,下面羅列了一些相關說法,供記錄:

    redis和memecache的不同在於[2]:

    1、存儲方式:
    memecache 把數據全部存在內存之中,斷電後會掛掉,數據不能超過內存大小
    redis有部份存在硬盤上,這樣能保證數據的持久性,支持數據的持久化(筆者注:有快照和AOF日誌兩種持久化方式,在實際應用的時候,要特別注意配置文件快照參數,要不就很有可能服務器頻繁滿載做dump)。
    2、數據支持類型:
    redis在數據支持上要比memecache多的多。
    3、使用底層模型不同:
    新版本的redis直接自己構建了VM 機制 ,因爲一般的系統調用系統函數的話,會浪費一定的時間去移動和請求。
    4、運行環境不同:
    redis目前官方只支持LINUX 上去行,從而省去了對於其它系統的支持,這樣的話可以更好的把精力用於本系統 環境上的優化,雖然後來微軟有一個小組爲其寫了補丁。但是沒有放到主幹上

 

個人總結一下,有持久化需求或者對數據結構和處理有高級要求的應用,選擇redis,其他簡單的key/value存儲,選擇memcache。

發佈了37 篇原創文章 · 獲贊 10 · 訪問量 32萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章