Client
Client
is a Redis client to interact with a Redis server, sentinel, or cluster. It exposes a promise-based API, which users can interact with in an asynchronous manner.
Though the API intends to be thorough and extensive, it does not expose the whole Redis API. Instead, the intent is to expose Redis for use cases most appropriate to k6.
Usage
Single-node server
You can create a new Client
instance that connects to a single Redis server by passing a URL string.
It must be in the format:
redis[s]://[[username][:password]@][host][:port][/db-number]
Here’s an example of a URL string that connects to a Redis server running on localhost, on the default port (6379), and using the default database (0):
import redis from 'k6/experimental/redis';
const client = new redis.Client('redis://localhost:6379');
A client can also be instantiated using an options object to support more complex use cases, and for more flexibility:
import redis from 'k6/experimental/redis';
const client = new redis.Client({
socket: {
host: 'localhost',
port: 6379,
},
username: 'someusername',
password: 'somepassword',
});
TLS
You can configure a TLS connection in a couple of ways.
If the server has a certificate signed by a public Certificate Authority, you can use the rediss
URL scheme:
import redis from 'k6/experimental/redis';
const client = new redis.Client('rediss://example.com');
Otherwise, you can supply your own self-signed certificate in PEM format using the socket.tls object:
import redis from 'k6/experimental/redis';
const client = new redis.Client({
socket: {
host: 'localhost',
port: 6379,
tls: {
ca: [open('ca.crt')],
},
},
});
Note that for self-signed certificates, k6’s insecureSkipTLSVerify option must be enabled (set to true
).
TLS client authentication (mTLS)
You can also enable mTLS by setting two additional properties in the socket.tls object:
import redis from 'k6/experimental/redis';
const client = new redis.Client({
socket: {
host: 'localhost',
port: 6379,
tls: {
ca: [open('ca.crt')],
cert: open('client.crt'), // client certificate
key: open('client.key'), // client private key
},
},
});
Cluster client
You can connect to a cluster of Redis servers by using the cluster configuration property, and passing 2 or more node URLs:
import redis from 'k6/experimental/redis';
const client = new redis.Client({
cluster: {
// Cluster options
maxRedirects: 3,
readOnly: true,
routeByLatency: true,
routeRandomly: true,
nodes: ['redis://host1:6379', 'redis://host2:6379'],
},
});
Or the same as above, but passing socket objects to the nodes array instead of URLs:
import redis from 'k6/experimental/redis';
const client = new redis.Client({
cluster: {
nodes: [
{
socket: {
host: 'host1',
port: 6379,
},
},
{
socket: {
host: 'host2',
port: 6379,
},
},
],
},
});
Sentinel client
A Redis Sentinel provides high availability features, as an alternative to a Redis cluster.
You can connect to a sentinel instance by setting additional options in the object passed to the Client
constructor:
import redis from 'k6/experimental/redis';
const client = new redis.Client({
username: 'someusername',
password: 'somepassword',
socket: {
host: 'localhost',
port: 6379,
},
// Sentinel options
masterName: 'masterhost',
sentinelUsername: 'sentineluser',
sentinelPassword: 'sentinelpass',
});