blog
Redis vs. Memcached From An Administration and Management Perspective
If you Google around, it is trivial to find the comparison between Redis versus Memcached as shown here, here, here and here. In this blog post, we will do a similar comparison between Redis and Memcached but from the administration and management point of view instead. For those who are not familiar with these technologies, this article shall give you some insights on common operational aspects of managing a cache tier in the production environment.
CLI Tools
CLI, or Command Line Interface, is a text-based interface used to interact with software and operating systems by typing commands into the interface and receiving a response in the same way. From the operational perspective, most of the database operators (SysAdmin, DBA or DevOps engineer) prefer to use the CLI-based client tools for faster interaction with the server, rich functionality, automation via scripting, and granular control of the service.
Redis comes with its CLI called redis-cli. It is a very simple yet powerful tool to interact with the Redis Server and comes with an auto-complete feature. If you are using a local instance, the host is localhost, the default port is 6379, and there is no password by default:
$ redis-cli
The redis-cli is not like other CLI. Apart from the standard key/value datastore management, it can do a bunch of interesting things, for example:
-
Execute the same command a specified number of times with a user-selected pause between the executions using the -r
and -i arguments. -
Create backups of RDB files remotely.
-
Simulate a slave and print the replication stream it receives from the master.
-
Check the latency of a Redis server and show statistics or even an ASCII-art spectrogram of latency samples and frequencies.
Apart from the redis-cli, Redis also comes with a benchmark tool called redis-benchmark. This tool can be used to simulate running commands done by clients at the same time sending multiple queries. It is similar to Apache’s ab utility.
Memcached does not have an official CLI. Direct communication via telnet is commonly the best way to interact with the server via CLI. Consider the Memcached is listening on port 11211 on host 192.168.10.2, one would connect using the following command:
$ telnet 192.168.10.2 11211
For scripted processing, you may want to use ‘netcat’ or ‘nc’ command. For example:
{script to generate commands} | nc {memcached host} {memcached port} | {script to process output}
For example:
(echo stats; echo quit) | nc 192.168.10.2 11211 | grep get_hits
STAT get_hits 0
However, under-development Perl scripts –memcached-tool and damemtop – come with the source code under the scripts directory. If you installed it from the OS repository, it is usually located under the /usr/share/memcached/scripts directory. No guarantee comes with the usage of these scripts, hence do be cautious when using them.
For benchmarking, memaslap is a load-generation and benchmark tool for Memcached servers. There is a project called mc-benchmark, a straightforward port of redis-benchmark to Memcached protocol if you would like to compare Redis and Memcached for apple-to-apple comparison.
As you can see, both are using a different approach for their command-line clients. Redis is superior in this aspect since it offers a set of very convenient command-line options which makes administering the server easy.
GUI Tools
GUI, or Graphical User Interface, is a visual-based interface that features the use of graphic images, including windows, icons, and menus. A mouse is the most common way to navigate through a GUI, although the keyboard is used sometimes. From the operational perspective, most of the database operators (SysAdmin, DBA or DevOps engineer) prefer to use the GUI-based client tools for visualization-related purposes like database administration, monitoring and stats, reporting and analytics, and a more suitable remote access client for the end-users.
There are a couple of GUI tools available in the market to manage both Redis and Memcached, like FastoNoSQL, a popular open-source IDE tool for NoSQL, supported on most famous platforms like Windows, Linux, MacOSX, Android and FreeBSD.
For Redis, RedisInsight is probably the best and most popular Redis GUI tool which supports many Redis-specific features like Streams, RedisGraph, RedisTimeSeries, RedisJSON, RediSearch and RedisGears. It also comes with an intuitive web-based CLI with syntax highlighting and auto-complete and employs integrated help to deliver intuitive assistance. There are also many other open-source projects for Redis administration tools like phpRedisAdmin, Redis Commander and PHPRedMin.
For Memcached, there are several GUI open-source projects specifically for Memcached like PHPMemcachedAdmin and Memchaced Dashboard (yes, mind the spelling). Although not as many options as Redis, these tools can do most of the work considering Memcached’s simple setup and functionality.
All of these mentioned tools are also available in Docker, which means they can be easily containerized and managed by the container orchestration tools like Kubernetes and Docker Swarm.
Backup and Restore
Although Redis or Memcached primary usage is for in-memory caching ephemeral data, taking a backup and performing a restore are common administration tasks for getting a snapshot of the data for recovery, migration and redundancy purposes. It regularly happens when the caching layer has become prominently critical to your application, where a downtime on the cache tier might affect the majority of the user experience. With a backup, you can recover a failed cache instance easily with a warmed-up cache. It is a known best practice for stopping/starting/restarting the cache server without sacrificing any cached data.
Redis datastore can be backed up by copying the RDB file and Append-only File (AOF) file for full durability. Note that by default, AOF is disabled in Redis, so extra steps are required to enable this feature. For logical backups, like exporting the active dataset into a text file, some external tools like redis-dump and or node-redis-dump can be of assistance. If you’d like to learn more about this, we have covered the Redis backup method in great detail in another blog post titled An Overview of Backup tools for Redis.
For Memcached, there are several ways to achieve this using external tools. You may use the memdump and memcat utilities from the libmemcached suite. Note that using this tool has no guarantee of consistency because in the general case, there is no way to list all the keys that a Memcached instance is storing, especially if the dataset is big (millions of keys). So an iteration is required as shown on the tools’ documentation page. There is also a project called memcached-util, a more straightforward client to perform backup and restoration against a Memcached instance. To perform a backup, you may use the memcached-util command like this:
$ memcached-util --addr "192.168.10.2:11211" --op "backup" --filename "memcache_backup.json"
To restore, simply change the –op option to restore:
$ memcached-util --addr "192.168.10.2:11211" --op "restore" --filename "memcache_backup.json"
As you can see, Redis has official support for backup and restoration through its RDB and AOF files due to its support for persistent storage. Memcached on the other hand, requires additional tools to be installed and some tools have no guarantee of consistency. If this is tolerable in your environment, backing up and restoring Memcached should not be an operational overhead to your organization.
Service Control
Both services are pretty straightforward to operate. After the installation, you can simply run the server command and it will be started as a foreground process, as shown below:
$ redis-server # For Redis
$ memcached -u memcache # memcached has to run as memcache user
Since Redis is single-threaded, it is common to run multiple Redis instances to make use of all available CPU cores on a single host. The simplest thing to run multiple Redis instances to execute the redis-server utility multiple times with a different configuration file each, for example:
$ redis-server /etc/redis/redis1.conf
$ redis-server /etc/redis/redis2.conf
$ redis-server /etc/redis/redis3.conf
Managing multiple instances per host can be a complex and risky task. You won’t want to stop or restart the wrong instances hence an extremely careful practice is necessary to control the Redis services. You can use scripting, systemd or init script to simplify this but it is going to be an additional overhead on the administration tasks for the SysAdmins.
Memcached is multi-threaded by default and has no problem saturating many cores, so commonly running a single memcached per host will make use of all the cores.
Upgrade
The server software upgrade is very straightforward for both. Just replace the server’s binary via package installation or source-code recompilation of the new version, then restart the service and you are good to go.
Final Thoughts
Redis and Memcached have both advantages and disadvantages. From the management and administration perspective, Redis is far superior to Memcached as highlighted above. Hope this article can give you a different perspective on deciding which key/value datastores that you would like to administer for the long run.