NodeJS + Redis: Setting it Up Asynchronously
Recently, I began working on a project involving Redis, a very powerful and simple in-memory data structure store. In this article, I will discuss how I went about setting it up asynchronously.
Let’s get started! :)
First, follow these commands to gather the packages necessary to set-up a NodeJS + ExpressJS API with Redis.
npm initnpm install --save redis express body-parser
We will be using the node-redis package for our integration with the Redis server due to its simplicity, reliability, and documentation.
Next, let’s set-up the Redis server. For this I will use a standard practice, running the server in a docker container locally.
Run the following command in the project root directory where your docker-compose file should be located
docker-compose up -d
To ensure it is running:
docker ps
Now that our Redis server is running and we have the necessary packages, let’s start coding our API!
First, we set up our server with a few basic endpoints that will read and write from Redis.
The GET endpoint will take in a router param key to retrieve from Redis
GET http://localhost:3000/hello
The POST endpoint will store a JSON KV pair in Redis
POST http://localhost:3000/send
{
"key": "hello",
"value": "John Doe"
}
Now that our endpoints are set-up, let’s hook up our Redis store. Assuming that I am hooking up my Redis store for a large application, I have decided that the functions should be universally defined in a store.js file where any part of my application that needs Redis may import and use them.
Let’s take a look at what that store.js file may look like.
Per the node-redis documentation:
“Node Redis currently doesn’t natively support promises (this is coming in v4), however you can wrap the methods you want to use with promises using the built-in Node.js util.promisify
method on Node.js >= v8;”
Since I’ve wrapped my methods in promisify I may now import them and use them asynchronously, increasing readability and following best practice.
Let’s take a look at what that would look like when integrated into our server.js endpoints.
I’ve imported the Redis async functions created in store.js (get and set) and utilized them in my API calls. Hopefully, from this example, you see how easy it is to set up Redis and import its powerful functionality directly into your API.
Before I stop droning on and on…I have one more example to show if you are looking for finer control over your Redis store and even further decoupling.
Let’s look at how we can replicate promisify by instituting our own promises into a Redis function. We will replace the getList promisify function in store.js with our own JS Promise variation.
Adding Redis asynchronous support this way, rather than promisify as documented in the Redis npm package, allows for much finer control over the function. Before data is read into the Redis store we can provide custom validation, custom error handling, logging…etc, all decoupled from our application. Furthermore, we can set default values such as 0 and -1 as seen above that will carry over.
With Promisify:
getList("frameworks", 0, -1)
Custom Promises:
getList("frameworks")
I hope this helps you set up your next or existing project better with Redis! :)