Redis vs. Memcached : A detailed comparison

Nishant Joshi 05th Sep 2020

Redis and Memcached are two different types of in-memory data storage systems.  The Redis is an open-source key-value database or Memcached is a distributed memory caching system that speeds up database-driven websites and web application.

Memcached allows operations like Set, Get, Add, Replace, Delete, etc. On the other hand, Redis allows operations on different data types like hash tables, strings, sets, sorted sets, lists, etc. This article makes an in-depth comparison of these two technologies.

You can read another article to install memcached with wamp and PHP

Feature Comparison

For making data changes in Memcached, you have to copy the data to the client’s end and then set the data back. This leads to an increase in data sizes and network IO counts. As far as Redis is concerned, such changes can be made through the general GET/SET operations. Hence, Redis is a better choice for executing more complicated operations that require cache.

For simple key-value storage, Memcached has a higher memory utilization rate. In contrast, if Redis uses the hash structure, it will have an even higher memory utilization rate than Memcached.

Since Redis uses only single cores, it provides better performance than Memcached for small data storage purposes. But Memcached outstrips Redis when data to be stored is 100k or above.

Differences in memory management

Redis has a unique way of storing data. When there is no more space left in the physical memory, it swaps values that haven’t been used for a long time. The swap operation will trigger when memory usage surpasses the threshold value. This quality allows Redis to store more data than its machine memory capacity.

Unlike Redis, Memcached uses the slab allocation mechanism for storing data. It divides the allotted memory into small parts of a preset length to save the key-value data records of the corresponding length.

The memory management in Redis is carried out by two files, namely zmalloc.c and zmalloc.h, in the source code. It stores the distribution of memory by establishing an array whose length is equal to ZMALLOC_MAX_ALLOC_STAT. All the elements in the array set forth the number and size of memory blocks designated by the prevailing program.

Data persistence feature

Redis provides two data persistence policies, namely AOF log and RDB Snapshot. On the other hand, Memcached doesn’t support data persistence services.

1. AOF log

The Append Only File (AOF) is a plaintext whose contents are Redis standard commands. Redis appends commands that produce data changes to the AOF. A log is generated with every command for changing data. As a result, the AOF file becomes large in size.

Thus, Redis comes with another option called the AOF rewrite that is used to regenerate an AOF file. In the new AOF file, there is only one operation for each record. The generation process is as follows: Forking a process, traversing the data, and writing data into the new temporary AOF file.

When data is written into the new file, all the write operation logs are written on the old AOF file. These are recorded in the memory buffering zone simultaneously.

After the operation is completed, all the logs in the buffering zone are written by the system to the temporary file at one time. Hereafter, the atomic rename command is called out to replace the old AOF file with the new AOF file.

AOF is a write file operation that writes the operation logs to the disk. The following are the three settings option in the appendfsync: appendfsync no, appendfsync everysec, and appendfsync always.

This article will help to use Memcached with PHP and Mysql

2. RDB Snapshot

Redis stores the snapshot of the prevailing data into a data file for persistence, that is, the RDB snapshot. Redis employs the copy on write mechanism of the fork command. Once the snapshot is created, a subprocess is forked, making all the data cyclic and writing them on the RDB file. The save command of Redis is used to configure the timing of an RDB snapshot.

Example of RDB Snapshot

For example, you can configure snapshot generation after each 1,000 writes, if you want to configure snapshot generation for once every 10 minutes. You can also change numerous rules for implementation simultaneously. The configuration files of the Redis contains the definition of these rules.

During the runtime of Redis, you can set rules using the CONFIG SET command of Redis. You can do this without restarting Redis. Since the Redis’ RDB file performs its write operations in a new process, it is incorruptible.

RDB Snapshot

The Redis-generated subprocess will first write the data into a temporary file after the RDB file has been generated. Afterward, the temporary file is renamed into an RDB file through the atomic rename system call.

Hence, whenever Redis suffers a fault, the RDB file is always available. Simultaneously, in the internal implementation of Redis’ master-slave synchronization, the Redis’ RDB file is also a link.

However, the data saved in the RDB file maybe not up-to-date once the database encounters some problem. Also, you will loose the data during the period from the last RDB file generation to the Redis failure. But this is tolerable for a few businesses.

Authored By Nishant Joshi

Nishant likes to read and write on technologies that form the bedrock of modern-day and age like machine learning, data science, AI, and robotics. He works for Sage Software Solutions Pvt. Ltd., a leading provider of CRM and ERP System to small and mid-sized businesses in India.

Also on DiscussDesk