Introduction
Redis (Remote Dictionary Server) is an in-memory multi-model database famous for its sub-millisecond latency. It was created in 2009 by Salvatore Sanfilippo (also known as _antirez_
).
Faced with the problem of scaling some types of workloads using traditional database systems for his Italian startup, Sanfilippo began to prototype a first proof of concept version of Redis in Tcl
. He later translated that prototype to C
.
After open sourcing the project, with the help of Ezra Zygmuntowicz from Engine Yard, it quickly became popular with startups. Instagram and Github were some of the first companies to use a Redis implementation in their stack.
Redis Data Types
Redis supports the following data types in its store:
- Strings – sequence of characters (text) or binary data
- Lists - (linked lists) node-pointer based linear collections of data elements in the order that they were added in
- Sets – unordered collection of un-repeated string elements
- Sorted Sets – Sets associated with a score so the order can be maintained
- Hashes – store a mapping of keys to values, the values that can be stored in hashes are strings and numbers
- Bitmaps – also known as bit arrays and bit vectors are array data structures that store bits
- HyperLogLogs – a heuristic data structure used to count unique items while using very low memory, the downside is a standard error rate of 1%
- Streams - append only data structure where stream entries are composed of one or multiple field-value pairs
- Geospatial indexes - store named points in their associated latitude and longitude coordinates

Why is Redis Useful?
Redis uses a design where the data is always read and modified from the random access memory (RAM) on a computer. It can also be stored on a disk-- in an inappropriate format for random data access-- but only to reconstruct the data in memory after the system restarts.
his is why Redis can be considered both a store
and a cache
. We can use it to decrease data access latency, increase throughput, and ease the load off our relational or NoSQL database and application.
Other benefits of Redis include simple authentication: designed to be only accessed by trusted clients, and can be restricted to certain interfaces.
Let's test your knowledge. Click the correct answer from the options.
Why would we want to implement Redis as a cache for our web application?
Click the option that best answers the question.
- To improve the performance of our web app
- To save and recover data in case of a database failure
- So we can use Strings and Lists and Sets in our application
- For extending our modules
Redis Use Cases
Redis as a cache instance
Let's look at a simple example where we can use Redis as a cache to improve performance and utilize it to the maximum potential.

Let's say we have a web application that takes too long too retrieve data from a database server. Suppose iit takes from 30 to 60 seconds to perform that operation.
This waiting time is a terrible experience for the user. To fix this problem we can leverage a Redis
instance. Now, instead of directly querying the database and waiting 30 to 60 seconds, we can store data in a Redis cache instance and just query that instance running in the system memory (RAM).
The web server will first check if Redis has the data that it requires. If it doesn't have the data, this will be considered a cache miss
. The web server will then query the database and retrieve the data that it needs. After that it will populate the Redis instance with that data-- so that the next time it needs to retrieve that data, it will get it from the Redis instance. We call this a cache hit
.
This way we will bring down the total wait time to less than 5 seconds or even less than 1 second.
Redis as a multi-model database
Redis can also be used as a fully fledged multi-model database that can be used to store and and keep multiple data formats. Let's look at one case where we would make a lot of things simpler and easier by implementing a database with it.
We have a complex social media application that uses multiple microservices and different types of databases including Relational, Graph and Document type databases, and also a cache.

As we can see all these data services need to be maintained and deployed which means it takes a lot of work, energy and experience to keep them in a good and running state. Another thing which we will face in the future with this architecture is scaling
: when scaled all of the mentioned data services will have different requirements which can be an additional challenge for our team.
A good workaround for these problems is to use cloud platforms which comes with a drawback: we will have to pay for each managed data service separately. On the backend, we also face a problem: the application code gets very complex because we have to talk to multiple data services, and for each we need a separate connector and logic. This causes quite a few difficulties like higher latency due to each connection step between the services. This will add some latency to our application, and because of this, testing our application becomes challenging as well.
To solve all of these issues we can Redis as multi-model database. We will have only one data service so our microservices will need to talk to only a single data store, we will have less latency, less complicated code in our backend, we will be able to store multiple type of databases into one. Redis can also additionally act as a cache to improve the performance of our web application.
Are you sure you're getting this? Click the correct answer from the options.
What is the main reason that Redis makes our web applications work faster?
Click the option that best answers the question.
- Because it has different modules
- It has a lot of data types that other databases don't support
- It runs on the main system memory
- Because it is an open-source project
How Do We Actually Use Redis?
Every data point in the Redis database is a key
followed by one of many data structures. We can store data naturally like we would in our favorite programming language instead of sticking it inside a bunch of tables or documents.
To interact with the database we can use a simple set of commands like SET to create a key with String value:
1SET hello world
Here the key is 'hello' and the value is 'world'. To read a key we use GET followed by a key
1GET hello
2> world
Redis has a key value store that supports storing multiple types of data as we have discussed. We can extend that with Redis Modules which are dynamic libraries designed to help us adapt our database to our data rather than the other way around.

If we are using ElasticSearch, for example, we can use the RediSearch module or the RedisGraph module for graph data storage and so on.
By running our database on our main system memory you may be asking yourself: "how does Redis protect data against data loss?"
One way is by replicating instances. This way if the master Redis instance goes down we still have a few instances left with all the data. But if all the instances go down we will lose all our data, so this is not an ideal solution.
When it comes to keeping data intact, Redis uses multiple mechanisms to persist data:
- Snapshots (RDB) produces a single-file point-in-time snapshot of our dataset that will be store on a disk
- Append Only File (AOF) logs every write operation continuously and when restarting Redis it replays the AOF to rebuild the state
- Combination of both combining both AOF and RDB which results in better data persistence
If we wish we can also disable data persistence completely, by doing this our data will exist as long as the server is running.
Build your intuition. Click the correct answer from the options.
What is not an advantage of using Redis as a database ?
Click the option that best answers the question.
- lower latency
- it is cheaper
- less complicated code
- we can store multiple types of databases into one
Creating a Redis Client with Node.js
Install node-redis
First we have to install the node-redis
package using the node package manager npm
, to do this we run the following command.
- Java: You can use the Jedis library.
- JavaScript: The
redis
npm package, as you've shown. - Python: You can use the
redis-py
library. - C++: You can use the
hiredis
library. - Go: You can use the
go-redis
library.
Each of these libraries will have its own method of installation and usage. If you need examples of how to interact with Redis in any of these languages, please let me know!
1npm install redis
or to install it globally, add -g
:
1npm install -g redis
Now we can write our application code:
1import { createClient } from 'redis';
2
3async function nodeRedisDemo () {
4 try {
5 const client = createClient();
6 await client.connect();
7
8 await client.set('mykey', 'Hello from node-redis!');
9 const myKeyValue = await client.get('mykey');
10
11 const numAdded = await client.zAdd('vehicles', [
12 {
13 score: 4,
14 value: 'car'
15 },
16 {
17 score: 2,
18 value: 'bike'
19 }
20 ]);
21
22 for await (const { score, value } of client.zScanIterator('vehicles')) {
23 console.log(`${value} -> ${score}`);
24 }
25
26 await client.quit();
27 } catch (e) {
28 console.error(e);
29 }
30}
31
32nodeRedisDemo();
Please note that each language's Redis client has its own unique API, so the code may vary in structure and functionality. Make sure to consult the respective library's documentation for complete details on usage and best practices.
Try this exercise. Click the correct answer from the options.
How does Redis protect all of our data, even the one that was not auto saved?
Click the option that best answers the question.
- with the help of Snapshot-ing
- by replicating redis instances
- with the AOF mechanism
- by retrieving it from the database
One Pager Cheat Sheet
- Redis, an in-memory multi-model database developed by
_antirez_
, has become popular for its sub-millisecond latency and is used in many startups, such as Instagram and Github. - Redis
stores
andindexes
data in multiple ways, such as Strings, Lists, Sets, Hashes, Bitmaps, HyperLogLogs, Streams, and Geospatial Indexes. - Redis can be used to
store
andcache
data in RAM for quick access and to reduce application load, with built-in authentication to restrict usage to trusted sources. - Redis improves access latency, throughput, and eases the load on the database and application by storing frequently requested data in RAM instead of having to query the database each time.
- Redis can be used as a powerful tool to both improve application performance as a
cache
as well as to serve as amulti-model
database, reducing complexity and increasing speed. - By using
Redis
to store data on the main system memory, which is much faster than retrieving it from a spinning hard disk, web applications can benefit from drastically reduced wait time, often to less than 5 seconds or even 1 second. - We can use
Redis
to interact with a key value store that supports multiple data structures and extend its abilities with Redis Modules, as well as persist data using Snapshots (RDB), Append Only File (AOF), or a combination of both. - Redis is not cheaper than other databases as it requires multiple mechanisms, such as
Snapshots (RDB)
andAppend Only File (AOF)
, for data persistence, which increases costs. - Create a
Redis Client
with Node.js usingnode-redis
and thenpm
package manager. - Redis provides automatic data persistence with its
log-structured
Append-Only File (AOF) system, allowing it to recover all data post system crashes or power failure.