Two popular in-memory caches: a simple fast one, and a richer one with data structures.
Redis and Memcached are the two best-known in-memory caches. Both keep data in RAM and answer in a fraction of a millisecond.
Reading from a database costs milliseconds. Reading from memory costs microseconds. A cache server puts popular data in RAM on a separate machine, so many app servers can share it. Redis and Memcached are the usual choices.
Memcached was built to do one job very well: store a value under a key, return it quickly and forget it when memory is full (least recently used items go first). It does not save data to disk, and it has no built-in copies. If a node dies, its data is gone, and the cache refills from the database.
Redis looks like a key-value store, but a value can be a data structure: a list, a set, a sorted set, a hash and more. That lets the server do work for you, such as โgive me the top 10 scoresโ. It can also save data to disk, copy it to other nodes, and fail over automatically.
| Feature | Memcached | Redis |
|---|---|---|
| Data model | Keys to simple values (strings or blobs) | Strings, hashes, lists, sets, sorted sets, streams, bitmaps, HyperLogLog and more |
| Persistence | None. Memory only. | Optional: RDB snapshots and an append-only file (AOF) |
| Replication and failover | Not built in | Built in. Sentinel or Cluster handle failover and sharding. |
| Threads | Multi-threaded, uses many cores | Commands mostly run on one main thread. Newer versions add I/O threads, and you can run more instances. |
| Scaling out | The client spreads keys over nodes (consistent hashing) | Redis Cluster shards the keys for you |
| Eviction | Least recently used (LRU) | Several policies, including LRU and LFU, and no eviction |
| Extra features | Few | Pub/sub, Lua scripts, transactions, streams |
| Operating it | Very simple | More settings to understand |
A teaching model, not a benchmark. The fit scores count how many of your needs each product covers out of the maximum. The step counts are rough: about log2(N) for a sorted set, and N reads plus Nยทlog2(N) comparisons for sorting in the app. The 50 bytes per score is made up.
| Term | Meaning in easy words |
|---|---|
| In-memory store | A database or cache that keeps its data in RAM |
| Key-value | Data stored and found by a unique key |
| TTL (time to live) | How long a key stays before it is removed automatically |
| Eviction | Removing items when memory is full |
| LRU | Least recently used: remove what has not been touched for the longest |
| Sorted set | A Redis structure where each member has a score and the set stays ordered |
| Persistence | Saving data to disk so it survives a restart (RDB snapshots, AOF log) |
| Replication | Keeping copies of the data on other nodes |
| Sentinel | A Redis tool that watches nodes and promotes a replica after a failure |
| Redis Cluster | Redis mode that splits keys into slots spread over many nodes |
| Client-side sharding | The client library decides which node holds a key (used with Memcached) |
| Choice | We gain | We lose |
|---|---|---|
| Memcached | Simplicity and raw multi-core speed for plain values | Persistence, replication and data structures |
| Redis | Rich features and built-in safety | More complexity to operate and understand |
| Redis persistence on | Data can survive a restart | Some write cost, and disk settings to tune |
| Redis replicas | Failover and extra read capacity | A replica may be slightly behind the primary |
| Client-side sharding | No extra moving parts | The client must handle adding and removing nodes |
| Managed service | Less work, built-in failover | Cost and less control |