Discover Redis, a lightning-fast database!

NicolasBrondinBernard

Author
@NicolasBrondinBernard

Introduction to this rather surprising NoSQL database management system.

Article published on 17/02/2021, last updated on 10/08/2026

When we talk about databases, we often think of relational databases like MySQL, MariaDB or even PostgreSQL, but not all database management systems (DBMS) are relational, and not all of them use the SQL language.

All these systems are grouped under one name: NoSQL (literally not SQL).

But under this very (too) broad banner, we find very different systems, such as document, graph databases or even dictionaries (key-value).

Today we're going to talk about Redis, one of the most popular and fastest NoSQL databases on the market.

The particularities of Redis

Redis is above all a database system, its role is therefore to receive data, store it and redistribute it on demand, but its operation differs quite significantly from the most classic systems, notably through these three particularities:

"In-memory" data

Most DBMS store the collected data on a physical hard drive, but to improve the speed of his system, Salvatore Sanfilippo, the creator of Redis, decided that the data injected into it will be directly stored in the machine's RAM (random access memory).

Nevertheless, copies of the data are made very regularly and stored as binary files, so that the data can be reloaded and brought back into RAM in the event of a machine failure.

This in-memory storage makes it one of the fastest databases for data access, whether for reading or writing.

For reference, the official Redis documentation states that it is capable of "scanning a database of 1 million keys in 40 milliseconds while running on an entry-level laptop".

Key-value storage

Here there are no tables, no documents, but for each entry in the database: a value, associated with a unique key.

We more often talk about a dictionary, and this is actually where the name Redis comes from, which stands for "REmote DIctionary Server".

If you're used to indexing your data with primary (and secondary) keys, keep in mind that Redis only has a single index, that of the keys present in the database.

If you're used to using localStorage on the web, it's exactly the same thing, it's a key-value storage, a dictionary.

There's no auto-incrementation here, you decide the name of each key for each value you're going to store.

In order for them to be unique, keys are often composed of several "namespaces", most often separated by colons, which gives us for example: "user:nicolas@brondin.com" or even "analytic:signup:2020:02:17".

Data types

Behind each key in the database is a value, but this value can take on different types, ranging from a simple string of characters to complex and very practical types, of which here is a non-exhaustive list:

  • String of characters (String)
  • Lists of values (List)
  • Unordered set (Set)
  • Ordered set (Sorted set)
  • Dictionary (Hash)

If you're not familiar with this kind of data structure:

  • A set (Set) is a list whose values must not repeat
  • A dictionary (Hash) allows you to store objects in the form of key-values, as in JSON

A few features

Redis offers many features for finding a key (or a set of keys) very quickly, in order to then be able to retrieve the value, modify it or delete it.

As with a real dictionary, you don't search for a precise definition to find a word, that would take too long.

You first look for a word, sorted in a logical order, to see its definition.

Here, it's the same thing, we have a set of commands to browse this dictionary and modify it, for the rest, it's up to you to pay attention to how you're going to think about and create your keys!

Creating a key

With Redis, you don't ask to create new keys, you enter a value associated with a key, if the latter already exists, it is modified, otherwise it is created.

For example, to save a new value of type String, you can simply do:

  • SET key "value"
  • or MSET key "value" [key "value" ...]

The MSET command allows you to create several key-value pairs (String) in the same operation. Note that there are different commands for the other data types.

Link to the official documentation for SET and MSET

Multiple operations

Just like MSET seen previously, it is possible to perform commands on several keys at the same time, thus creating atomic operations (which cannot be separated into several sub-operations), for example:

  • MGET key [key ...] to retrieve the values of several keys
  • DEL key [key ...] to delete several keys

Link to the official documentation for MGET and DEL

Searching for keys by pattern

It is possible to search for a list of keys following the same format, using a syntax close to regular expressions.

Let's take for example a database containing the keys:

  • "user:nicolas@gmail.com"
  • "user:john@live.fr"
  • "sandrine@gmail.com"

To retrieve the keys of users registered with a gmail address, you simply need to do:

  • KEYS user:*@gmail.com

Link to the official documentation for the KEYS command

Transactions

Redis allows you to perform atomic operations of different kinds within a transaction.

The notion of transaction is essential in databases because it ensures that all operations have taken place successfully before saving the changes to the database.

This ensures data integrity, and with Redis it is possible to do this by combining the EXEC and MULTI commands like this:

MULTI
SET hello "world"
GET hello
EXEC

Here we will only retrieve the content of the hello key if it has indeed been created beforehand, this use case is obviously only for example purposes.

Link to the official documentation for MULTI and EXEC

And many others

The goal here is not to make an exhaustive list of all the features, but to show you that getting started with Redis is accessible to the majority of developers without too many obstacles.

To go further, it is also possible to manage streams, geo-spatial data, to manage real-time message sending...

Use cases

Among the companies that use Redis, we can notably mention Twitter, GitHub, Snapchat and StackOverflow, which are only a fraction of its users, but which show the solidity of this technology.

But in the end, what can Redis be used for? Can it be used as a classic database?

The answer is yes, but it's not the best use you could make of it.

If you have a data schema containing a lot of relationships between each other, then it's more sensible to use a relational database system as your main database.

But the interest of Redis is to run in parallel with your classic DBMS to manage specific data, either because there is a lot of it, or because this data requires a very high reading or writing speed.

So here are a few examples of using a Redis database:

  • Caching HTML pages
  • Caching SQL queries
  • Storing user sessions
  • Managing notifications for a social network
  • Real-time analytics and statistics
  • ...

Now it's up to you to find what use you're going to make of this database in your project's architecture!


Guillaume Jaillet sur Unsplash

Finished reading this article?
Our complete courses
Take it to the next level with our courses!

Complete courses, exercises and certificates to really learn programming!

4.8 average rating

Comments (0)

to leave a comment

No comments yet