Redis vs. Memcached

Choosing a caching solution.

Redis vs. Memcached

Introduction

Caching data is important because it helps speed up application performance and increase efficiency. There are various types of caching like Database Caching, Session Management, Web Caching, etc. and thus it is very important for us to choose the right database for our usecases. Redis and Memcached are two contenders which are widely used in the industry for providing a caching solution. Both of them have pros and cons and should be selected as per usecases.

Memcached

  • Memcached is an open-source and distributed memory object caching system.
  • Memcached was originally developed by Brad Fitzpatrick in 2003 for the Live Journal website.
  • Memcached stores data based on key-value pairs.
  • Memcached is multithreaded and you can see performance improvements where your application can utilize multiple cores.

Architecture

Memcached has four main components:

  1. Client Software: It is used to give a list of available Memcached servers.
  2. A Client-based hashing algorithm: It chooses a server based on the key.
  3. Server Software: It is used to store values and their keys into an internal hash table.
  4. LRU: To remove the least recently used key.

Screenshot 2022-03-06 at 3.29.48 PM.png

When to use Memcached

  • If you want to only store object data for database caching.
  • If you want to run large cache nodes, and require multithreaded performance with the utilization of multiple cores.
  • If you had an extremely simple application on only a few servers, and only required simple string interpretation for your application.
  • If you want the ability to scale your cache horizontally as you grow.
  • If your application does not require data persistence.

Redis

  • Redis which stands for Remote Dictionary Server is a part of an open-source (BSD licensed) system.
  • Generally used for in-memory data structure store, used as a database, cache, and message broker.
  • Redis is, mostly, a single-threaded server from the POV of commands execution. It is not designed to benefit from multiple CPU cores.

Architecture

  1. Client: Redis client can be a Redis console client or any other programming language’s Redis API.
  2. Server: Redis server is responsible for storing data in memory. It handles all kinds of management and forms the major part of architecture. Screenshot 2022-03-06 at 3.47.05 PM.png

When to use Redis

  • If you are looking for more advanced data types, such as lists, hashes, bit arrays, HyperLogLogs, and sets.
  • If your application requires sorting and ranking datasets in memory like showing players on the leaderboard.
  • If your application requires a pub-sub broker.
  • If you want to persist your data.
  • If your application requires geospatial operations.

Similarities between Redis and Memcached

  • They serve as in-memory, key-value data stores.
  • They are from NoSQL family of data management solutions.
  • They keep all the data in RAM, which is useful in terms of a caching layer.
  • Are both open source, with plenty of documentation to help get set up.

Cons of Memcached

  • No built-in security mechanism.
  • It's only good for small key-value pairs and nothing else.
  • No persistence of data. Everything goes away when you restart it.
  • It does not support data replication.
  • If there is not enough space, the server will delete the oldest data.
  • Key length for values is limited to 250 characters (1 MB).

Cons of Redis

  • Parallelism cannot be achieved as it is single-threaded.
  • Redis data structures cannot be horizontally shared.
  • Redis requires a huge ram because it is in-memory so are not supposed to use it on ram servers.
  • Redis replication is asynchronous. Therefore, when a primary cluster fails over to a replica, a small amount of data might be lost due to replication lag.

Conclusion

Memcached brings speed onboard but Redis is enriched with its vast data structures and their operations. Redis support all operation that Memcached could perform plus additional data structure operations. Memcached has been used for ages because of its simple architecture, on the other hand, Redis is becoming a popular choice because of its rich in-memory data structure operations. One should list down all the usecases which we need in our application and then should choose between Memcached and Redis among it.