Redis get wiped out when cleaning a lot of jobs at once.
Description
When trying to clea queues with a lot of tasks (>500k) using queue.clean(0, "completed"); it sometimes leads to wiping out the whole Redis instance. The tasks for the other queues are wiped as well. Not sure if it's a bug or I'm using it the wrong way.
Minimal, Working Test code to reproduce the issue.
Generate a lot of tasks (>500k) to some queue and then try to clean them up using queue.clean(0, "completed");
Bull version
- 3.12.1
that is quite difficult to believe, I would need a test case that reproduce this.
import Queue from "bull";
let queue1 = new Queue("test-queue-1", {
redis: { host: "localhost", port: 7005 },
});
let queue2 = new Queue("test-queue-2", {
redis: { host: "localhost", port: 7005 },
});
async function main() {
for (let k = 0; k < 500000; k++) {
await queue1.add({ test: "data" });
}
await queue2.add({ test: "data" });
console.log(await queue1.getWaitingCount());
console.log(await queue2.getWaitingCount());
await queue1.clean(0, "wait");
console.log(await queue1.getWaitingCount());
console.log(await queue2.getWaitingCount());
}
main();
I ran this code locally and actually got OOM while cleaning the first queue. After running the script again only to show the number of tasks in each queue, both had 0 tasks. For the redis instance I used the local docker container:
docker run -p "7005:6379" --rm redis
I think you need to correct that code, all "add" calls return a promise and you must handle it, if you ignore it we do not really know when you got the OOM.
Sorry, you are right. Just corrected it and I'm checking if I get similar results.
I saw you edited the code, but you missed this one:
queue2.add({ test: "data" });
regarding the results, what are you getting and what do you expect?
I already had a few situations when I tried to clean up "completed" tasks using this command and ended up wiping out all the other data for other queues as well. I would expect it to clean only the queue and tasks that I specify. In my scenarios it would be cleaning up the "completed" jobs. Is this an expected way to use this method?
ok, I will look into it.
Hello @manast, Is there any updates regarding this? I am facing the same issue; I have a queue with ~7 million completed jobs, tried queue.clean() without a limit and ended up with all data getting wiped out from redis affected all other queues. I have tried iterating over and passing a limit eg: 5000 jobs but after a while it resulted in the same behaviour as before.
Thanks in advance!
@IAlshawabkeh is there are any piece of code that can help me reproduce the issue?
I experience the same issue today.
Called clean('completed') on a couple queues with about a million completed jobs in total and ALL redis keys were deleted, which was... significantly bad.
I haven't had a chance to try and create a reproduction, but the situation/code was really straight forward:
Queue A with a million completed jobs Queue B with 200k completed jobs
queueA.clean(0, 'completed') queueB.clean(0, 'completed')
Now ALL redis keys wiped. Screenshot of memory usage:

It is pretty weird, how can this happen? If you check the code here: https://github.com/OptimalBits/bull/blob/develop/lib/commands/cleanJobsInSet-3.lua it is first getting all the jobIds inside the set that you provide (completed for the given queue), then iterates every jobId and removes all data related to that job.
Hmm, what setting do you have on your redis instance for maxmemory-policy?
Just a theory, but say you have it configured as a cache, and when deleting that many jobs at once, since the script is accumulating all the jobIds in a table (that is returned by the script), the memory could reach a limit that makes redis remove data. Of course you should have the policy set to "noeviction" when using it with Bull.
I appreciate the quick response!
I read through all the code and yeah, don't see how its possible at the Node application layer or LUA script levels. Seems more likely an infrastructure or Redis config issue.
I think you're on to something with regard to maxmemory-policy.
Ours is volatile-lru right now. I'll look into swapping to noeviction.
volatile-lru: evict keys by trying to remove the less recently used (LRU) keys first, but only among keys that have an expire set, in order to make space for the new data added.
My understanding from that is that volatile-lru wouldn't entirely explain what we saw: our Redis has numerous non-Bull cache entries and it dropped to 0 memory usage, implying something more than just dropping a handful of stale keys with TTLs.
I'll keep looking in this direction.
Yes, volatile-lru should not remove keys that have no expire set, which in the case of Bull is only the rate-limiter keys and the lock keys. So it does not explain the behaviour you saw.
We could do an experiment, if we set up a redis instance with low memory, with just a couple of megabytes, and add jobs until getting close to memory limit and then try to clean all waiting jobs, then we can maybe reproduce the issue. Which redis version are you using?
We're using 6.0.5
I've gotta go handle some other stuff here but will come back to this!
Are there any updates on this issue? We are considering bull as our queueing system for our solution, but we are also expecting hundreds of thousands of jobs and we would like to rely on this queue.clean() method.
@baspeti as long as your redis instance is properly configured with maxmemory-policy: noeviction you should be fine.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.