You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
excerpt: 'Client is a Redis client to interact with a Redis serveror cluster.'
3
+
excerpt: 'Client is a Redis client to interact with a Redis server, cluster, or sentinel.'
4
4
weight: 10
5
5
weight: 10
6
6
---
7
7
8
8
# Client
9
9
10
-
`Client` is a Redis client to interact with a Redis server or cluster. It exposes a promise-based API, which users can interact with in an asynchronous manner.
10
+
`Client` is a [Redis](https://redis.io) 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.
11
11
12
-
Though the API intends to be thorough and extensive, it does not expose the whole Redis API.
13
-
Instead, the intent is to expose Redis for use cases most appropriate to k6.
12
+
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.
14
13
15
-
Note that the `Client` is configured through the [`Options`](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/redis/redis-options) object.
14
+
## Usage
16
15
17
-
## Example
16
+
### Single-node server
17
+
18
+
As shown in the above example, the simplest way to create a new `Client` instance that connects to a single Redis server is by passing a URL string.
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):
A client can also be instantiated using an [options](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/redis/redis-options) object to support more complex use cases, and for more flexibility:
36
+
37
+
{{< code >}}
38
+
39
+
```javascript
40
+
importredisfrom'k6/experimental/redis';
41
+
42
+
constclient=newredis.Client({
43
+
socket: {
44
+
host:'localhost',
45
+
port:6379,
46
+
},
47
+
username:'someusername',
48
+
password:'somepassword',
49
+
});
50
+
```
51
+
52
+
{{< /code >}}
53
+
54
+
### TLS
55
+
56
+
A TLS connection can be established in a couple of ways.
57
+
58
+
If the server has a certificate signed by a public Certificate Authority, you can use the `rediss` URL scheme:
Otherwise, you can supply your own self-signed certificate in PEM format using the [socket.tls](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/redis/redis-options#tls-configuration-options-tlsoptions) object:
71
+
72
+
{{< code >}}
73
+
74
+
```javascript
75
+
importredisfrom'k6/experimental/redis';
76
+
77
+
constclient=newredis.Client({
78
+
socket: {
79
+
host:'localhost',
80
+
port:6379,
81
+
tls: {
82
+
ca: [open('ca.crt')],
83
+
},
84
+
},
85
+
});
86
+
```
87
+
88
+
{{< /code >}}
89
+
90
+
Note that for self-signed certificates, k6's [**insecureSkipTLSVerify**](https://grafana.com/docs/k6/<K6_VERSION>/using-k6/k6-options/reference/#insecure-skip-tls-verify) option must be enabled (set to `true`).
91
+
92
+
#### TLS client authentication (mTLS)
93
+
94
+
You can also enable mTLS by setting two additional properties in the [socket.tls](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/redis/redis-options#tls-configuration-options-tlsoptions) object:
95
+
96
+
{{< code >}}
97
+
98
+
```javascript
99
+
importredisfrom'k6/experimental/redis';
100
+
101
+
constclient=newredis.Client({
102
+
socket: {
103
+
host:'localhost',
104
+
port:6379,
105
+
tls: {
106
+
ca: [open('ca.crt')],
107
+
cert:open('client.crt'), // client certificate
108
+
key:open('client.key'), // client private key
109
+
},
110
+
},
111
+
});
112
+
```
113
+
114
+
{{< /code >}}
115
+
116
+
### Cluster client
117
+
118
+
You can connect to a cluster of Redis servers by using the [cluster](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/redis/redis-options#redis-cluster-options-clusteroptions) configuration property, and passing 2 or more node URLs:
Or the same as above, but passing [socket](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/redis/redis-options#socket-connection-options-socketoptions) objects to the nodes array instead of URLs:
140
+
141
+
{{< code >}}
142
+
143
+
```javascript
144
+
importredisfrom'k6/experimental/redis';
145
+
146
+
constclient=newredis.Client({
147
+
cluster: {
148
+
nodes: [
149
+
{
150
+
socket: {
151
+
host:'host1',
152
+
port:6379,
153
+
},
154
+
},
155
+
{
156
+
socket: {
157
+
host:'host2',
158
+
port:6379,
159
+
},
160
+
},
161
+
],
162
+
},
163
+
});
164
+
```
165
+
166
+
{{< /code >}}
167
+
168
+
### Sentinel client
169
+
170
+
A [Redis Sentinel](https://redis.io/docs/management/sentinel/) provides high availability features, as an alternative to a Redis cluster.
171
+
172
+
You can connect to a sentinel instance by setting additional [options](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/redis/redis-options) in the object passed to the `Client` constructor:
173
+
174
+
{{< code >}}
175
+
176
+
```javascript
177
+
importredisfrom'k6/experimental/redis';
178
+
179
+
constclient=newredis.Client({
180
+
username:'someusername',
181
+
password:'somepassword',
182
+
socket: {
183
+
host:'localhost',
184
+
port:6379,
185
+
},
186
+
// Sentinel options
187
+
masterName:'masterhost',
188
+
sentinelUsername:'sentineluser',
189
+
sentinelPassword:'sentinelpass',
190
+
});
191
+
```
192
+
193
+
{{< /code >}}
194
+
195
+
## Real world example
18
196
19
197
{{< code >}}
20
198
@@ -42,15 +220,8 @@ export const options = {
42
220
},
43
221
};
44
222
45
-
// Get the redis instance(s) address and password from the environment
46
-
constredis_addrs=__ENV.REDIS_ADDRS||'';
47
-
constredis_password=__ENV.REDIS_PASSWORD||'';
48
-
49
223
// Instantiate a new redis client
50
-
constredisClient=newredis.Client({
51
-
addrs:redis_addrs.split(',') ||newArray('localhost:6379'), // in the form of 'host:port', separated by commas
|[`Client.set(key, value, expiration)`](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/redis/client/client-set)|**[SET](https://redis.io/commands/set)**| Set `key` to hold `value`, with a time to live equal to `expiration`. |
119
292
|[`Client.get(key)`](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/redis/client/client-get)|**[GET](https://redis.io/commands/get)**| Get the value of `key`. |
120
293
|[`Client.getSet(key, value)`](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/redis/client/client-getset)|**[GETSET](https://redis.io/commands/getset)**| Atomically sets `key` to `value` and returns the old value stored at `key`. |
@@ -131,10 +304,10 @@ export function handleSummary(data) {
131
304
|[`Client.ttl(key)`](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/redis/client/client-ttl)|**[TTL](https://redis.io/commands/ttl)**| Returns the remaining time to live of a key that has a timeout. |
132
305
|[`Client.persist(key)`](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/redis/client/client-persist)|**[PERSIST](https://redis.io/commands/persist)**| Removes the existing timeout on key. |
|[`Client.lpush(key, values)`](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/redis/client/client-lpush)|**[LPSUH](https://redis.io/commands/lpush)**| Inserts all the specified values at the head of the list stored at `key`. |
139
312
|[`Client.rpush(key, values)`](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/redis/client/client-rpush)|**[RPUSH](https://redis.io/commands/rpush)**| Inserts all the specified values at the tail of the list stored at `key`. |
140
313
|[`Client.lpop(key)`](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/redis/client/client-lpop)|**[LPOP](https://redis.io/commands/lpop)**| Removes and returns the first element of the list stored at `key`. |
@@ -145,10 +318,10 @@ export function handleSummary(data) {
145
318
|[`Client.lrem(key, count, value)`](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/redis/client/client-lrem)|**[LREM](https://redis.io/commands/lrem)**| Removes the first `count` occurrences of `value` from the list stored at `key`. |
146
319
|[`Client.llen(key)`](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/redis/client/client-llen)|**[LLEN](https://redis.io/commands/llen)**| Returns the length of the list stored at `key`. |
|[`Client.hset(key, field, value)`](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/redis/client/client-hset)|**[HSET](https://redis.io/commands/hset)**| Sets the specified field in the hash stored at `key` to `value`. |
153
326
|[`Client.hsetnx(key, field, value)`](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/redis/client/client-hsetnx)|**[HSETNX](https://redis.io/commands/hsetnx)**| Sets the specified field in the hash stored at `key` to `value`, only if `field` does not yet exist. |
154
327
|[`Client.hget(key, field)`](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/redis/client/client-hget)|**[HGET](https://redis.io/commands/hget)**| Returns the value associated with `field` in the hash stored at `key`. |
@@ -159,19 +332,19 @@ export function handleSummary(data) {
159
332
|[`Client.hlen(key)`](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/redis/client/client-hlen)|**[HLEN](https://redis.io/commands/hlen)**| Returns the number of fields in the hash stored at `key`. |
160
333
|[`Client.hincrby(key, field, increment)`](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/redis/client/client-hincrby)|**[HINCRBY](https://redis.io/commands/hincrby)**| Increments the integer value of `field` in the hash stored at `key` by `increment`. |
|[`Client.sadd(key, members)`](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/redis/client/client-sadd)|**[SADD](https://redis.io/commands/sadd)**| Adds the specified members to the set stored at `key`. |
167
340
|[`Client.srem(key, members)`](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/redis/client/client-srem)|**[SREM](https://redis.io/commands/srem)**| Removes the specified members from the set stored at `key`. |
168
341
|[`Client.sismember(key, member)`](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/redis/client/client-sismember)|**[SISMEMBER](https://redis.io/commands/sismember)**| Returns if member is a member of the set stored at `key`. |
169
342
|[`Client.smembers(key)`](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/redis/client/client-smembers)|**[SMEMBERS](https://redis.io/commands/smembers)**| Returns all the members of the set values stored at `keys`. |
170
343
|[`Client.srandmember(key)`](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/redis/client/client-srandmember)|**[SRANDMEMBER](https://redis.io/commands/srandmember)**| Returns a random element from the set value stored at `key`. |
171
344
|[`Client.spop(key)`](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/redis/client/client-spop)|**[SPOP](https://redis.io/commands/spop)**| Removes and returns a random element from the set value stored at `key`. |
|[`Client.sendCommand(command, args)`](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/redis/client/client-sendcommand)| Send a command to the Redis server. |
0 commit comments