paint-brush
Redis and Node.JS: Learn the Basicsby@smpnjn
2,995 reads
2,995 reads

Redis and Node.JS: Learn the Basics

by Johnny SimpsonJanuary 30th, 2023
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Redis is a powerful piece of technology that lets us store data in memory. Let's look at how to use it with Node.JS
featured image - Redis and Node.JS: Learn the Basics
Johnny Simpson HackerNoon profile picture

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 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


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


npm i redis


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();


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');


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')


Redis is much faster than other databases since it's available right in the 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');


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']);

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