When working with dynamic and demanding ap­pli­ca­tions that use database con­nec­tions, opting for an in-memory database like Memcached or Redis sig­nif­i­cant­ly boosts per­for­mance. Both solutions are open-source and work in a similar way. However, when it comes to features and use cases, there are some dif­fer­ences that you should be aware of.

Put simply, Memcached is designed to be easy to use, whereas Redis offers a more com­pre­hen­sive range of features (whether they are helpful will depend on the use case). Let’s take a closer look at these two in-memory data stores and their dif­fer­ences and sim­i­lar­i­ties.

A brief overview of Memcached and Redis

Firstly, Memcached has been around for almost 20 years, whereas Redis arrived on the market about 7 years later. The de­vel­op­ers of Memcached wanted to find a way of speeding up access to the kind of databases that are required for advanced web ap­pli­ca­tions – and they did just that. Their in-memory cache service (Memcached for short) uses key/value pairs to make cached data available at lightning speed, cir­cum­vent­ing the need for hard disk access.

Redis is short for Remote Dic­tio­nary Server. It too is a key-value in-memory database and, like Memcached, it is used for a wide range of projects, including websites, gaming, and online mar­ket­places.

Memcached vs Redis: sim­i­lar­i­ties

The Memcached and Redis in-memory data stores read data at in­cred­i­bly fast speeds, reducing response times to just a few mil­lisec­onds or less. In terms of syntax, both systems are easy to use and can be in­te­grat­ed into ap­pli­ca­tions without any need for complex code. Moreover, both Memcached and Redis allow data to be dis­trib­uted across several nodes, so it is easy to scale systems ver­ti­cal­ly as the amount of data grows. Finally, they are open-source solutions and support numerous protocols and pro­gram­ming languages such as Java, Python, PHP, C, C++, C#, JavaScript, Node.js, Ruby, and Go.

Memcached vs. Redis: dif­fer­ences

Although Memcached and Redis have a lot in common, each has some unique features too. We will focus on the most important ones here.

Ar­chi­tec­ture: Memcached is multi-threaded, which means it can use multiple pro­cess­ing cores si­mul­ta­ne­ous­ly. Redis on the other hand uses a single core. This makes it more efficient at handling small amounts of data. For very large amounts of data, however, Memcached is the better option.

Data types: Memcached can only handle simple strings of up to 1 MB, whereas Redis supports values up to 512 MB in size and more complex data struc­tures like lists, hashes, streams, sets, sorted sets, bitmaps, and hy­per­loglogs. What’s more, Redis can also measure and store geospa­tial data.

Data per­sis­tence: Unlike Memcached, Redis is not simply an in-memory system. With Memcached, if there is no more space on a server for new items, the least recently used data will au­to­mat­i­cal­ly be deleted. Data is also lost if a Memcached instance fails. With Redis on the other hand, data can be restored using the “point-in-time” snapshot function or Append Only File (AOF) mode. Note however, that AOF mode may slow things down slightly.

Cluster man­age­ment: Redis uses both client-side and server-side caching to dis­trib­ute data, whereas Memcached only uses client-side caching. The nodes in a Redis cluster can exchange data with one another. There is no notable impact on response times if a node subset fails. In Memcached this kind of data exchange is not possible at all.

Trans­ac­tions: Both in-memory systems use what are known as “atomic commands”. This means that when values are entered, they are in­stan­ta­neous­ly displayed on the client side. In Redis, commands can also be grouped together in the form of isolated atomic op­er­a­tions. This feature, called pipelin­ing, means that Redis can respond to multiple commands at the same time. This is not the case in Memcached.

Pub/Sub: Redis supports the Pub/Sub ar­chi­tec­ture. This messaging pattern fa­cil­i­tates efficient com­mu­ni­ca­tion between ap­pli­ca­tions. It is used for high-per­for­mance chat rooms, live streams, social media feeds, and com­mu­ni­ca­tion between different servers.

Lua scripting: If de­vel­op­ers want to use Redis to perform complex cal­cu­la­tions, they can use Lua scripts. Lua is a light­weight pro­gram­ming language into which new logic can easily be added. This boosts per­for­mance and sim­pli­fies ap­pli­ca­tions.

Memcached vs. Redis: summary table

Memcached Redis
Easy to install and use Easy to install and use
Latency in the order of mil­lisec­onds Latency in the order of mil­lisec­onds
Open-source and com­pat­i­ble with popular pro­gram­ming languages Open-source and com­pat­i­ble with popular pro­gram­ming languages
Multi-thread engine, ideal for large quan­ti­ties of data Single-thread engine, ideal for smaller quan­ti­ties of data
Vertical scal­a­bil­i­ty Vertical and hor­i­zon­tal scal­a­bil­i­ty (see “Cluster man­age­ment”)
Key length limited to 250 char­ac­ters (1 MB) Supports many data types (up to 512 MB)
No data per­sis­tence or failover mechanism Data can be restored thanks to snapshots, AOF and backups to hard disk
Ideal for caching and session storage Ideal for complex processes and for running multiple op­er­a­tions si­mul­ta­ne­ous­ly

Summary

So, which is best, Memcached or Redis? Both solutions are mature in-memory data stores. They differ mainly in terms of supported data formats, cluster man­age­ment, memory man­age­ment and data per­sis­tence. As for which engine is better, it depends on how you intend to use it. If you are already using Memcached and don’t need any extra func­tion­al­i­ty, then it may not be worth switching to Redis. However, if you do want more features and better backup options, then Redis is a good al­ter­na­tive.

Go to Main Menu