It’s very tempting to use LLMs to solve the problem at hand, and that’s ok if you need to get something done quickly. If your job demands that you use LLMs to finish tasks faster, that’s fine too.
But relying on LLMs too much can make you forget programming principles, lose the urge to learn something new, and miss that feeling of achievement when you finally finish a task you were stuck on for days.
So how do you use it to learn?
I use Redis job queues at work, and I always wanted to understand how they actually work, and why repositories like BullMQ are so popular on GitHub. I imagined it to be simple JS code calling Redis to store and fetch data. But then how does it handle concurrency, running each job exactly once, delayed jobs and more? My first thought was polling, which sounds like the obvious answer: they must have a loop with a tick that picks up the next job.
So I dug through the BullMQ code. It’s kinda difficult to navigate; being a TypeScript project, it’s full of generics and was hard to read at first. So I used Claude to get some answers. I asked, “Where and how does it save a job?” The answer: it saves to Redis by calling Lua scripts.
Lua what? Apparently Redis can run Lua scripts directly. All it needs is SCRIPT LOAD <file content> and it can basically run Lua code. But why?
So I asked another question: “What exactly happens when a new job comes in?” Now Claude started telling me something more important. BullMQ runs Redis commands like SET and LPUSH to store data in Redis, and these operations decide which job runs next.
Ok! So it uses Lua scripts to run a batch of commands together, instead of sending them one by one from a normal Redis client.
Now the bigger question: if BullMQ doesn’t poll, how does it decide which job to run? The answer wasn’t straightforward, so I looked for a simpler queue and found bee-queue. The repo is small and the code is pretty readable, and that’s where I got the nitty-gritty sorted out.
technical stuff
bee-queue uses BRPOPLPUSH, a fancy command that moves an item from one list to another. Basically it moves a job from the waiting list to the running list and returns the item it moved, which in our case is the job id. Then our code just runs that job. Since the command is prefixed with B (blocking), it waits until there is an item to move in the waiting list. BRPOPLPUSH has been deprecated in favor of BLMOVE, which does the same thing.
Here’s how it looks in redis-cli. First, add a job: store its payload, then push its id to the waiting list.
$ redis-cli SET myjobid jobdata
OK
$ redis-cli LPUSH waiting myjobid
(integer) 1
Then the worker moves an item from waiting to running, waiting up to 1 second for one to show up, and gets back the job id:
$ redis-cli BRPOPLPUSH waiting running 1
"myjobid"
In JS this looks like:
// producer
await this.redis.set("myjobid", "jobdata");
await this.redis.lpush("waiting", "myjobid");
// worker
const jobId = await this.redis.brpoplpush("waiting", "running", 1);
await runJob(jobId);
That’s the most basic way to build a simple job queue. Once you understand that Redis runs all commands on a single thread, it clicks: no matter how many JS workers you run, each job gets picked up only once. The “run once” logic is all handled by Redis, and the Node code is just a wrapper around it.
why redis commands though?
This part took me a while, so let me break it down.
Why Redis at all? A queue is just a list of jobs. You could keep it as an array in JS, but then it lives inside one process. Run 3 workers and you get 3 separate arrays; restart the app and the jobs are gone. Redis sits outside your app, so every worker on every machine sees the same lists, and the jobs survive a restart.
Why not just read and write the data from JS? Because two workers can do the same thing at the same time. Say you pick up a job like this:
// two workers run this at the exact same moment
const jobId = await redis.lindex("waiting", -1); // both read "job1"
await redis.rpop("waiting"); // one removes job1, the other removes job2
await runJob(jobId); // job1 runs twice, job2 never runs
Between your read and your write, the other worker sneaks in. This is a race condition. Redis fixes this because every single command is atomic: it runs on one thread, one command at a time, start to finish. When two workers call BRPOPLPUSH together, Redis runs one, then the other, so each gets a different job. That’s why queues lean on these specific commands instead of doing the logic in JS.
Why move the job to running instead of just popping it? If a worker pops a job and then crashes, the job is gone forever. By moving it to a running list, the job is still sitting in Redis. If a worker dies mid-job, something can look at running, see the job got stuck, and put it back in waiting. That’s how queues avoid losing jobs.
Why blocking? With polling, every worker keeps asking Redis “anything new?” every few milliseconds, even when nothing is there. A blocking command just waits, and Redis wakes the worker up the moment a job lands. No wasted calls, and no delay.
So why Lua? Single commands are atomic, but adding a job needs more than one command. Look at the example above: SET then LPUSH. Sent separately, if the app crashes between the two you’re left with half a job, and if I had flipped them, a worker could pick up the job before its data even exists. And a real queue does even more: generate a new id, store the data, push the id to the list. A Lua script runs all of it as one unit, so nothing else runs in between, and it’s a single call to Redis instead of three.
-- add a job, all in one go
local id = redis.call("INCR", "job:id") -- new unique id
redis.call("HSET", "job:" .. id, "data", ARGV[1]) -- store the payload
redis.call("LPUSH", "waiting", id) -- now workers can see it
return id
You run it with EVAL <script> 0 <payload>, or load it once with SCRIPT LOAD and call it by its hash with EVALSHA, which is what BullMQ does.
What about delayed jobs? This is where a different data type comes in: the sorted set. Every item in it has a score, and Redis keeps them sorted by that score. Use the time the job should run as the score:
$ redis-cli ZADD delayed 1764662400000 myjobid
(integer) 1
Then something checks for jobs that are due with ZRANGEBYSCORE delayed 0 <now> and moves them into waiting. And yes, that move is also a Lua script, otherwise two workers could move the same job twice.
So each Redis data type has a job in the queue:
- lists (
LPUSH,BLMOVE):waitingandrunning, in order - hashes (
HSET,HGET): the job data, one hash per job - sorted sets (
ZADD,ZRANGEBYSCORE): delayed and priority jobs, ordered by time or priority - sets (
SADD): finished and failed job ids - counters (
INCR): unique job ids
Along the way, I got to learn Redis too. Earlier all I knew was SET and GET. Now I understand the data types it handles, like lists, sets, hashes and more, and their commands like HSET, HGET, LPUSH, RPUSH and LREM. I didn’t get the prefixes before; they just stand for the data type (L for list, H for hash, and so on).
Now back to BullMQ. It doesn’t use BRPOPLPUSH; it uses a complex locking mechanism with tokens and has a ton of features. They built a finite state machine to handle jobs, priority jobs and delayed jobs. But under the hood it’s all Redis commands, using different data structures.
what did i do with all this?
While experimenting and reading this code, I wanted to write my own version of bee-queue using BLMOVE and ioredis instead of node-redis. I also wanted a barebones job queue that just does the job, without the extra features I won’t need. BullMQ is reliable and should be the obvious choice; I just wanted to build one for the sake of learning.
The code is heavily inspired by bee-queue, though imo mine is a bit more readable. Read it here: myq. Get it? my queue = myq ;)