Redis vs Memcached 比較

兩者都是 in-memory database,都可以提供使用者低延遲、高效率來存取資料,並且都非常適合拿來當快取資料使用。目前來說,主流是使用 Redis,雖然 memcached 支援 multi-threaded 並且在內存管理(Slab allocation)上很有效率,但比 Redis 比缺少了許多必要的功能,例如 backup、replication、encryption、multi-database、Pub/Sub function、data-type也只有一種 string 類型,所以使用 Redis還是比較推薦的。

Jerry’s Notes
What’s next?
5 min readMar 18, 2022

--

!!!總結: 建議在大部份的使用場景上,優先考慮使用 Redis,除非您需求是短暫快速取資料,並且資料本身可以去後端取回(充許重新快取),此時您可以考慮試試 memcached。

Choosing between Redis and Memcached?

Redis and Memcached are popular, open-source, in-memory data stores. Although they are both easy to use and offer high performance, there are important differences to consider when choosing an engine. Memcached is designed for simplicity while Redis offers a rich set of features that make it effective for a wide range of use cases. Understand your requirements and what each engine offers to decide which solution better meets your needs.

Redis Engine Specs

Redis Engine :
Single-Thread
Data-Type — Complex
https://redis.io/topics/data-types

Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes with radius queries and streams. Redis has built-in replication, Lua scripting, LRU eviction, transactions and different levels of on-disk persistence, and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster.

Memcached Engine Specs

Memcached Engine :
Multi-Thread
Data-Type — Simple String

Memcached (pronounced variously mem-cash-dee or mem-cashed) is a general-purpose distributed memory-caching system. It is often used to speed up dynamic database-driven websites by caching data and objects in RAM to reduce the number of times an external data source (such as a database or API) must be read.

Q. What’s different between Redis and DynamoDB?

Redis is key/value base database which data stored in memory. Redis是一種內存數據庫,所有數據在內存中,支持高並發訪問,使用單進程單線程+IO多路復用機制保證了線程安全。

DynamoDB is key/value base database (NoSQL), and also support document data set like JSON. However, It’s data stored in disk. So, it’s performance slow than Redis. DynamoDB是一項Web服務,數據的計算,存儲均在AWS雲中,用戶只所做的其實只是提交請求和接收響應,而不需要過多的考慮快速的數據量增長而導致的內存不足等問題,並支援分區鍵和排序鍵以及二級索引可以應對很多不同場景下的需求。

Q: Why Redis is single threaded?

官方FAQ表示,因為Redis是基於內存的操作,CPU不容易造成Redis的瓶頸,Redis的瓶頸最有可能是機器內存的大小或者網絡帶寬。既然單線程容易實現,而且CPU不容易成為瓶頸,自然設計為單執行序。

Redis採用了單線程的模型,保證了每個操作的原子性,也減少了線程的上下文切換和競爭。

採用單線程,避免了不必要的上下文切換和競爭條件,也不存在多進程或者多線程導致的切換而消耗 CPU。

Redis is single threaded. How can I exploit multiple CPU / cores?

It’s not very frequent that CPU becomes your bottleneck with Redis, as usually Redis is either memory or network bound. For instance, using pipelining Redis running on an average Linux system can deliver even 1 million requests per second, so if your application mainly uses O(N) or O(log(N)) commands, it is hardly going to use too much CPU.

However, to maximize CPU usage you can start multiple instances of Redis in the same box and treat them as different servers. At some point a single box may not be enough anyway, so if you want to use multiple CPUs you can start thinking of some way to shard earlier.

You can find more information about using multiple Redis instances in the Partitioning page.

However, with Redis 4.0 we started to make Redis more threaded. For now, this is limited to deleting objects in the background, and to blocking commands implemented via Redis modules. For future releases, the plan is to make Redis more and more threaded.

Redis 是單執行序的服務 (single threaded engine) 的說明。

As you may understand, Redis is a single threaded engine, thus, it can only work on one query at a time. This means that if any request is already being processed by the Redis engine, any other queries or connection requests incoming during this time may be held up which might contribute to a connection timeout or latency in execution of the requests.

由於 Redis 本身是單執行序的服務,所以當您前端執行命令時,後端 Redis 是一個、一個、一個命令,循序來執行,若您當下又執行高時間複雜度的命令,就有比較大的機會,造成短暫指令操作阻塞的状况發生。

Q: Is Redis really single-thread?

# ps -eLf | grep -i redis-server
UID PID PPID LWP C NLWP STIME TTY TIME CMD
root 21613 1 21613 0 4 Jun14 ? 00:03:46 redis-server 0.0.0.0:7006
root 21613 1 21614 0 4 Jun14 ? 00:00:00 redis-server 0.0.0.0:7006
root 21613 1 21615 0 4 Jun14 ? 00:00:00 redis-server 0.0.0.0:7006
root 21613 1 21616 0 4 Jun14 ? 00:00:00 redis-server 0.0.0.0:7006

PID VS PPID VS LWP 說明?

1、PID(process ID):PID是程序被操作系統加載到內存成為進程後動態分配的資源。每次程序執行的時候,操作系統都會重新加載,PID在每次加載的時候都是不同的。

2、PPID(parent process ID): PPID是程序的父進程號。

3、LWP: 線程ID。在用戶態的命令(比如ps)中常用的顯示方式。

!!! 同一個進程Process,但仍會起不多個線程 Thread 來提供服務。

所以嚴格來說,redis 是Single Process and multi Thread.. 管理的是同一塊記憶體空間。

Redis 的單線程,跟 enhances I/O (IO多線程) 的差別?

Redis單線程是指獲取 (socket 讀)、解析、執行、內容返回 (socket 寫) 等都由一個順序串行的主線程處理,這個主線程就是我們平時說的”單線程”,而其他的清理髒數據、無用連接的釋放、LRU淘汰策略等等也是有其他線程在處理的,因此其實在Redis6之前的Redis,本質上也是多線程 “ Single Process and multi Thread” 的,但一般使用者無法釐清這個差異,所以基本跟客戶還是說明 “Redis 的單線程服務”。

!!! 更精準一點來說,是同一個進程 Process,但仍會起多個線程 Thread來運行”Single Process and multi Thread”。

Redis 的IO多線程只是用來處理網絡數據的讀寫和協議解析,執行命令仍然是單線程,所以主要是為了提升執行命令前後的網絡I/O性能。

■ Enhances I/O (IO多線程) 自 ElastiCache redis 5.0.3 就有支援了,雖然IO變多線程,但只提升處理I/O的速度(提升網路的吞吐量及延遲) ,但Redis 處理客戶端的請求時仍是使用單線程。

而 Redis 6 — Threaded I/O 的功能,也是跟 ElastiCache redis 5.0.3 enhances I/O (IO多線程) 的目的是一致的,也是使用額外的線程,來提升處理I/O的速度,但Redis 處理客戶端的請求時仍是使用單線程是不變的。

Redis 6 — Threaded I/O 開啟IO多線程

$ vim redis.conf
#開啟IO多線程
io-threads-do-reads yes

#配置線程數量,如果設為1就是主線程模式。
io-threads 4
!!! redis conf註釋裡為什麼推薦io thread為4個?
因為過多個線程,不一定會有更好的效能,所以這也跟實例有多少vCPU有關。
也如同使用 Pipelining 一樣,同時並發過高的數量,也不一定有更佳的效能,這部份您可以參考 "ElastiCache client connection handling" 有進一步的測試。

!!! 請注意, Redis 6 — Threaded I/O ElastiCache redis 5.0.3 enhances I/O (IO多線程) 達到的目的應該是一致的,但實作的方式是不同的。

Comparing Redis and Memcached

Q: Why is Redis preferred over Memcached or Why is Memcached preferred over Redis?

Choosing between Redis and Memcached :
Redis and Memcached are popular, open-source, in-memory data stores. Although they are both easy to use and offer high performance, there are important differences to consider when choosing an engine. Memcached is designed for simplicity while Redis offers a rich set of features that make it effective for a wide range of use cases. Understand your requirements and what each engine offers to decide which solution better meets your needs.
Learn about Amazon ElastiCache for Redis

Amazon ElastiCache supports the Memcached and Redis cache engines. Each engine provides some advantages. Use the information in this topic to help you choose the engine and version that best meets your requirements.

Q: Why is Redis preferred over Memcached?

Support to authenticate users with role-based access control. (version 6.x)
Support encryption include In-transit and At-rest.
Support Authenticating Users with the Redis AUTH Command
Support dynamically adding or removing shards from your Redis (cluster mode enabled) cluster.
You need complex data types, such as strings, hashes, lists, sets, sorted sets, and bitmaps.
You need persistence of your key store. (Support snapshot backup)
You need automatic failover if your primary node fails.

Q: Why is Memcached preferred over Redis?

■ You need the simplest model possible.
■ You need to run large nodes with multiple cores or threads.
■ You need the ability to scale out and in, adding and removing nodes as demand on your system increases and decreases.
■ You need to cache objects.

Q: 在 ElastiCache Redis 中,為什麼實例 m4.large 的機型有 8 GiB 的內存, cache.m4.large 卻只有 6.42 GiB,而在 redis-cli 裡面看到的 maxmemory 最後只剩 4.8 GiB???

A: 因為 ElastiCache Redis 會保留一定比例的內存,給作業系統使用,所以 8 GiB 內存的機型,在 ElastiCache Redis 的實例機型卻只剩 6.42 GiB。另外 ElastiCache Redis 還是參考參數 Parameter: reserved-memory/reserved-memory-percent 來保留一定比例的內存,給 Redis 服務使用,主要保留給 Replication、及 Snapshot backup 時來使用。

--

--

Jerry’s Notes
What’s next?

An cloud support engineer focus on troubleshooting with customer reported issue ,and cloud solution architecture.