go to all blogs

Hello everyone!! Hope you are doing great.

In this short tutorial, we will see how to set up your own Redis server on Windows machine and use it with Node.js.

Let's get going ☕

Redis

Redis Image

What is Redis?

As per the official documentation,

Redis is an open-source (BSD licensed), in-memory data structure store, used as a database, cache and message broker.

Did you get it right? No? Okay, Let me clear it for you.

In simple terms, Redis (Remote Dictionary Server) is an open-source, in-memory, blazingly fast and an easy to use database which primarily uses key-value data storage. Redis uses superly optimized Data Structures which makes it high speed. The way redis is setup makes it highly suitable accross languages and frameworks.

Let us see some of the features of this awesome database/ server :

  1. Key-Value based Database. You can also use it to store more complex types like Arrays, HashMaps etc.

  2. Redis is an in-memory data store, which means all operations are executed on the main memory i.e RAM.

  3. Written in ANSI C language.

  4. Uses highly optimized implementation of data structures like Lists, Sets, Sorted Sets, Hashes and many more.

  5. Due to its in-memory data storage and efficient usage of optimized data structures, speed is never an excuse for Redis. It can perform close to 1.5 million operations per second with close to <1ms latency while performing operations. Isn't it super fast?

  6. Redis implementation is available across all popular languages like Python, Java, C++, PHP, Ruby, C# etc.

Now that we have covered what is Redis, let us see where can we use it in our development.

  1. Caching: Caching is a process of storing application-related data, which we want to access at high speed and at more frequent intervals compared to the other data. So this itself makes a great use-case for redis.

  2. Session storage: This is one of the most common use cases in modern apps. What it does is when you logout of the session, it adds your session ID in some so-called "black-list" and if the app encounters that particular blacklisted session ID in the page request, it can block and redirect the user.

  3. High-speed transactions: We can use Redis where there is a need of performing DB transactions at subtly high speed and the data to be handled needs to be accessed faster than the other data.

These are some of the common use-cases where you will see Redis being used. The list is countless as always.

How do we Install this on Windows? Let's see.

Install Redis on Windows 10

If you are checking for Windows .exe or .msi installer on the official website, then you will not get it. This is because there is no official support for Windows builds. Upon digging through a few Stack Overflow questions and YouTube videos, I have created this list of steps which you can follow easily.

  1. Go to this GitHub releases link to download either .msi or .exe

  2. Extract it to any folder. After extracting you will see a folder structure like this:

Redis Folder Structure

Redis Folder Structure

  1. Copy the path of "64bit" folder and add it to SYSTEM ENVIRONMENT VARIABLES at the end of PATH variable. If you don't know how to add Environment variable in windows, here is the link for the same.

  2. Open your cmd and type redis-server. You will see the Redis console with "Server Started" and other system logs like this:

Redis Server Message

Redis Server Message

Basic commands

Now, let us see some of the most common used Redis commands which you can execute directly on Redis-CLI. To start the interactive CLI, type redis-cli in the cmd. Go ahead with the following commands:

  1. SET key value

    This command is used to set a KEY-VALUE pair in the system and the key-value pair is valid for lifetime unless we delete it.

  2. GET key

    It is used to get the value of a stored KEY

  3. SETEX key TTL value

    Set the key-value pair with the expiration time as well, denoted by TTL - Time to live (in seconds) which describes how long the key is valid for.

  4. FLUSHALL

    Clear all key-value pairs from the system.

  5. DEL key

    Deletes a particular key from the system. No changes appear if the key does not exist.

Execution of the above commands is shown below:

Redis Commands Execution

Redis Commands Execution

Above listed are some of the most commonly used commands. You can have a look at the detailed list here.

Use Redis with Node.js

In this section, we will see how to use our awesome Redis server along with any Node.js script or Express.js server.

For this, go ahead and install the package redis in your project.

1npm install --save redis

After installing we have to create a Redis Client with which we will integrate our Node.js app/ script. We interact with Redis server via Node.js through this client.

Note that the API is entirely asynchronous. To get data back from the server, you will have to use a callback. Currently, Promises are not supported. Always pass the callback to all the methods.

1const redis = require('redis')
2const client = redis.createClient()
3
4//1. SET key value
5client.set('Lionel', 'Messi', (err, data) => {
6 if (err) throw new Error(err)
7
8 console.log('Setting Lionel to Messi. ', data)
9})
10
11//2. GET key
12client.get('Lionel', (err, data) => {
13 if (err) throw new Error(err)
14
15 console.log('Getting Lionel. ', data)
16})
17
18//3. SETEX key TTL value
19client.setex('Messi', 1000, 'Barcelona', (err, data) => {
20 if (err) throw new Error(err)
21
22 console.log('Setting Messi with Barcelona with TTL 1000 seconds. ', data)
23})
24
25//4. FLUSHALL
26client.flushall((err, data) => {
27 if (err) throw new Error(err)
28
29 console.log('Removing all key-value pairs.. ', data)
30})

The createClient() function is implemented with a couple of default parameters which you can pass as an object.

  • port : 6379, default PORT number for Redis Server.

  • host : 127.0.0.1, referring to localhost.

Other optional parameters you can pass to this function is given here.

Code written above describes most commonly used commands. You can always go this page for a list of all such commands.

Conclusion

In this way, we have successfully seen how to install Redis on our Windows machine. Also, we saw how it works with Node.js script. In the same way, you can integrate it with Express.js app.

That's all for this one guys. See you next time.

Happy coding.