DEV Community

Cover image for Getting Started with Redis and Node.JS
Johnny Simpson
Johnny Simpson

Posted on • Originally published at fjolt.com

Getting Started with Redis and Node.JS

Redis is a powerful tool for caching data and having it available in memory. Redis requires RAM to use, and every piece of data you use in Redis is loaded into the RAM. That means it can be accessed really quickly, which allows us to cache and provide data to users super fast.

Redis has lots of uses, including caching web pages and as a normal database for quicker access. It also can be used out of the box with Node.JS. In this guide, I'll show you through how to get started with Redis and Node.JS.

Using Redis with Node.JS

Before you start, make sure you install Redis first. Then start up a new Node.JS project/folder, and initiate npm using npm init in the folder:

npm init
Enter fullscreen mode Exit fullscreen mode

This will guide you through the npm setup. You can then install the npm redis package using:

npm i redis
Enter fullscreen mode Exit fullscreen mode

After that, you're free to use redis in your code in whatever way you like. The Node.JS redis package gives you an interface to store data in a Redis database, just like you would if you were using Redis from the command line. To get started with it, you can import redis into your code like so:

import { createClient } from 'redis';

const client = createClient();
client.on('error', (err) => console.log('Redis Client Error', err));
await client.connect();
Enter fullscreen mode Exit fullscreen mode

createClient is the function we use to connect to redis - after which we initiate our redis connection by using the connect() method on that function. After that, we can interact with redis using our client variable.

Let's look at some examples. To set a key, we can now use client.set:

await client.set('myKey', 'someValue');
Enter fullscreen mode Exit fullscreen mode

Since it takes a bit of time to set the data in RAM, each of these functions are async - meaning they return promises. So make sure you use them in conjunction with await or then. You can learn more about promises here.

Similarly, we can retrieve a key using get:

await client.get('myKey')
Enter fullscreen mode Exit fullscreen mode

Redis is much faster than other databases, since it's available right in memory - so expect a high level of performance using these functions. You can also use any other typical redis function, like Hashes:

await client.hSet('user', '1', 'someValue');
await client.hGetAll('user');
Enter fullscreen mode Exit fullscreen mode

You can also use sendCommand if you'd rather send a specific command of any kind to Redis. This is useful if you find something which Redis for Node.JS does not support:

await client.sendCommand(['HGETALL', 'someKey']);
Enter fullscreen mode Exit fullscreen mode

Conclusion

Redis is super fast and I used it quite extensively to build fjolt. I hope you've enjoyed this quick guide on getting started with Redis in Node.JS

Latest comments (0)