Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Redis · Function

createClient

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.

How it works

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.

Syntax

createClient(options?)

Parameters

NameTypeDescription
optionsRedisClientOptionsOptional configuration object controlling the Redis connection, including a connection url, authentication, socket settings, RESP version, and client-side caching options.

Returns

A node-redis client object. The client exposes connection lifecycle methods, status properties, events, and Redis commands.

Key points

  • Use createClient from the redis package when a Node.js or JavaScript application needs a Redis client.
  • Calling createClient() without connection options prepares a client for the local Redis server on port 6379.
  • A connection URL can specify the remote host, port, username, password, and database selection.
  • Attach an error listener so client connection or command errors are surfaced instead of becoming unhandled events.
  • Use isReady to test whether the client can currently send commands.
  • Use isOpen when the relevant state is whether the underlying client socket is open.

Common mistakes

  • Close the client with quit() after use so the Redis connection is shut down cleanly.
  • The default client targets localhost on port 6379; configure another endpoint with the url option.

Example: Create and use a node-redis client

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.

When to use it

Application caching

A Node.js service needs to read and write cached values in Redis.

Remote or secured Redis access

A program must connect to a hosted or authenticated Redis deployment.

General Redis data operations

An application needs Redis commands through an async JavaScript API.

Interview questions

QWhat does 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.

QDoes creating the client connect immediately?

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.

QHow do you configure a non-default Redis connection?

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.

  • connect
  • quit
  • isReady
  • isOpen
  • set
  • get
  • client-side caching

Source: Connect to the server | Docs. Checked 2026-10-15.