What is Redis and Memcached?
Both Redis and Memcached are open source cache memory engines, that offer high performance, have many similarities, although there are significant differences between those two. Memcached is supposed to be as simple as possible, whereas Redis provides a wide choice of features that make it effective and multi-functional.
Redis
Redis is a store of in-memory data structures. It is used as a database, cache. Also, it supports data structures such as strings, lists, sets, bitmaps, geospatial indexes. It has built-in replication, supports Lua scripts, provides high availability thanks to Redis Sentinel and automatic partitioning with Redis Cluster.
Redis maps value type keys. An important difference between Redis and other structured storage systems is that it supports not only strings but also abstract data types, such as:
- Sets
- Sorted sets
- Hashes
- Replication
- Geospatial data
In addition to string sets, sorted sets, hashes, applications can use these more advanced data structures to support a variety of use cases. For example, you can use Redis Sorted Sets to easily implement a game leaderboard that keeps a list of players sorted by their rank.
Sets
Sets are unordered collections of unique items. Sets can add, remove, and test for the existence of members. The benefit of a set over a list is that duplicates are not allowed by a set, which means that for data where you are just storing a unique key, if it does not exist, you can just add the key and know that it will be unique within the set.
Sorted sets
Sorted sets are an expansion of the Redis set, while they are a collection of unique strings. However, each member of the Redis sorted set is given a score to help order the set. Sorted sets also allow for accessing ranges based on the score given by Redis.
Hashes
Redis Hashes are maps between the string fields and the string values. Hence, they are the perfect data type to represent objects.
Replication
Redis comes with a feature of creating multiple replicas of a Redis primary. This allows you to scale database reads and to have highly available clusters.
Geospatial Data
Redis has purpose-built commands for working with real-time geospatial data at scale. It can perform operations such as finding the distance between two elements and finding all elements within a given distance of a point.
Memcached
Memcached is a high-performance, distributed memory object caching system, simple in nature, but intended for use in speeding up dynamic web applications by alleviating database load. Memcached is simple yet powerful. Its simplicity increases speed and solves many problems. Also, its API is compatible with the most popular languages. Unfortunately, Memcached has some serious disadvantages, for instance:
- Doesn’t support transactions, which Redis does.
- Doesn’t support advanced data structures, which Redis does.
- Lacks durability – you cannot save data on a drive.
Why store data in RAM?
Of course, the access time is several orders higher. RAM memory is a memory with free access to memory cells, which is characterized by a much faster read time than hard drives.
Redis and Memcached – things in common
- Both are open source, with plenty of documentation in order to help getting started
- Both are NoSQL data stores that keep data as key/value pairs
- Both are used to speed up applications
- Both are supported by major cloud service providers
Differences between Redis and Memcached
- Memory usage – In Redis, setting the maximum size depends on the user, he will not use more than he needs to, and memory that is no longer in use will be freed up, which in the case of Memcached is only possible when it gets restarted.
- Capacity – Redis can cache several commands and execute them simultaneously, which allows it to achieve even greater throughput for bulk import or other activities that require multiple commands.
- Performance – it often depends on loaded and comparable between Redis and Memcached, but there are workloads that Memcached cannot perform and Redis can.
- Scaling – Redis comes with tools thanks to which it is more functional than Memcached.
- Key/value pairs – Memcached can store key/value pairs, where the value is limited to 1MB. Redis – instead of that – can store up to 512MB.
- Data durability – By default, Redis persists data using a mechanism called a snapshot. There are many configuration options that allow you to tune the durability precisely.
- Many data types – Redis supports many data types, while Memcached still has string limitations.
- Disk memory dump – Redis does that by default and is very configurable. Memcached needs extra tools for that instead.
Chart results
Charts show us that Redis works much faster and is more effective, which you can see below:
Conclusion
Redis has much more to offer, including features that are not available in Memcached. This makes it much more functional. Redis’s supremacy is seeable in almost every aspect. As a result, Redis can do more than Memcached does in a faster and better way. Personally, I recommend using Redis rather than Memcached.