Home > Help Center > Shared Hosting > Website > Memcached or Redis?

Memcached or Redis?

It's a question that nearly always arises in any discussion about squeezing more performance out of a modern, database-driven Web application. When performance needs to be improved, caching is often the first step taken, and Memcached or Redis are typically the first places to turn.

These renowned cache engines share a number of similarities, but they also have important differences.

Redis, the newer and more versatile of the two, is almost always the superior choice.

Similarities

Both Memcached and Redis serve as in-memory, key-value data stores, although Redis is more accurately described as a data structure store.

Both Memcached and Redis belong to the NoSQL family of data management solutions, and both are based on a key-value data model.

They both keep all data in RAM, which of course makes them supremely useful as a caching layer. In terms of performance, the two data stores are also remarkably similar, exhibiting almost identical characteristics (and metrics) with respect to throughput and latency.

Memcached was originally developed by Brad Fitzpatrick in 2003 for the LiveJournal website. Since then, Memcached has been rewritten in C (the original implementation was in Perl) and put in the public domain, where it has become a cornerstone of modern Web applications. Current development of Memcached is focused on stability and optimizations rather than adding new features.

Redis was created by Salvatore Sanfilippo in 2009, and Sanfilippo remains the lead developer of the project today. Redis is sometimes described as "Memcached on steroids," which is hardly surprising considering that parts of Redis were built in response to lessons learned from using Memcached. Redis has more features than Memcached and is, thus, more powerful and flexible.

Used by many companies and in countless mission-critical production environments, both Memcached and Redis are supported by client libraries in every conceivable programming language, and it's included in a multitude of packages for developers.

Why are Memcached and Redis so popular?

Not only are they extremely effective, they're also relatively simple. Getting started with either Memcached or Redis is considered easy work for a developer. It takes only a few minutes to set up and get them working with an application. Thus, a small investment of time and effort can have an immediate, dramatic impact on performance - usually by orders of magnitude.

When to use Memcached?

Because Redis is newer and has more features than Memcached, Redis is almost always the better choice.

However, Memcached could be preferable when caching relatively small and static data, such as HTML code fragments.

Memcached's internal memory management, while not as sophisticated as that of Redis, is more efficient in the simplest use cases because it consumes comparatively less memory resources for metadata. Strings (the only data type supported by Memcached) are ideal for storing data that's only read, because strings require no further processing.

That said, Memcached's memory management efficiency diminishes quickly when data size is dynamic, at which point Memcached's memory can become fragmented. Also, large data sets often involve serialized data, which always requires more space to store.

While Memcached is effectively limited to storing data in its serialized form, the data structures in Redis can store any aspect of the data natively, thus reducing serialization overhead.

The second scenario in which Memcached has an advantage over Redis is in scaling. Because Memcached is multithreaded, you can easily scale up by giving it more computational resources, but you will lose part or all of the cached data (depending on whether you use consistent hashing).

Redis, which is mostly single-threaded, can scale horizontally via clustering without loss of data. Clustering is an effective scaling solution, but it is comparatively more complex to set up and operate.

When to use Redis?

You'll almost always want to use Redis because of its data structures.

With Redis as a cache, you gain a lot of power (such as the ability to fine-tune cache contents and durability) and greater efficiency overall. Once you use the data structures, the efficiency boost becomes tremendous for specific application scenarios.

Redis' superiority is evident in almost every aspect of cache management. Caches employ a mechanism called data eviction to make room for new data by deleting old data from memory.

Memcached's data eviction mechanism employs a Least Recently Used algorithm and somewhat arbitrarily evicts data that's similar in size to the new data.

Redis, by contrast, allows for fine-grained control over eviction, letting you choose from six different eviction policies. Redis also employs more sophisticated approaches to memory management and eviction candidate selection.

Redis supports both lazy and active eviction, where data is evicted only when more space is needed or proactively. Memcached, on the other hand, provides lazy eviction only.

Redis gives you much greater flexibility regarding the objects you can cache. While Memcached limits key names to 250 bytes and works with plain strings only, Redis allows key names and values to be as large as 512MB each, and they are binary safe.

Plus, Redis has five primary data structures to choose from, opening up a world of possibilities to the application developer through intelligent caching and manipulation of cached data.

Differences

The first one is for caching small and static data, such as HTML code fragments. Memcached uses less sophisticated internal memory management compared to Redis and consumes comparatively less memory resources for metadata which makes it more efficient.

Using Redis as a cache, you'll gain the ability to fine-tune cache contents and durability. Redis is better in almost every aspect of cache management.

Redis also employs more sophisticated approaches to memory management and eviction candidate selection. Caches employ a mechanism called data eviction to delete old data from memory in order to make room for new data.

Memcached's data eviction mechanism uses an LRU (Least Recently Used) algorithm and somewhat arbitrarily evicts data that's similar in size to the new data.

Redis, by contrast, allows for fine-grained control over eviction though a choice of six different eviction policies. Redis also employs more sophisticated approaches to memory management and eviction candidate selection.

So, it looks like a natural choice to use Redis because of its broader functional and convenient features.

Conclusion

With a great deal of certainty , we can summarize the results as follows:

If a system works well with Memcached, do not change it - it's very likely to invest a lot of time to implement Redis without achieving a satisfactory results.

If your application does not need most of the Redis functionality, use Memcached for its proven simplicity, reliability and speed.

For everything else, the mass recommendation is in favor of Redis.

Reference:
Itamar Haber @infoworld.com
Redis vs Memcached @bogotobogo.com

#server #cache #overview

Still not finding what you're looking for?

Contact our support team with any additional questions or concerns.

Contact support