Despite these similarities, it can be quite a challenge choosing which to use for your applications.
In this article, we will break down the essential aspects of an in-memory database and compare them.
Let us start at the basics and discuss what is Redis and what is Memcached?
What is Redis?
Remote Dictionary Server or Redis for short is defined as a free, open-source in-memory database used as a cache or message broker. It was developed in 2009 by Salvatore Sanfilippo and now powers popular systems such s Twitter, StackOverflow, GitHub, etc.
What is Memcached?
Memcached is defined as a free, open-source, and high-performance in-memory database. It is used to provide a caching mechanism for applications but acts generically.
It was developed in 2004 by Brad Fitzpatrick and is now used by popular applications such as Twitter, Facebook, Youtube, Instagram, Udemy, Slack, etc.
Language and Platform Support
Redis is written in ANSI C and works in all POSIX systems. Redis is supported in Linux, BSD, and OSX systems. Windows support is not yet available as of writing this tutorial.
Memcached is written in ANSI C but cross-platform. Although there is no official release of Memcached for Windows, you can find Cygwin implementations of the database or compile it for your platform.
Redis has multiple data structures, which are very suitable for many needs. It supports data types such as:
- Sorted Sets
- Geospatial Indexes
Redis also allows you to perform automatic operations such as appending to strings, calculating set union, difference, intersection, etc., appending an element to a list, etc.
Memcached, on the other hand, supports plain binary strings. This makes it easy to use and uses less memory overhead than Redis.
Both Redis and Memcached follow a client-server architecture. However, Redis is single-threaded while Memcached is multi-threaded.
Both Redis and Memcached support the distribution of data across various nodes.
Since both Redis and Memcached are in-memory databases, they provide sub-milliseconds latency.
Redis and Memcached allow you to clear cache using the FLUSHALL or FLUSHDB and FLUSH_ALL commands.
Both Redis and Memcached allow you to scale as your data grows. However, Redis scales well horizontally while Memcached benefits well on vertical scalability
Redis supports a collection of eviction policies that can be customized according to your needs.
Check our tutorial on Redis eviction policies to learn more.
On the other hand, Memcached is limited to the LRU eviction policy.
Ease of Use/Documentation
Redis is very well documentation and has a large community behind it. This makes it very easy to learn and use.
Although Memcached is general and relatively documented, you may need to dig into the source code to implement custom features.
Redis provides you with a built-in CLI utility to access and manage your databases and Redis server.
Memcached uses telnet to connect and manage your server.
Redis uses TCP connection protocol or Unix-like sockets with no support for UDP.
Memcached supports both TCP and UDP protocols.
Redis offers a simple master-slave replication implementation. It is effortless to use and configure. The replication will create exact copies of the master instance regardless of what happens to the master.
Memcached does not natively support replication. However, you can implement data replication using patch tools such as http://repcached.lab.klab.org/
Redis natively supports snapshotting by saving a snapshot of your datasets to a disk in binary file. However, you can customize the snapshotting features in Redis.conf file.
For persistence, Redis supports:
- RDB persistence
- AOF persistence.
Check our tutorial on Redis persistence to explore further.
Memcached does not natively support disk dump. However, you can use tools such as Memcached-dd to implement.
There is no data persistence for Memcached.
Redis supports server-side scripting using an embedded LUA interpreter. It uses EVAL and EVALSHA functions to evaluate LUA scripts.
Keep in mind that LUA scripts in Redis are synchronous. Hence, other operations are blocked when the scripts are executing.
Memcached does not support server-side scripting.
Redis natively supports the Publish-Subscribe messaging model.
Memcached has no support for the Pub-Sub messaging model.
Redis supports streams with the addition of Redis stream types in Redis version 5.0
Memcached does not natively support streams. However, you can use tools such as Kafcache to implement streams.
Redis comes with native support for real-time geospatial data. However, Memcached does not have a data structure to support geospatial data.
Although Memcached uses atomic operations, it does not support transactions.
By default, Redis supports transactions to execute commands.
Redis supports almost all of the major programming languages. Supported client lists are as shown below:
- Common Lisp
- emacs lisp
- GNU Prolog
Memcached is not left behind and provides clients with major programming languages. However, it does come short in the list of clients compared to Redis.
The following cloud providers support Redis:
- Google Cloud as MemoryStore
- Amazon AWS as Redis
- Microsoft Azure as Azure Cache
- Alibaba Cloud as AsparaDB
- IBM Cloud
The following providers also support Memcached:
- Google Cloud as MemoryStore
- Amazon AWS as Amazon ElastiCache
- Microsoft Azure
- Alibaba Cloud as AsparaDB
- IBM Cloud as Memcached by Bitnami
This tutorial provides a comprehensive breakdown of the similarities and differences of Redis and Memcached databases. Keep in mind that this tutorial serves as an information reference. Use it to make a choice based on your requirements.
Happy coding & See you in the next one!!!