Now that we have written our first javascript program to connect to Redis and save some data to Redis. Time to move forward and understand significant bits and bytes of Redis client and different ways to connect to Redis server.
Redis clients overview
To connect our application to Redis instances, we Redis client or Redis client library that is supported by our application’s language. Redis clients have important features such as Managing Redis connection, Implementing Redis protocols, and providing language-specified API for Redis commands.
Redis clients use RESP (REdis Serialization Protocol) to communicate with the Redis server. RESP serializes different data types like integers, strings, and arrays, and then sends a Request to the Redis server in form of arrays of strings that represent the command to execute. For this request Redis server replies with command specified data type. RESP is a binary-safe Protocol which means input is treated as a raw stream of bytes and its textual aspect is ignored.
list of different Redis clients
Connecting to Redis from Node.js Application
- Connecting to Redis using Host and Port
const redis = require('redis');
const connectWithPortAndHost = (port, host) => {
const client = redis.createClient(
{
host: host,
port: port
}
);
client.connect();
client.on('connect', () => console.log('connected to redis'));
client.PING().then((res) => {
console.log('PING : ', res)
}).then(() => {
client.quit();
});
};
connectWithPortAndHost('6379', '127.0.0.1');
output:
connected to redis
PING : PONG
- Connecting to Redis with default configurations
const redis = require('redis');
const connectWithDefaultPortAndHost = () => {
const client = redis.createClient();
client.connect();
client.on('connect', () => console.log('connected to redis'));
client.PING().then((res) => {
console.log('PING : ', res)
}).then(() => {
client.quit();
});
};
connectWithDefaultPortAndHost();
output:
connected to redis
PING : PONG
- Connecting to Redis with Redis URL
const redis = require('redis');
const connectWithRedisURL = () => {
const client = redis.createClient({
url: 'redis://127.0.0.1:6379',
});
client.connect();
client.on('connect', () => console.log('connected to redis'));
client.PING().then((res) => {
console.log('PING : ', res)
}).then(() => {
client.quit();
});
};
connectWithRedisURL();
output:
connected to redis
PING : PONG
- Connecting to Redis With Password
const redis = require('redis');
const connectWithRedisUsingPassword = (host, port, password) => {
const client = redis.createClient({
host: host,
port: port,
password: password
});
client.connect();
client.on('connect', () => console.log('connected to redis'));
client.PING().then((res) => {
console.log('PING : ', res)
}).then(() => {
client.quit();
});
};
connectWithRedisUsingPassword('localhost', 6379, '123456');
output:
connected to redis
PING : PONG
node-redis library events
In the above code examples, we have used different events like connect and error. There are other different events that tell us about the state of connections.
Example:
client.on('connect' , () => console.log('connect'));
client.on('ready' , () => console.log('ready'));
client.on('reconnecting', () => console.log('reconnecting'));
client.on('error' , () => console.log('error'));
client.on('end' , () => console.log('end'));
node-redis events | connection state |
ready | connection has been made to Redis and is ready to send commands |
end | connection has been closed |
reconnecting | retrying to make the connection back |
error | some error occurred while trying to connect |
Connection Pooling in Redis
Connection Pooling is a technic in which an application creates multiple connections with the server or set of connections. Which can be used as needed, once the task is complete connection returns back to the pool.
In the case of nodeJS, it's effectively a single-threaded environment, so connection pooling does not provide performance benefits