"redis" is the "whole in one" package that includes all the other packages. If you only need a subset of the commands,
you can install the individual packages. See the list below.
The above code connects to localhost on port 6379. To connect to a different host or port, use a connection string in
the format redis[s]://[[username][:password]@][host][:port][/db-number]:
You can also use discrete parameters, UNIX sockets, and even TLS to connect. Details can be found in
the client configuration guide.
To check if the the client is connected and ready to send commands, use client.isReady which returns a boolean.
client.isOpen is also available. This returns true when the client's underlying socket is open, and false when it
isn't (for example when the client is still connecting or reconnecting after a network error).
Redis Commands
There is built-in support for all of the out-of-the-box Redis commands. They are exposed
using the raw Redis command names (HSET, HGETALL, etc.) and a friendlier camel-cased version (hSet, hGetAll,
etc.):
You can also watch keys by calling
.watch(). Your transaction will abort if any of the watched keys change.
Blocking Commands
In v4, RedisClient had the ability to create a pool of connections using an "Isolation Pool" on top of the "main"
connection. However, there was no way to use the pool without a "main" connection:
for await (const key of client.scanIterator()) { // use the key! await client.get(key);}
This works with HSCAN, SSCAN, and ZSCAN too:
for await (const { field, value } of client.hScanIterator("hash")) {}for await (const member of client.sScanIterator("set")) {}for await (const { score, value } of client.zScanIterator("sorted-set")) {}
You can override the default options by providing a configuration object:
client.scanIterator({ TYPE: "string", // `SCAN` only MATCH: "patter*", COUNT: 100,});
Disconnecting
The QUIT command has been deprecated in Redis 7.2 and should now also be considered deprecated in Node-Redis. Instead
of sending a QUIT command to the server, the client can simply close the network connection.
client.QUIT/quit() is replaced by client.close(). and, to avoid confusion, client.disconnect() has been renamed to
client.destroy().
client.destroy();
Client Side Caching
Node Redis v5 adds support for Client Side Caching, which enables clients to cache query results locally. The Redis server will notify the client when cached results are no longer valid.
// Enable client side caching with RESP3const client = createClient({ RESP: 3, clientSideCache: { ttl: 0, // Time-to-live (0 = no expiration) maxEntries: 0, // Maximum entries (0 = unlimited) evictPolicy: "LRU" // Eviction policy: "LRU" or "FIFO" }});
Of course, if you don't do something with your Promises you're certain to
get unhandled Promise exceptions. To take
advantage of auto-pipelining and handle your Promises, use Promise.all().
:warning: You MUST listen to error events. If a client doesn't have at least one error listener registered and
an error occurs, that error will be thrown and the Node.js process will exit. See the > EventEmitter docs for more details.
The client will not emit any other events beyond those listed above.
Supported Redis versions
Node Redis is supported with the following versions of Redis:
Version
Supported
8.0.z
:heavy_check_mark:
7.4.z
:heavy_check_mark:
7.2.z
:heavy_check_mark:
< 7.2
:x:
Node Redis should work with older versions of Redis, but it is not fully tested and we cannot offer support.