Memcached Vs. Redis

In our previous article, we have talked about Data Base Management Systems (DBMS) check out that article if you are interested. Now, if you were trying to squeeze more performance from a modern database-driven web application, then probably, the best thing to do is to go with Caching. The process of storing web pages, images & other types of web multimedia using an in-memory Data Storage System/ Cache System(CS) to reduce server lag. Each time you been through a discussion about that, mostly this question well raise – Memcached or Redis?

In this article, we are going to make an accurate comparison between them to come up with the best choice for your specific work.

First of all, both Memcached & Redis are in-memory (CS). That means they both stores most of the data – not all of it – In the Random Access Memory (RAM) of the server. Memcached is the product of Brad Fitzpatrick, meant at the beginning for his personal use, for his website LiveJournal, on May 22, 2003. Redis/ Remote Dictionary Server, on the other hand, was written by Salvatore Sanfilippo under Redis Lab, on April 10, 2009.

Caching System

Before the comparison let us first introduce their similarities;

Both Memcached & Redis:

  • Are key-value storing Databases.
  • Are free & open source in-memory (CS).
  • Were written in C. Memcached was initially written in Perl, then later rewritten in C by Anatoly Vorobey.

Without any delay, let us start our comparison:

Memcached Vs. Redis Performance

The CS type usage can directly affect your applications’ speed & reactivity. Now both Memcached & Redis can perform improving DB speed by caching its results, HTML fragments, or any other data that takes time to generate each time.

This point is related somehow to the next compression point Data Types Support. Because as Memcached provides a high-performance distributed memory cache service, it stops there! Nothing more. Redis, on the other hand, can perform that efficiently plus other highly valuable operations. For example, Redis can perform some unique operations on Server-end. That isn’t possible by any means with Memcached.

The following table states all of those features.

Memcached Vs. Redis Performance

Data Types Supported

In terms of data types support, Memcached supports only data records & strings of the simple key-value structure, while Redis supports a wide range of different data structures, with the commands you need to make the most of those data types. Also, using Redis Object internally, it represents all the keys & values, allowing them to inspect internal objects associated with keys.

we listed a detailed set of those extra data structures in the table below:

Memcached Vs. Redis Data Types Supported

Server operating systems

The OS your DB running shouldn’t be a matter of concern as most servers holding databases operates using one of those five operating systems, listed in the table below. But if your server uniquely, runs on Unix, then integrating Redis with your application isn’t possible.

Memcached & Redis OS support

 

Memcached Vs. Redis Memory Management

Memory management efficiency is a crucial factor influencing the system’s performance. The way Memcached & Redis manage memory is very different. One significant managing scheme difference is that Redis doesn’t store all the data in the RAM/ Server Memory.

Now, we can clarify the concept behind the Random-Access Memory (RAM) as the following;

The reason why it’s considered “Random-Access,” is that any of its memory cells are accessible directly if the row & column that intersect at that cell was known. Because of its “Random,” it can’t store long-term data; it can only temporarily, store information needed for specific operations at the time you are interacting with it.

 

Cash Systems

Cash Systems are the managers of those RAMs, they copy the data of your frequent operations, queries, & more, from the Servers’ Hard Drives “Database,” keeping them in the RAM to retrieve them quickly the next time they are needed.

As storing data in the RAM doesn’t make it permanent, Redis provides the option of storing those data in files in the disk, so they can’t be lost. By the way, this doesn’t mean it operate randomly, Redis calculates the values for the keys to move it to the disk-based on this formula;

Redis Swapability

Then makes these values for the keys permanent into the disk & erases them from RAM, which allows users to keep much bigger data than any Random Memory can hold.

But! Hold on. This feature isn’t always beneficial, because When reading data from Redis. If it didn’t find the value of the read key in the memory, then it needs to load the corresponding data from the disk file & then return it to the requester. Here the problem is that Redis can respond only after successfully loading all the swap files from the desk. Here you can imagine the sacrifice of some concurrency & velocity! So, this feature is suitable for batch operations with a small number of clients, but not of the same value for a large website program.

C language malloc/free functions

For the In-Memory data storing – In C language – the most common functions for distributing & releasing memory are malloc/free. These functions have a crucial defect for developers. The problem is that unmatched malloc & free can easily cause memory leakage. As well as, the high-frequency calls make it difficult to recycle & reuse that amount of memory fragments. This reduces memory utilization & causes the system calls to consume a far larger system overhead than the general function calls.

Each of Memcached & Redis adopts their memory management mechanisms to solve this issue;

Memcached

Memcached uses what called the Slab Allocation Mechanism; segment the allocated memory into slices of a predefined length to store key-value data records of the same length to solve the memory fragment problem completely. This method, however, works very efficiently. Except for that, it may cause space waste. As a result of the system being allocating each & every slice in the memory space of a specific length. So, longer data might fail to utilize the space fully.

Memcached Slab Allocation Mechanism

Redis

On the other hand, Redis uses a more convenient mechanism. Redis stores memory size in the memory block header following memory allocation like the figure below;

Redis Memory Management

Now, (real_ptr) reflects the pointer returned after Redis calls malloc. It stores the memory block size in the header & keeps the memory used by the waist always able to determine. So that the system returns the length of the size type, then the ret_ptr. When it needs to release memory, the system passes ret_ptr to the memory management program. That program can easily calculate the value of real_ptr & then pass real_ptr to release the memory through the ret_ptr. This what gives Redis the advantage of not causing any space waste.

Programming language support

Based on what programming language your database & application were built with, the choice of one CS on behalf of another may drastically differ!

Memcached & Redis doesn’t have all programming languages support.

The following table states – nearly- all programming languages “that are still in use” nowadays;

Memcached & Redis programming languages support

Memcached Vs. Redis Ease of Use

Even though the install-&-run process of Memcached isn’t that easy, while the other is much easier & no dependencies required. Both of them are syntactically easy to use & require a minimal amount of code to integrate into your application. That means anyone can use both with a minimal amount of coding knowledge.

However, Redis seems to be having more public interest over the latest years, according to Google trends;

the Redline represents Redis.

A close up of a piece of paper Description automatically generated

Remember that the more a tool is public, the more community support & information resources you can find.

When to choose each of them?

Memcached:

  • When caching relatively small & static data.
  • When caching doesn’t need to be long term stored – as Memcached stores all the data in the RAM – & doesn’t include any disk saving feature.
  • If you are working with HTML code fragments.
  • When your work needs high scalability – as Memcached is multithreaded – & Redis is single-threaded.

Redis:

  • When you need to store data in a variety of formats: array, lists, sets, & sorted sets.
  • If caching needs to be long term stored.
  • When you need the manipulation of the cached data along with intelligent caching.

Outline

  1. Memcached provides high-performance distributed memory cache service, & Redis can perform that efficiently plus other highly valuable operations.
  2. Redis supports a wide range of different data structures, while Memcached supports only data records of the simple key-value structure.
  3. Most servers holding databases operates using one of those five operating systems; (BSD, Linux, OS X, Unix & Windows). Both Memcached & Redis can work with all of them except for – Unix with Redis – as it doesn’t support this OS.
  4. The choice of one CS on behalf of another may drastically differ Based on what programming language your database & application were built with. Redis supports way more P-languages compering to Memcached. (Check table 4)
  5. The install-&-run process of Memcached isn’t that easy, while the other is much easier & no dependencies required. But Both of them are syntactically easy to use.
  6. The Memory Management – Slab Allocation – Mechanism of Memcached is very efficient, but it causes space waste. While the mechanism adopted by Redis doesn’t create any waste.

 

 

 

 

 

 

 

 

 

 

 

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here