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.
Definition
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.
Data Storage
Redis has multiple data structures, which are very suitable for many needs. It supports data types such as:
- Strings
- Lists
- Sets
- Hashes
- Sorted Sets
- Bitmaps
- Hyperlogs
- Geospatial Indexes
- Streams
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.
Architecture
Both Redis and Memcached follow a client-server architecture. However, Redis is single-threaded while Memcached is multi-threaded.
Data Partitioning
Both Redis and Memcached support the distribution of data across various nodes.
Latency
Since both Redis and Memcached are in-memory databases, they provide sub-milliseconds latency.
Cache Clearing
Redis and Memcached allow you to clear cache using the FLUSHALL or FLUSHDB and FLUSH_ALL commands.
Scalability
Both Redis and Memcached allow you to scale as your data grows. However, Redis scales well horizontally while Memcached benefits well on vertical scalability
Eviction Policy
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.
Database Management
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.
Communication Protocol
Redis uses TCP connection protocol or Unix-like sockets with no support for UDP.
Memcached supports both TCP and UDP protocols.
Replication
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/
Persistence/Snapshots
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.
Server-Side Scripting
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.
Pub/Sub
Redis natively supports the Publish-Subscribe messaging model.
Memcached has no support for the Pub-Sub messaging model.
Streams
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.
https://github.com/jpzk/kafcache
Geospatial Support
Redis comes with native support for real-time geospatial data. However, Memcached does not have a data structure to support geospatial data.
Transaction Management
Although Memcached uses atomic operations, it does not support transactions.
By default, Redis supports transactions to execute commands.
Clients/Programming Languages
Redis supports almost all of the major programming languages. Supported client lists are as shown below:
- ActionScript
- ActiveX/COM+
- Bash
- Boomi
- C
- C#
- C++
- Clojure
- Common Lisp
- Crystal
- D
- Dart
- Delphi
- Elixir
- emacs lisp
- Erlang
- Fancy
- gawk
- GNU Prolog
- Go
- Haskell
- Haxe
- Io
- Java
- Julia
- Kotlin
- Lasso
- Lua
- Matlab
- mruby
- Nim
- Node.js
- Objective-C
- OCaml
- Pascal
- Perl
- PHP
- PL/SQL
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.
They include:
- .NET
- C
- C++
- ColdFusion
- Erlang
- Elixir
- Java
- Lisp
- LUA
- Ocaml
- Perl
- PHP
- Python
Cloud Support
The following cloud providers support Redis:
- Google Cloud as MemoryStore
- Amazon AWS as Redis
- Microsoft Azure as Azure Cache
- Alibaba Cloud as AsparaDB
- Oracle
- 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
Final Thoughts.
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!!!