[ Memcached vs Redis ] Which One is Best For Your Website

This is the question arise every-time when you think to scale your application(actually database driven application). Database driven application is the application where most of the data are stored and fetched from the database.

Caching is the first option when thinking about the performance improvement. But you don’t know which caching server will give you the best performance.

So here I will be comparing features of both the caching server which will helps you to decide between apple and apple.

1. Data Structures

Memcached support only string data type which is ideal for storing read-only data because it’s not easy to modify the string value. If you have to store array or objects in Memcached you have to serialize them first and then store them.

So if you have to alter the array you have to read it and then un-serialize it and alter the array and then store it back to the same key location.
While Redis support almost all types of data like Array, String, Integer etc. And Redis provides the functionality to alter the data directly into the system.

Redis gives you much greater flexibility regarding the objects you can cache. While Memcached limits key names to 250 bytes and works with plain strings only, Redis allows key names and values to be as large as 512MB each, and they are binary safe.

2. Replication

Redis support for master-slave replication. Where the read operation can be done on the slave and the write can be done on the master. The slave server is the exact copy of the replication provides scalability and availability. As any one of the slaves gets failed, the other slave provides data access.

Memcached does not support replication.

3. Memory Efficiency

For simple key-value pairs, Memcached is more memory efficient than Redis. Redis is only memory efficient when you are using hashes.

Memcached is designed to represent a plain string to string dictionary which can make better use of memory.
But Memcached is not perfect from to point of view of memory utilization. If you have an application which dramatically changes the size of cached value over time you are likely to waste a lot of memory on your server due to fragmentation and the only solution is “Reboot”. And rebooting in Memcached costs a lot as you will lose all your cached data and you have to re-cache all the data. And of-course rebooting is not the permanent solution.

Redis is a lot more deterministic from this point of view.

4. Storage type

Memcached stores variables in its memory. It retrieves any information directly from the servers memory instead of hitting the database again. On the other hand, Redis is like a database that resides in memory. It executes (read and write) a key/value pair from its database to return the resultset and all data resides in memory. Developers are using Redis also for real-time metrics, analytics.

5. Pipelining

Redis provides an important feature “pipelining”. If you have many commands to execute, you can send all-of-them at once to Redis instead of one-by-one.

Generally, when we have to execute multiple commands to either Memcached or Redis, each command is a separate request-response cycle. Using pipelining Redis can buffer these commands and execute all of them at once with providing the response in a single reply.

This can provide you better performance where you have to run multiple commands to finish an action.

6. Lua Scripting

Lua scripting is kind of Redis own SQL or stored procedure.
Sometimes you have a complex operation and you want to finish this operation atomically but you can’t afford roll-back in case of the complex operations in case of failure.

These type of problem can be solved with lua scripting and of-course many more problem can be solved with lua scripting.

The entire scripting is executed atomically. So if you can fit your logic into a lua script you can save a lot of locking transactions.

7. Transactions and Atomicity

Commands in Redis are atomic, meaning you can be sure that as soon as you write a value to Redis that value is visible to all clients connected to Redis. There is no wait for that value to propagate.

Technically Memcached is atomic as well, but with Redis adding all this functionality beyond Memcached it is worth noting and somewhat impressive that all these additional data types and features are also atomic.

8. Data Persistence

Redis persist the in-memory data to a file system on regular intervals. During the failure of Redis node, the data can be restored from Redis Data file.

While data is Memcached is persistent only till you reboot the system. Memcached will clean-up the data when you reboot the system and you cannot get the data back.

Conclusion

Both are the great piece of software when we talk about caching. It’s not wise to move to Redis if you are already using Memcached and want to use Redis only for the key value caching. Because both are almost same in case caching. Use Redis if you need to do more operation on the cached data.
On the other hand, you might want to stick to Memcached for its simplicity, reliability, and speed.

But if you are going to set-up new caching server on your project go for the Redis as it will give you the whole new experience of caching