连接
为了开始使用 Queue,连接到 Redis 实例是必要的。 BullMQ uses the node module ioredis, and the options you pass to BullMQ are just passed to the constructor of ioredis. If you do not provide any options, it will default to port 6379 and localhost.
Every class will consume at least one Redis connection, but it is also possible to reuse connections in some situations. For example, the Queue and Worker classes can accept an existing ioredis instance, and by that reusing that connection, however QueueScheduler and QueueEvents cannot do that because they require blocking connections to Redis, which makes it impossible to reuse them.
Some examples:
import { Queue, Worker } from "bullmq";
// Create a new connection in every instance
const myQueue = new Queue("myqueue", {
connection: {
host: "myredis.taskforce.run",
port: 32856,
},
});
const myWorker = new Worker("myworker", async (job) => {}, {
connection: {
host: "myredis.taskforce.run",
port: 32856,
},
});
import { Queue, Worker } from "bullmq";
import IORedis from "ioredis";
const connection = new IORedis();
// Reuse the ioredis instance
const myQueue = new Queue("myqueue", { connection });
const myWorker = new Worker("myworker", async (job) => {}, { connection });
Note that in the second example, even though the ioredis instance is being reused, the worker will create a duplicated connection that it needs internally to make blocking connections. Please read on the ioredis documentation on how to properly create an instance of IORedis.
If you can afford many connections, by all means just use them. Redis connections have quite low overhead, so you should not need to care about reusing connections unless your service provider is imposing you hard limitations.
{% hint style="danger" %} Make sure that your redis instance has the setting
maxmemory-policy=noeviction
in order to avoid automatic removal of keys which would cause unexpected errors in BullMQ {% endhint %}