BullMQ
Search…
Reusing Redis Connections
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.

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 you are 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 to do some custom connection logic, you may still need to keep a list of all the connections you created so you can manually close them later when the queue shuts down, if you need a graceful shutdown for your process
  • do not set a keyPrefix on the connection you create, use bull's built-in prefix feature if you need a key prefix
1
const { REDIS_URL } = process.env;
2
3
const Redis = require("ioredis");
4
const client = new Redis(REDIS_URL);
5
const subscriber = new Redis(REDIS_URL);
6
7
const opts = {
8
// redisOpts here will contain at least a property of
9
// connectionName which will identify the queue based on its name
10
createClient: function (type, redisOpts) {
11
switch (type) {
12
case "client":
13
return client;
14
case "subscriber":
15
return subscriber;
16
case "bclient":
17
return new Redis(REDIS_URL, redisOpts);
18
default:
19
throw new Error("Unexpected connection type: ", type);
20
}
21
},
22
};
23
24
const queueFoo = new Queue("foobar", opts);
25
const queueQux = new Queue("quxbaz", opts);
26
Copied!
Copy link
Contents