Before we start, I would like to thank Brian Sam-Bodden, who’s a developer advocate for Redis, for kindly reviewing this story. Thank you, Brian!
You probably know or even used Redis as a cache. Did you actually know it’s much more than that?
Brian did a presentation on Redis OM Spring which made me very interested in understanding and experimenting with Redis’ capabilities. This is the first story of many that will soon come. Join me in my newest adventure. 🙂
1. Redis is an actual database
Redis is not a Cache, Redis is an actual database and it just happens to work really well as a cache. More than that, Redis is one of the most popular open-source engines today and it was named the “Most Loved” database by Stack Overflow for five consecutive years.
There’s nothing preventing you from using Redis as your primary Database. And besides working as a database, it also has different features that we’ll be exploring later in this story.
2. Redis has more than one mechanism of persistence
Probably the first thing most developers think of when they think of Redis is that they will lose all their data if the instance goes down. However, that’s not exactly true. Redis is a database and it just happens to be memory first instead of memory last.
Although all its data is stored in memory, Redis has two ways of writing to the disk.
The first one is what they call Redis Database, or in short, RDB. And the second one is what they call Redis Append Only File, or in short, AOF. You can choose to use one of them, both of them, or none of them. Let’s dig a bit into each one:
The RDB persistence performs point-in-time snapshots of your dataset at specified intervals, let’s say, every 5 minutes.
The disadvantage is that if your server goes down for whatever reason, you may lose minutes of data, which is not ideal if you cannot deal with data loss at all. But, on the other hand, due to the nature of how the snapshots are created, you are able to restart your server much faster when needed.
AOF is a persistence mechanism that allows the Redis server to keep track and log every command executed on the server.
These command logs can then be re-played when the server starts up, recreating the database to its original state. Using AOF, Redis appends each command sequentially executed on the server. This prevents any data loss due to incorrect command orders. However, AOF is much slower than the equivalent RDB.
What Redis recommends is that you have both options turned on to have the best of both worlds.
You can check my story where I deep dive into Redis Persistence if you want to read more about persistence on Redis:
3. Redis is super fast
As you already know, all data in Redis is stored in memory. Accessing data in memory is much faster than accessing data on disk. An operation in Redis takes less than a millisecond, which is a great performance advantage.
Accessing 10 thousand records from the disk will take an average of 30 seconds to run. While accessing the same number of records from memory would take 0.0002 seconds to run.
A Redis database supports millions of operations per second. Which means it’s a super-fast read/write system.
One could still think that if this is stored in memory, it means we could easily lose all our data, but as we previously saw in this story, Redis has more than one mechanism to prevent this from happening.
4. Redis has flexible data structures
One of the problems with simplistic key-value databases is that if developers store anything more than simple keys/values, they will end up doing work on the client-side (for example, de- and re-serializing JSONs, or splitting and re-joining lists).
Redis mitigates this issue by offering flexible data structures that enable you to develop applications with fewer lines of code to store, access, and use your data.
These data structures avoid the overhead associated with translation between application objects to database entities for every database operation.
Among these data structures, you can find:
- Strings — text or binary data up to 512MB in size
- Lists — a collection of Strings in the order they were added
- Sets — an unordered collection of strings with the ability to intersect, union, and diff other Set types
- Sorted Sets — Sets ordered by a value
- Hashes — a data structure for storing a list of fields and values
- Bitmaps — a data type that offers bit-level operations
- HyperLogLogs — a probabilistic data structure to estimate the unique items in a data set
- Streams — a log data structure Message queue
- Geospatial — a longitude-/latitude-based entries Maps, “nearby”
- JSON — a nested, semi-structured object of named values supporting numbers, strings, Booleans, arrays, and other objects
If you want to read more about Redis Data Structures, you can check their documentation right here.
5. Redis has a built-in primary replica architecture
Like any other process, the Redis process might be killed for whatever reason which can cause our data source to be unavailable while the process is being restarted.
To prevent downtime, Redis has a built-in Primary/Replica architecture that not only allows Redis to recover faster from downtime but also improves read performance.
When the main process is killed, one of the Replicas becomes the Primary and processes all requests while the Primary process is being restarted. Once the Primary is up again, it is re-instantiated as the main one while the Temporary Primary goes back to being a Replica again.
If you want to read more about Redis Replication and High Availability, you can check their documentation right here.
6. Redis is Popular and Open Source
As mentioned previously, Redis is super popular, and although it’s not the 1st (it’s the 5th) most popular database, it was elected the most loved database by Stack Overflow for 5 consecutive years, which means most developers don’t actually know how powerful Redis can be.
And besides being super popular, it’s also open-source, which means it has a big community around it and it’s very well documented online.
I’m very happy you got to this part of the story, I‘m truly thankful for this.
Support my work: follow me and clap to this story.
7. Redis has transactions
Different from other NoSQL Databases that support transactions only on the document (row) level, Redis can run a series of commands in a single transaction.
All the commands in a transaction are serialized and executed sequentially. A request sent by another client will never be served in the middle of the execution of a Redis Transaction. This guarantees that the commands are executed as a single isolated operation after the EXEC
command is triggered.
To start a transaction, the user needs to trigger the MULTI
command, and although Redis doesn’t support rollbacks, it has a very similar feature triggered by the DISCARD
command, which will discard any commands queued to be executed.
And Redis even supports Optimistic Locks by using the command WATCH
. By using this command, Redis will fail the transaction in case there are race conditions and another client modifies the result in the time between our call to WATCH
and our call to EXEC
.
8. Redis is a message broker
Redis is not only a database, it’s also a message broker.
Redis supports typical pub/sub operations, such as publish and subscribe. Publishers publish messages to a channel, or multiple channels, and subscribers subscribe to one or more channels.
One thing to bear in mind is it works very similarly to a Radio Broadcast. You are able to listen to a station while you’re tuned into it. However, you’re incapable of listening to any message broadcasted while your radio was off.
Redis Pub/Sub will only deliver messages to connected subscribers. This means that if one subscriber loses connection and this connection is restored later on, it won’t receive any message it has missed nor be notified about any missed message. Therefore, it limits use cases to those that can tolerate potential message loss.
9. Redis is supported by Spring
If you are used to implementing code with the Spring Framework, we have good news!
Redis is also supported by the Spring Framework. There are currently two solutions well developed for this matter. One is Spring Data Redis, developed by Spring and the other one is Redis OM Spring, which is maintained by Redis and built on Spring Data Redis as well.
You can read more about it here.
10. You can extend Redis functionality with Modules
And this is the icing on the cake and probably the coolest on this list.
Redis modules extend the functionality of Redis by using libraries that will run on top of Redis. Modules enrich Redis with core data structures and modern data models.
RedisTimeSeries
Time-series databases are great for large ingestion and metrics analysis. RedisTimesSeries adds a time series data structure and features to Redis that you can find in other time-series databases like querying by start date and end date and also aggregation functions (max, min, sum, avg, etc). What you cannot find in other time-series databases is the low latency read and high volume inserts that Redis can provide.
You can read more about RedisTimeSeries here.
RedisGraph
Graph databases are trending right now. They represent an ideal solution for storing data and connecting relationships between data much more effectively than traditional relational databases. And Redis already has a module for that.
You can read more about RedisGraph here.
RedisJSON
With the RedisJSON module, Redis can store native JSON type data, through which you can easily access various attributes in JSON, similar to MongoDB.
You can read more about RedisJSON here.
RediSearch
RedisSearch is a full-featured search engine that provides secondary indexing, full-text search, and a query language for Redis. These features enable multi-field queries, aggregation, exact phrase matching, and numeric filtering for text queries.
According to this benchmark run by Redis, they found RediSearch to be 58% faster 58% on indexing and x4 faster when performing two-word searches on the indexed dataset.
You can read more about RediSearch here.
Other modules:
Those modules that were described above are maintained by Redis itself, however, anyone can write new modules to Redis, and you can find a list of available modules here.
Conclusion
Redis is much more than a simple Memcache. By reading this article you not only learned that Redis is an actual database, but also many powerful abilities that it provides.
I hope you have enjoyed this story. I will be diving more into Redis on the following stories, so don’t forget to follow me and subscribe to my next stories!
Leave a Reply