Skip to main content
A standard queue requires 3 connections to the Redis server. In some situations you might want to re-use connections—for example on Heroku where the connection count is restricted. You can do this with the createClient option in the Queue constructor.

Important Notes

bclient connections cannot be re-used, so you should return a new connection each time this is called.
  • Client and subscriber connections can be shared and will not be closed when the queue is closed
  • When shutting down the process, first close the queues, then the shared connections (if they are shared)
  • If you are not sharing connections but still using createClient for custom connection logic, you may need to keep a list of all connections to manually close them later for graceful shutdown
  • Do not set a keyPrefix on the connection you create—use Bull’s built-in prefix feature if you need a key prefix

Implementation

const { REDIS_URL } = process.env;

const Redis = require('ioredis');
let client;
let subscriber;

const opts = {
  // redisOpts here will contain at least a property of connectionName which will identify the queue based on its name
  createClient: function (type, redisOpts) {
    switch (type) {
      case 'client':
        if (!client) {
          client = new Redis(REDIS_URL, redisOpts);
        }
        return client;
      case 'subscriber':
        if (!subscriber) {
          subscriber = new Redis(REDIS_URL, redisOpts);
        }
        return subscriber;
      case 'bclient':
        return new Redis(REDIS_URL, redisOpts);
      default:
        throw new Error('Unexpected connection type: ' + type);
    }
  }
}

const queueFoo = new Queue('foobar', opts);
const queueQux = new Queue('quxbaz', opts);

Connection Types

The main Redis client used for most queue operations. Can be reused across multiple queues.
Used for subscribing to Redis pub/sub channels for event notifications. Can be reused across multiple queues.
The blocking client used for blocking operations. Cannot be reused—must return a new connection each time.

Connection Count Comparison

Without Reusing

2 queues = 6 connections(3 connections per queue)

With Reusing

2 queues = 4 connections(1 shared client + 1 shared subscriber + 2 bclient)

Graceful Shutdown

// Shutdown sequence
await queueFoo.close();
await queueQux.close();

// Now close shared connections
await client.quit();
await subscriber.quit();
This pattern is especially useful when working with platforms that have connection limits, such as Heroku or when using Redis services with connection-based pricing.

Build docs developers (and LLMs) love