1. 程式人生 > >memcached和redis支援上的區別

memcached和redis支援上的區別

    我這段時間在用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。