Caching: What to choose Redis or Memcached

leangaurav
2 min readMar 1, 2023

This is a short post just to quickly compare the two on high level and direct you to other good resources.

What is Memcached?
https://memcached.org/

Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering.

What is Redis?
https://redis.io/docs/about/

Redis is an open source (BSD licensed), in-memory data structure store used as a database, cache, message broker, and streaming engine. Redis provides data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes, 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.

Taken from AWS

Since posts become outdated over time jump to AWS and read it directly. Since it can be assumed that cloud vendors have to keep their documentation up to date.

Summary

  • Memcached is simple Key-Value store for strings and objects
  • Redis is memached + many things
  • Both support common caching use cases like keys with TTL etc.
  • Max number of keys:
    Redis: 2³²Refer this for updated info
    Memcached: Need reference.
  • Memcached max key size: 250 bytes, max value size: 1MB default + configurable
  • Redis max key size: 512 MB

Other Points to Note

Single vs MultiThreaded

Redis internally runs on a single thread (makes implementation easy).
Memcached can run on multiple cores.

Hence Memcached can have better performance. Look at benchmarks or run your own for your workload.

Simple vs Complex 🎯

If you need a simple Key Value store: go for Memcached.
Need complex data structures: go for Redis.
You want to store your key value data somewhere. Use whatever your organization already uses. Doesn’t matter.

Coming to practical usage (my perspective ❗)

I see both Redis and Memcahed being used at places without issues. Mainly I have seen it used as a key value store or some sort of caching application. Also Redis pub-sub for live streams.
However Redis seems to have data loss issues in case of network partitions. Read these:

  1. https://aerospike.com/when-to-use-aerospike-vs-redis/
  2. https://redis.io/docs/management/sentinel/
  3. https://aphyr.com/posts/283-jepsen-redis Old but still relevant I think

So I won’t think of using Redis as a Datastore.

--

--

leangaurav

Engineer | Trainer | writes about Practical Software Engineering | Find me on linkedin.com/in/leangaurav | Discuss anything topmate.io/leangaurav