If there is API which needs to call again and again. To call API many times will make the website performance very slow. We can use Redis cache mechanism to make website performance faster.
Why Redis?
- It’s a “NoSQL” key-value data store. More precisely, it is a data structure server.
- With Redis, you gain a lot of power (such as the ability to fine-tune cache contents and durability) and greater efficiency overall.
- Redis gives you much greater flexibility regarding the objects you can cache.
- Redis is very fast and can perform about 110000 SETs per second, about 81000 GETs per second.
- Redis natively supports most of the datatypes that developers already know such as list, set, sorted set, and hashes. This makes it easy to solve a variety of problems as we know which problem can be handled better by which data type.
- All Redis operations are atomic, which ensures that if two clients concurrently access, Redis server will receive the updated value.
- Redis is a multi-utility tool and can be used in a number of use cases such as caching, messaging-queues (Redis natively supports Publish/Subscribe), any short-lived data in your application, such as web application sessions, web page hit counts, etc.
What to choose between Redis and memcacheD?
Installation :
> Installing Redis is so much easier. No dependencies required.
Memory Usage :
> For easy key-value pairs, MemcacheD is more memory efficient than Redis. Redis is more memory efficient, only after you use Redis hashes.
Persistance :
If you are using MemcacheD, data might be lost with a restart and rebuilding cache is a costly process. On the other hand, Redis can handle persistent data. By default it syncs data to the disk at least every 2 seconds, offering optional & tuneable data persistence meant to bootstrap the cache after a planned shutdown or an unintentional failure.
Replication :
> MemcacheD does not support replication, whereas Redis supports master-slave replication. It allows slave Redis servers to be the exact copies of master servers. Data from any Redis server can replicate to any number of slaves. Replication can be used for implementing a cache setup that can withstand failures and find the maintenance for uninterrupted facilitation to the application.
Storage :
> MemcacheD stores variables in its memory & retrieves any information directly from the server memory instead of hitting the database again. On the other hand, Redis is like a database that resides in memory. It executes (reads and writes) a key/value pair from its database to return the result set. Developers use Redis for real-time metrics & analytics too.
Read/Write Speed :
> MemcacheD is very good to handle high traffic websites. It can read lots of information at a time and give you back at a great response time. Redis can neither handle high traffic on read nor heavy writes.
Data Structure :
> MemcacheD uses strings and integers in its data structure. Hence, everything you save can either be one or the other. With integers, the only data manipulation you can do is adding or subtracting them. If you need to save arrays or objects, you will have to serialize them first and then save them. To read them back, you will need to un-serialize.
In comparison Redis has stronger data structures, which can handle not only strings & integers but also binary-safe strings, lists of binary-safe strings, sets of binary-safe strings and sorted sets.
Key Length :
> MemcacheD’s key length has a maximum of 250 bytes, whereas Redis has a maximum of 2GB.
Conclusion :
Use Redis, if you need to do operations on cached datasets at once or need to spread one enormous cache over a geographically challenged area. Read-write splitting against caching will enormously help performance and alleviate the cache connection starvation.