Application caching
A Node.js service needs to read and write cached values in Redis.
Redis · Function
createClient creates a node-redis client configured for Redis, defaulting to localhost on port 6379. Supply options such as a connection URL for another server, then connect explicitly before using commands.
createClient constructs a node-redis client from optional connection settings. The returned client is not connected automatically: call connect() before issuing commands, monitor its error event, and call quit() when finished.
createClient(options?)| Name | Type | Description |
|---|---|---|
options | RedisClientOptions | Optional configuration object controlling the Redis connection, including a connection url, authentication, socket settings, RESP version, and client-side caching options. |
A node-redis client object. The client exposes connection lifecycle methods, status properties, events, and Redis commands.
createClient from the redis package when a Node.js or JavaScript application needs a Redis client.createClient() without connection options prepares a client for the local Redis server on port 6379.isReady to test whether the client can currently send commands.isOpen when the relevant state is whether the underlying client socket is open.quit() after use so the Redis connection is shut down cleanly.url option.import { createClient } from 'redis';
const client = createClient();
client.on('error', (error) => console.error('Redis Client Error', error));
await client.connect();
await client.set('key', 'value');
console.log(await client.get('key'));
await client.quit();Creates a local Redis client, connects explicitly, performs a basic write and read, then closes the connection.
A Node.js service needs to read and write cached values in Redis.
A program must connect to a hosted or authenticated Redis deployment.
An application needs Redis commands through an async JavaScript API.
createClient() return and where does it connect by default?It creates a node-redis client for Node.js. With no options, the documented example connects to Redis on localhost at port 6379 after connect() is called.
Follow-up: How would you target a secured remote instance? Pass a url such as redis://username:password@host:port, or configure connection options including TLS.
The client must be connected explicitly with await client.connect(). Commands should be issued after that connection is established.
Follow-up: How can readiness be checked? Use client.isReady for whether it is ready to send commands; client.isOpen reports whether the underlying socket is open.
Pass an options object, commonly { url: 'redis://...' }, to select a host, port, credentials, or database through the connection URL. The documentation also shows socket settings such as TLS configuration.
Follow-up: How would you handle failures in production? Register an error listener and configure options such as socket.connectTimeout or disableOfflineQueue when appropriate.
connectquitisReadyisOpensetgetclient-side caching