Using Redis with Node JS

Chamara Madhushan Liyanage
HackerNoon.com
3 min readApr 8, 2018

--

What is Redis

Redis is an in-memory data structure store which can be used as a database, a cache and a message broker. Redis supports different data structures such as strings, lists, sets, hashes, bitmaps and etc. . Simply Redis uses you RAM to store data which is very fast, however if you reboot your server the values are gone, unless you enable Redis persistence. Good news by default Redis enables persistence mechanism for you (you can disable or configure persistence according to your needs.)

Install Redis (Linux)

Refer: https://redis.io/download

Download, extract and compile Redis with:

The binaries that are now compiled are available in the src directory. Run Redis with:

You can interact with Redis using the built-in client:

OK, now we have successfully installed Redis on the local machine. Here you can see useful commands that we can use with Redis. They are super simple and helpful. Spend some time with those commands.

Using Redis in your NodeJS application

First you need to install the Redis client for NodeJS via npm.

Now create a file called redisDemo.js in your NodeJS project.

By default redis.createClient() will use 127.0.0.1 and port 6379. If you have a customized ip and and a port use

Now, we want to listen for the connect event to see whether we successfully connected to the redis-server. We can check for a successful connection like this.

Likewise, we want to check if we failed to connect to the redis-server. Well we can listen for the error event for that.

This might trigger when you forget to start the redis-server before application is run. So make sure to run the redis server before testing this code.

Note: you can start, stop the redis server using following commands.

Let’s see how our code looks like now.

Now, Let’s see how to set some simple value under a key in redis. You can use set() and get() methods for that.

in client.set() we first give the key and then the value. Remember Redis is a key-value store. Redis will create a key named ‘my test key’ and assign the value ‘my test value’ for that key.

You see that I’ve used that redis.print on set() method. well it prints “Reply: OK” to the console saying that redis saved the value. you can omit that argument if you want.

Now in get() method we simply retrieve the value we just saved by specifying the exact key name. Then it will print the saved valued in the console.

Let’s see the complete code now.

The output will look like this

Conclusion

Redis is very powerful in-memory data-store that we can use in our applications. It’s very simple to save and get data without much overhead. refer https://www.npmjs.com/package/redis for more use cases and refer https://redis.io/commands for more redis commands.

--

--