How to Use Redis with Node.js
One of the most popular libraries for working with Redis server in a Node.js application is node-redis
, which can be found at https://github.com/NodeRedis/node-redis.
To install the library in your project, run the following command:
1 | npm install redis |
Tip: If your project is brand new and does not have a
package.json
file, make sure to runnpm init -y
first.
Connecting to the Redis Instance
Once the library is installed, require it in your project using:
1 | const redis = require('redis') |
or
1 | import redis from 'redis' |
After you have the redis
object, create a new client using:
1 | const client = redis.createClient({ |
and connect to the Redis server using (inside an async function):
1 | await client.connect() |
Once you have the client, you can perform all the operations that Redis provides.
To close the connection, simply call:
1 | client.quit() |
Storing and Retrieving Key-Value Pairs
To store a key-value pair in Redis, use the set()
method:
1 | client.set("<key>", "<value>") |
For example:
1 | client.set("name", "Flavio") |
If you run KEYS *
in redis-cli
on a clean Redis server, you will see the two keys appearing:
To retrieve the value stored in a key, use the get()
method:
1 | const value = await client.get("name") |
To delete a key-value pair, use the del()
method:
1 | client.del("names") |
Working with Lists
In Redis, you can work with lists using the following commands provided by the client
object:
lPush
rPush
lTrim
lRange
To create a list, use the lPush()
method:
1 | client.lPush('names', 'Flavio') |
To push a new item to the bottom of the list, use the rPush()
method:
1 | client.rPush('names', 'Roger') |
To add an item at the top of the list, use the lPush()
method:
1 | client.lPush('names', 'Syd') |
To list all the items in a list, use the lRange()
method:
1 | const result = await client.lRange('names', 0, -1) |
To drop items from a list, use the rPop()
method:
1 | client.rPop('names') |
To delete a list, use the del()
method:
1 | client.del('names') |
Working with Sets
In Redis, you can work with sets using the following commands provided by the client
object:
sAdd
sPop
sMembers
.
To create a set, use the sAdd()
method:
1 | client.sAdd('names', 'Flavio') |
To add more items to the set, use the sAdd()
method:
1 | client.sAdd('names', 'Roger') |
You can add multiple items at once using the sAdd()
method:
1 | client.sAdd('names', 'Roger', 'Syd') |
You can also pass an array to add multiple items:
1 | const names = ['Flavio', 'Roger', 'Syd'] |
To list all the items in a set, use the sMembers()
method:
1 | const names = await client.sMembers('names') |
To drop a random item from a set, use the sPop()
method:
1 | client.sPop('names') |
You can add a second parameter to drop multiple random items:
1 | client.sPop('names', 3) |
To delete a set, use the del()
method:
1 | client.del('names') |
Working with Hashes
In Redis, you can work with hashes using the following commands provided by the client
object:
hSet
hGetAll
hSet
hIncrBy
To create a hash, use the hSet()
method:
1 | client.hSet('person:1', 'name', 'Flavio', 'age', 37) |
To get all the properties of a user, use the hGetAll()
method:
1 | const items = client.hGetAll('person:1') |
To update a hash property, use the hSet()
method:
1 | client.hSet('person:1', 'age', 38) |
To increment a value stored in a hash, use the hIncrBy()
method:
1 | client.hIncrBy('person:1', 'age', 1) |
To delete a hash, use the del()
method:
1 | client.del('person:1') |
Subscriptions
Subscriptions are an amazing feature of Redis that allow us to do really fancy things in Node.js.
A publisher sends a message on a channel and multiple subscribers receive it.
To subscribe to a channel, use the subscribe()
method:
1 | await subscriber.subscribe('dogs', (message) => { |
To publish a message on a channel, use the client.publish('<channel>', '<message>')
method:
1 | client.publish('dogs', 'Roger') |
Please note that you cannot publish and subscribe using the same client
instance.
To do so in the same app, create 2 clients:
1 | const subscriber = redis.createClient({ ... }) |
tags: [“Redis”, “Node.js”, “programming”, “database”, “JavaScript”]