Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

node-redis throws The client is closed when you send a command

ErrorClientClosedError: The client is closed

Short answer

node-redis throws ClientClosedError when the client's socket is not open. That happens when connect() was never awaited, when close() or destroy() already ran, or when reconnecting stopped. Find which one with client.isOpen and an error listener, then create and connect the client once.

  • Applies to node-redis 5, Node.js 22, Redis 7
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Log the client's state and every event right where the error appears. Do this before changing any code.

    client.on('error', (err) => console.error('redis error', err));
    client.on('end', () => console.log('redis connection ended'));
    client.on('terminated', (cause) => console.error('redis gave up', cause));
    console.log('isOpen', client.isOpen, 'isReady', client.isReady);
  2. 2Match what you see, then jump to its fix

    1. 1isOpen is false on the very first command, and no end event was loggedThe client was never connected before a command ranFix
    2. 2An end event is logged, then later requests failclose() or destroy() ran, and the client is used againFix
    3. 3A terminated event is logged before the errorReconnecting was given up by the reconnect strategyFix

What does the error look like?

  • ClientClosedError: The client is closed

Cause 1 of 3

The client was never connected before a command ran

1Check

createClient() only builds the object. Commands sent before the socket is open are rejected with this error. Look for a missing await or a command run at import time.

grep -rn "createClient\|\.connect()" --include=*.js --include=*.ts --exclude-dir=node_modules .

2Fix

Create the client in one module, register the error listener, and await connect() there. Import that module wherever Redis is used.

// redis.js
import { createClient } from 'redis';

export const client = await createClient()
  .on('error', (err) => console.error('Redis Client Error', err))
  .connect();

3Verify

A command run right after startup succeeds.

node -e "import('./redis.js').then(async ({client}) => { console.log(await client.ping()); await client.close(); })"

Confirmed by github.com

Did this fix it?

Cause 2 of 3

close() or destroy() ran, and the client is used again

1Check

Search for close(), quit() or destroy() calls in request handlers, tests or shutdown hooks that also run while the app is still serving. Calling quit() or close() on a client that is already closed throws the same error.

grep -rn "\.close()\|\.quit()\|\.destroy()" --include=*.js --include=*.ts --exclude-dir=node_modules .

2Fix

Share one long-lived client for the whole process and close it only once, on shutdown. If you must replace a client, create and connect a new one instead of reusing the old object.

process.on('SIGTERM', async () => {
  if (client.isOpen) await client.close();
  process.exit(0);
});

3Verify

Requests keep working across many calls, and only SIGTERM triggers the end event.

for i in 1 2 3 4 5; do curl -s -o /dev/null -w '%{http_code} ' http://localhost:3000/health; done

Confirmed by github.com

Did this fix it?

Cause 3 of 3

Reconnecting was given up by the reconnect strategy

1Check

When reconnectStrategy returns false or an Error, the client stops reconnecting and emits terminated, then the companion error. Look at the strategy in your client options.

grep -rn "reconnectStrategy" --include=*.js --include=*.ts --exclude-dir=node_modules .

2Fix

Return a delay so the client keeps retrying, or destroy the dead client and build a new one when it terminates. The default strategy is exponential backoff capped at 2000 ms plus jitter.

const client = createClient({
  socket: { reconnectStrategy: (retries) => Math.min(2 ** retries * 50, 2000) },
});
client.on('terminated', (cause) => {
  console.error('client will not reconnect:', cause);
  queueMicrotask(() => client.destroy());
});

3Verify

Stop Redis, start it again, and watch commands resume without a restart of the app.

sudo systemctl stop redis-server && sleep 3 && sudo systemctl start redis-server

Confirmed by github.com

Did this fix it?

How do I stop it coming back?

  • Keep one client per process in one module, connected with top-level await, and import it everywhere.
  • Always attach an error listener: without one, an error is thrown and can exit the process.
  • Close the client only in the shutdown handler, never inside a request.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Databases

node-redis throws ConnectionTimeoutError on connect

Redis Client Error ConnectionTimeoutError

node-redis raises ConnectionTimeoutError when the socket does not connect within socket.connectTimeout, which is 5000 ms by default.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.