What is Redis?
Developers looking for a powerful, feature-rich database should look no further than Redis. According to the DB-Engines ranking, Redis is currently the 8th most widely used database management system, as well as the most popular choice worldwide for key-value databases. What's more, Stack Overflow's 2019 survey found that Redis is the "most loved" database technology, ahead of alternatives such as Oracle, MySQL, and Microsoft SQL Server.
But what is Redis exactly, and what are the benefits and features of Redis? In this article, we'll discuss everything you need to know about Redis, so that you can decide whether it's the right database technology for your situation.
What is Redis?
Redis (which stands for REmote DIctionary Server) is an open-source in-memory data structure store that is commonly used to implement non-relational key-value databases and caches.
This distinction is important: Redis is technically a data structure store, and not a key-value database. In traditional key-value databases, each key is an arbitrary string that is associated with an uninterpreted string of bytes (which may be a string, an integer or floating point number, an image, binary data, etc.)
Data structure stores such as Redis remove this limitation. The key may be a string, or a more complex data structure such as a list, set, hash, bit array, or stream. This enables developers to take advantage of multiple Redis use cases:
- Key-value database: Redis is commonly used to implement a non-relational key-value database with a lightweight and efficient footprint. For example, a database with 1 million small keys (e.g. strings) occupies roughly 85 megabytes in memory.
- Cache: Because it can store arbitrary data, Redis can also implement an application cache. Data is stored in-memory, delivering the best possible performance when reading from and writing to the cache.
- Message broker: Since version 2.0, Redis has supported the publish/subscribe messaging pattern, which allows it to be used as a message broker as well. Redis' ease of use and high performance have won converts from other messaging systems such as RabbitMQ.
With major tech companies such as Twitter, GitHub, and Pinterest all using Redis, it's no surprise that Redis is both popular and widely beloved among developers.
How Does Redis Work?
Redis works by storing keys that map to one of five different data structures: STRINGs, LISTs, SETs, HASHes, and ZSETs (sorted sets). Some Redis commands are shared among all of these five data structures, while others are only used by one or two of them.
In order to protect the integrity of the database, there are four main versions of Redis that you can deploy:
- Redis standalone with vertical scalability and no resilience.
- Redis master-slave replication in which a single master replicates data to multiple slaves.
- Redis Sentinel, which monitors a master-slave system and automatically reconfigures a new master if the old one goes down.
- Redis Cluster, which uses a multi-master architecture to provide both scalability and resilience for very large deployments.
Redis Benefits and Features
- Lightweight footprint: Redis is written in ANSI C and has no external dependencies, which means that it's very lightweight and efficient.
- Sharding: Redis Cluster implements sharding, which allows Redis to handle multiple concurrent transactions and/or write requests.
- High availability: Redis supports master-slave replication in order to improve the database's performance and availability. If the master database fails, a backup slave database can take its place until the master recovers.
- Wide support: There are more than four dozen programming languages with Redis clients, including Java, C/C++, Python, Haskell, Ruby, and nearly any language in wide use today. Redis officially supports the Linux and macOS operating systems, and unofficial support for Windows is also available.
Redis Comparison
Of course, Redis isn't the only database and caching solution on the market. So how do Redis alternatives such as Hazelcast, Apache Ignite, and Memcached stack up?
Both Memcached and Redis are high-performance key-value databases. Unlike Redis, however, Memcached is only capable of storing data in plain string keys. Redis, on the other hand, supports multiple storage formats for data and can automatically write data to disk in two different ways (AOF and RDB). Redis also gives more flexible control over cache eviction policies, and supports data replication in order to improve availability and withstand unexpected crashes.
Redis also looks good when compared with alternatives such as Hazelcast and Apache Ignite. For example, neither Hazelcast nor Ignite has the rich array of Java distributed objects and collections that you have access to with a third-party Redis Java client like Redisson.
There are significant technical differences between Redis and Hazelcast, in particular. Redis is single-threaded, which saves developers from dealing with the "split brain" networking problem, while Hazelcast is multi-threaded. Redis also uses the jemalloc memory allocator to efficiently manage various data types, while Hazelcast stores data on the heap, which may affect the performance of larger applications.
Redis Java Clients
Redis already has a great deal to recommend it - and that only becomes more true if you supplement it with a third-party Redis Java client such as Redisson.
Because Redis doesn't include built-in compatibility with programming languages such as Java, many developers choose to install a third-party Java client library. Redisson, in particular, stands out for its rich offering of Java objects and collections. In addition, Redisson includes support for the asynchronous, reactive, and RxJava2 programming models, as well as integrations with popular Java tools such as Spring, JCache, Hibernate, and Apache Tomcat.
Redisson stacks up favorably in comparisons with other third-party Redis Java clients, such as Jedis and Lettuce. In particular, Redisson PRO is significantly faster than Jedis when running common Redis commands such as HSET, RPUSH, SADD, SET, and INCR.