To connect to Redis from an application, we need a Redis client library for the language that we're coding in. Redis clients perform the following functions:
- Manage the connections between our application and the Redis server.
- Handle network communications to the Redis server using Redis' wire protocol.
- Provide a language specific API that we use in our application.
Here's a complete Node.js script that uses ioredis to perform the SET and GET commands that we previously tried in redis-cli:
ioredis wraps each Redis command in a function that can either accept a callback or return a Promise. Here, I'm using async/await to wait for each command to be executed on the Redis server before moving on to the next.
Running this code displays the value that's now stored in Redis:
The following additional resources can help you understand how to access Redis from a Node.js application:
- ioredis: Home page for the ioredis client.
- node_redis: Home page for the node_redis client.
- Redis clients by programming language: A large list of Redis clients at redis.io.
In this video, I take a look at how to get up and running with the ioredis client: