Memcache - kdwivedi1985/system-design GitHub Wiki
What is Memcache?
- Memcache is an open-source, high performance, distributed, in-memory key-value storage. (Caching system).
- It provides sub-millisecond read/write perfomance.
- It doesn't provide high availability. If any node goes down, data on that node will be lost.
How does Memcache cluster works?
- Memcache doesn't support clustering, while we can setup distributed memcache environment, without automatic failover, it doen't have master/slave architecture or replication framework, that is the reason it's not a highly available cache.
- Client is responsible for partitioning.
- The client or application uses a hashing algorithm (often consistent hashing) to map each cache key to one of the Memcached nodes. This ensures keys are distributed evenly across the cluster.
- Adding or removing nodes changes the hash ring, which causes some keys to remap to different nodes. Consistent hashing minimizes the number of keys that need to move during scaling.
Memcache VS Redis?
Feature | Memcache | Redis |
---|---|---|
Data Types | Only strings (key-value) | Strings, lists, sets, hashes, etc. |
Persistence | No (data lost on restart) | Yes (RDB snapshots, AOF logs) |
Eviction Policy | LRU only | Multiple policies (LRU, LFU, etc.) |
Replication | No | Yes (master-slave, replication) |
Pub/Sub Support | No | Yes |
Atomic Operations | Very limited( SET, ADD, REPLACE, DELETE, APPEND, PREPEND, CAS (Check-And-Set) | Extensive atomic operations (INCR, DECR, LPUSH, LPOP, SADD, ZADD, LPUSH, LPOP, LTRIM etc.) |
Use as DB/Cache/Broker | Only a cache | Can be used as cache, DB, or broker |
Performance | Very fast | Also very fast, slightly more overhead |
- Memcached does not support atomic operations on complex data types like lists or dictionaries natively. To atomically update such structures, you typically use the CAS operation to fetch-modify-set with concurrency control.