Insights

|

10

minutes read

A Deep Dive into Comparison: Redis vs Memcached

Michael High

April 13, 2023

feature image

Introduction

If you are finding a solution for server-side caching, it is likely that you have seen some suggestions like Redis or Memcached.

Redis and Memcached are both open-source in-memory databases that use key-value format to store data. They are utilized to accelerate systems and are supported by major programming languages like Java, Python, JavaScript, and C, and are widely used by large tech companies.

This article aims to differentiate Redis and Memcached and identify which one is best suited for a specific system.

Get to know Redis

Here are some benefits of Redis:

  • Extremely fast: Redis is very fast and is able to perform around 110000 SETs and 81000 GETs per second.
  • Support various data types: Redis supports most of the commonly-used data types such as list, set, sorted set, and hashes.
  • Atomic Operations: All Redis operations are atomic, which ensures that if two clients access concurrently, the Redis server will receive the updated value.
  • Comprehensive and well-documented support: Redis is updated frequently and has an easy-to-read official documentation guide.

And here are its drawbacks:

  • Weak security: Redis only offers basic security by using usernames and passwords
  • Single-threaded only: Redis only runs in single-threaded mode on only one core of a CPU.
  • Command only: Redis supports only commands to get data, no query language or relational algebra is supported.

Get to know Memcached

Here are what Memcached offers:

  • Extremely fast: Memcached is exceptionally fast thanks to the in-memory key-value store
  • Multi-threaded support: Memcached supports Multi-threaded architecture that enables vertical scaling for your system.
  • Easy to use: Memcached is simple but quite powerful in terms of development.

However, there are also some drawbacks:

  • No persistence: Memcached does not support any way to persist data.
  • Limited key and value size: Memcached has a 1MB limit for the keys and values size that you stored.
  • Lack of data types: Memcached only supports String value.

Compare Redis and Memcached

Data structures

Redis supports most of the commonly used data types such as String, Hash, List, Set, Sorted, alongside bitmap and geospatial, and can store data up to 512MB in size.

Memcached, on the other hand, only supports String data type with only a maximum value of 1MB.

Memory Usage

When using simple String data, Memcached has a better memory utilization rate than Redis. But when it comes to more complex data, as Redis supports Hash structure, it has a better memory utilization rate.

Transaction processing

Redis has built-in commands like MULTI, EXEC, DISCARD, and WATCH to support transaction processing. In contrast, Memcached does not support transaction processing.

Performance

Reading

Both Redis and Memcached are pretty fast when reading, but Redis is a little bit faster.

Writing

Memcached is much faster than Redis after the records reach more than 100,000.

Data persistence

Redis supports data persistence through RDB snapshots and AOF logs, which help bootstrap caching data after a planned restart or an unintentional system failure.

Whereas, Memcached only stores data in memory and does not support data persistence.

Scalability

Redis is mostly a single-threaded server, which is why it will not benefit from multiple cores CPU servers. However, its native support for clustering enables it to scale horizontally.

Memcached can easily scale vertically, as it is multithreaded. It can also be scaled horizontally, but not without the help of many popular Memcached clients. Plus, many major cloud service providers also help Memcached scale horizontally easily by providing data partitioning.

Publish and Subscribe to Messages

Redis supports publishing and subscribing to messages through the pub/sub message queues.

Memcached does not support publishing and subscribing to message

Use cases for Redis and Memcached

Use Memcached when:

  • You want a simple caching server that stores simple and small key-values
  • You need support for multi-threaded

Opt for Redis when:

  • You need to store a more complex data structure rather than just a string
  • You need to use other features like pub/sub messages, geolocation, etc
  • You need to persist data for backup or warm restart

Conclusion

In conclusion, Redis is a better choice for data structures, atomic operations, and transaction processing. Meanwhile, Memcached is an excellent choice for simple string data, multithreading support, and easy-to-use development. Additionally, Redis is suitable for systems that require data persistence, while Memcached is suitable for systems that do not.

References