Redis client for nodejs application
I was given a task at work last year to integrate the redis cache for one of our microservices. Here's a little bit about the micro service that I'm working on: it's a worker that listens to pubsub events, processes those events, and sends the processed data to a vendor.
In the middle of processing the event, there are several API calls into other internal services that need an access token. In order to speed up the process we need a redis cache to save the same request to our service to generate an authentication token, since the token is used multiple times in the code, it would be best to cache the access token.
After doing some research, I found two nodejs libraries for connecting with redis.
At first, I decided to use the node redis library. There have been some incidents after we run negative scenario testing, in which we shut down the redis server. We expect that the service should function normally after that. Unfortunately, the service has also gone down.
I'm trying to implement exception handling for the node-redis library, but it's not as easy as I expected. In order to save time, I use the ioredis library, which I find very easy to use and the problem solve. My implementation can be seen here.
My favorite part of ioredis is that it's created using typescript, making it a good match for the service I'm working on, which is using nestjs and typescript.
Here are some comparisons for two redis libraries for your reference.
- Created specifically for nodejs
- Doesn't support promise
- 1 to 1 mapping of the Redis commands
- It's will stop nodejs process when the client can't connect the redis server
- You can handle the error so the client doesn't stop your app but it's hard
- Used by bigger company like alibaba
- Support promise, ES6 and typescript friendly
- Doesn't stoping your app when the client can't connect to redis server
- So that's way you should use ioredis.
I recommend using
ioredis since it is simpler to work with.