Redis Database Explained | ninjasquad
As the first article in a series explaining the top most popular databases, let’s take a closer look at the Redis database.
Redis is an open-source in-memory data structure store, widely used as a cache, key-value database, and message broker. Moreover, it is oftentimes ranked as the most popular key-value database and one of the top NoSQL databases.
Before we dive into the Redis database details, let’s take a minute to learn more about its history.
Redis was created in 2009 by Salvatore Sanfilippo, also known as antirez. Its name comes from the Remote Dictionary Server and was created when antirez was working on a web logs analyzer for his startup. Initially, the prototype was written in the Tcl programming language and was later converted to C. After a couple of weeks, Salvatore open-sourced the project, which quickly gained popularity due to its speed, flexibility, and ease of use.
In 2015, Sanfilippo founded Redis Labs, a company that provides support, services, and products for Redis. Nevertheless, at the moment of writing, antirez is not a maintainer anymore and the company itself is called simply Redis.
How Does Redis Database Stand Out?
With all of that being said, let’s learn how Redis works and what makes it different.
So, as mentioned above, Redis stores data in memory which makes it much faster than most DBMSs (which persist data to disk). But, does it really mean that the data are lost if the server running Redis restarts or goes down? Not exactly.
Redis was designed to combine ultra-fast data access and modification using in-memory storage with data persistence to disk. To prevent data loss, Redis supports writing data to disk using different storage options such as:
- snapshotting (by default, every max 2 seconds),
- and append-only file (AOF) logging.
So, if the server restarts, the data are simply restored to memory from the disk space.
It’s worth mentioning, that although the Redis database comes with support for plenty of different data structures, it does not have support for queries known from other databases. It is a key-value store and, the commands (like GET or SCAN) are rather direct instructions than queries, which leverage indexes, etc.
Nextly, let’s take a look at a couple of Redis advantages:
- First of all, speed. At this point, we shouldn’t be surprised that the performance is a huge advantage for Redis. That’s what it’s been designed for and disk-based solutions simply cannot compete with their in-memory rival.
- Secondly, a huge and active community. And although I will use this argument pretty often in this series, we can never forget about it. A large community with a tremendous amount of materials available online has a huge impact on the ease of Redis incorporation. Time spent on searching and debugging can be significantly reduced and its excess can be used much better. Moreover, the Redis database is supported by a wide range of programming languages and comes with plenty of libraries and solutions, which we can use to make our lives easier.
- As the next thing- data types support. Redis, as a key-value store comes with support not only for string values but also for various other data structures. We can use hashes, lists, sets, sorted sets, geospatial data, and even JSON and Graph using dedicated modules. Depending on the type of value used, Redis lets us use dedicated commands, like sets union, differences, and lists sorting.
- Following, data persistence. And as I mentioned previously, by design, Redis combines low-latency read/write in-memory operations with data persistence. So in case, our server goes down or simply restarts, the data won’t be lost. Or technically, with the default snapshotting every 2 seconds, we’ll lose a max of 2 seconds of data.
- Moreover, it’s easy to use. Instead of sophisticated queries, we communicate with Redis using a pretty simple command-line interface.
- Lastly, Redis comes with many additional features, like transactions, pub/sub, or Lua scripting.
Redis Database Disvantages
So, with all of the benefits being covered, let’s take a look at the Redis disadvantages:
- As the first one, it can be expensive with large datasets. And although in-memory storage gives Redis a huge performance advantage over disk-persisting databases, additional memory can be much more expensive.
- Whatsoever, keys management. Redis is a key-value store and it is our job to define and maintain the appropriate keys structure.
- Moreover, it is not designed for complex queries and relationships. This makes it easy to use and fast, but it can also limit the ability to model complex relationships between data.
- Finally, it’s single-threaded. Redis is single-threaded, which means that it can only process one command at a time. This can limit its performance when dealing with a large number of concurrent requests.
At this point, we know what are Redis advantages and drawbacks, so let’s take a look at what can it be used for:
- Caching- Redis database can be used to store frequently accessed data in memory for fast access. It will be a great add-on everywhere, where latency matters.
- Session management- it can be used to store user session information, like preferences and login status. This can help us handle user requests much faster.
- Real-time analytics- and basically this was the main use case for which Redis was brought to life. Its support for data structures allows for fast and efficient calculations of real-time metrics.
- Leaderboards- thanks to the support for sorted sets it can be used for the easy ranking of items, making it well-suited for implementing leaderboards in applications.
- Messaging- with pub/sub messaging, we can easily implement message queues and real-time client-server communication.
- Product catalogs– lastly, it can be a great choice for product catalogs to store product information.
Which Companies Use Redis?
As the last thing, let’s take a look at a couple of known companies, which use the Redis database.
When we look at the list of the most popular companies, that were the first to adopt Redis in their technology stack, we will see GitHub and Instagram.
Nowadays, when Redis database has a well-established position in the market and proved for many years that it can be trusted and can bring value to almost any project, it can be found on the backend of companies, like:
- and many more.
Redis Database Summary
And that’s all for this article explaining what exactly the Redis database is. Together, we’ve learned more about Redis history, use cases, and advantages, as well as the disadvantages and the companies using it to deliver their products.
I hope you enjoy this series covering the most popular databases of 2022. If you enjoyed the content, please leave a comment in the section below.
Have a great day!