Memcached vs. Redis
When working with dynamic and demanding applications that use database connections, opting for an in-memory database like Memcached or Redis significantly boosts performance. Both solutions are open-source and work in a similar way. However, when it comes to features and use cases, there are some differences that you should be aware of.
Put simply, Memcached is designed to be easy to use, whereas Redis offers a more comprehensive 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 differences and similarities.
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 developers of Memcached wanted to find a way of speeding up access to the kind of databases that are required for advanced web applications – 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, circumventing the need for hard disk access.
Redis is short for Remote Dictionary 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 marketplaces.
Memcached vs Redis: similarities
The Memcached and Redis in-memory data stores read data at incredibly fast speeds, reducing response times to just a few milliseconds or less. In terms of syntax, both systems are easy to use and can be integrated into applications without any need for complex code. Moreover, both Memcached and Redis allow data to be distributed across several nodes, so it is easy to scale systems vertically as the amount of data grows. Finally, they are open-source solutions and support numerous protocols and programming languages such as Java, Python, PHP, C, C++, C#, JavaScript, Node.js, Ruby, and Go.
Memcached vs. Redis: differences
Although Memcached and Redis have a lot in common, each has some unique features too. We will focus on the most important ones here.
Architecture: Memcached is multi-threaded, which means it can use multiple processing cores simultaneously. 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 structures like lists, hashes, streams, sets, sorted sets, bitmaps, and hyperloglogs. What’s more, Redis can also measure and store geospatial data.
Data persistence: 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 automatically 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 management: Redis uses both client-side and server-side caching to distribute 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.
Transactions: Both in-memory systems use what are known as “atomic commands”. This means that when values are entered, they are instantaneously displayed on the client side. In Redis, commands can also be grouped together in the form of isolated atomic operations. This feature, called pipelining, 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 architecture. This messaging pattern facilitates efficient communication between applications. It is used for high-performance chat rooms, live streams, social media feeds, and communication between different servers.
Lua scripting: If developers want to use Redis to perform complex calculations, they can use Lua scripts. Lua is a lightweight programming language into which new logic can easily be added. This boosts performance and simplifies applications.
Memcached vs. Redis: summary table
Memcached | Redis |
---|---|
Easy to install and use | Easy to install and use |
Latency in the order of milliseconds | Latency in the order of milliseconds |
Open-source and compatible with popular programming languages | Open-source and compatible with popular programming languages |
Multi-thread engine, ideal for large quantities of data | Single-thread engine, ideal for smaller quantities of data |
Vertical scalability | Vertical and horizontal scalability (see “Cluster management”) |
Key length limited to 250 characters (1 MB) | Supports many data types (up to 512 MB) |
No data persistence 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 operations simultaneously |
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 management, memory management and data persistence. 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 functionality, 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 alternative.