Redis and its alternatives: Important points to decide distributed caches
Caching is one popular solution when looking to increase performance in accessing data. When you look around to search for distributed cache solutions top 3 would be: Redis, Memcahed and Hazelcast.
Redis
The Redis project was started in early 2009 by an Italian developer named Salvatore Sanfilippo. Redis was initially written to improve the performance of company internal product.By June of 2009, Redis was stable enough to use in production. Salvatore fostered a great community, added features at a very rapid pace, and dealt with any and all reports of database corruption, instability, etc. with the utmost severity and rest is history.
Redis is a way to store key-value pairs, in a selection of different data types such as Lists, Sets, and Hashes. Redis keeps this data in memory, meaning that it is extremely fast to return the data when requested. This speed makes it perfect as a cache for your application where you need to request and return data and speed is an important factor.
More about Redis at: https://deeptimittalblogger.medium.com/redis-something-to-think-of-while-choosing-data-store-next-time-377727039ff8
Memcached
Memcached was first developed by Brad Fitzpatrick for his website LiveJournal, on May 22, 2003. It was originally written in Perl, then later rewritten in C by Anatoly Vorobey, then employed by LiveJournal.
Like Redis, Memcached is an open source way to store key value pairs in memory, meaning that data is very quickly retrieved. This makes Memcached another way to return data where speed is a factor. Memcached is also multithreaded, meaning that there may be some performance improvements where your application can utilize multiple cores.
Hazelcast
Hazelcast another distributed cache solution is both the name of the product and the company that created it. The start-up was founded in 2008 by Talip Ozturk and Fuad Malikov. The first open-source implementation of Hazelcast was released at the beginning of 2009. Several updated versions have been made public since then, the latest one dating from February 4, 2020. Hazelcast is also provides key value store and great for storing complex data objects.
Lets first compare Redis with Memcached to see where we can use simple Memcached.
Redis vs Memcached
- Data type support: Memcached stores data only as String, so to update or process data entire data needs to be fetched. Redis has great support for data types.
- Persistence: Data in memory will be wiped off if machine shuts down for any reason hence its important that data is backed up in disks as well. Redis has that back up while Memchached does not have it, which makes it suitable only for small use cases where lossing data is not very critical.
- Availability: Redis follows master-slave pattern to ensure high availability where memcached does not support that.
- Multithreading: Memcached supports multithreading, so there could cases where memcached might outperform Redis as it can use multicore because Redis is single threaded.
Decision: Considering above differences memcached is good to use when you have small applications which does not require high availability and if data is lost it can be re-generated easily. For larger applications Redis could be a good use cases becasue of persistence and high availability.
War does not end here, Let see how Redis and Hazelcast compares with each other. If you check documentation on Hazelcast website they have been proudly calling it as Redis replacement.
Redis vs Hazelcast
- Multithreading: Hazelcast being multithreaded can outperform Redis in some use cases in terms of performance.
- Hardware scaling: Hazelcast supports automatic discovery of node by just proving IP addresses, and it takes care of rebalancing data. In Redis there are good manual steps to add new node because cluster rebalancing is not done out of box. Same goes for removing nodes, in Redis for removing master nodes we need to rebalance ports again and migrate the data.
- Hardware cost: For redis to have high availability master slave pattern is followed in topology, while in Hazelcast same nodes are used to have data back up for other master nodes, hence less physical machines used in hazelcast.
- Client libraries: Hazelcast has very few language support compared to Redis.
- Querying support: Though both supports key value storage but hazelcast is able to store complex data objects. Hazelcast provides support to build SQL like query through which data can be queried using specific fields in value. In Redis you can only query data through keys. This proved to be great decision maker as it will help in getting less data across network and also becomes great tool for debugging later.
- Compute: Both Redis and hazelcast supports direct compute functions on nodes. While Redis supports lua script, so u have to learn another scripting language, while Hazelcast supports Java programs. Another difference in Redis lua script does not reach to other nodes and has to be executed on individual nodes while Hazelcast can execute same program on every node.
- Read-write through pattern: Redis alwasys works as standalone and does not have any integration with any DB. While Hazelcast provides read write through pattern where it can be used as cache for DB.
- Documentation and community support: Hazelcast has very less documentation and community support.
- Understanding the internals: Hazelcast is written in Java and redis is written in C. How does it matter to us. As a Java programmer while I was trying to understand deep of how Redis key expiration, it took me 3–4 days as I am used to read code in OOPS structure. In Hazelcast understanding internals would not be that difficult because of programming language.
To me Hazelcast looks clear winner for so many aspects but as it has not still reached the same maturity and community support makes me still more inclined to use Redis for larger applications.
I have not used Hazelcast and have been using Redis extensively. For my next opportunity to use distributed cache solution ,I am definitely going to give Hazelcast try to do more comparison between two and get few numbers to compare.