My first task at my first job was not building a feature. The system was already running, receiving notifications from outside through webhooks, handled directly by a handler in the Laravel app. While it was quiet there was no problem at all.
As soon as traffic picked up, BOOM, the system was overwhelmed and some of the requests got dropped. Requests arrived faster than they could be processed, and whatever was not processed just vanished. Nothing stored it for another try later.
Key takeaways
- A queue makes work finish fast; a queue does not make work finish correctly.\n- Updates that arrive out of order let the old value win, and a wrong warehouse count lets goods get bought too soon.\n- Retries are a normal state, not a sign the system is broken, so what you must ensure is not "no duplicates" but "a duplicate changes nothing".\n- What decides the matter is who owns the decision: your application, or the database.
My first decision, expensive enough
I decided to build the pub/sub myself from scratch, with RabbitMQ and pika. The reasoning made sense at the time, and probably everyone in my position would give the same one: control. I wanted to know exactly what happened between a message arriving and the work finishing.
What I got was work that never finished. Queues, reconnect, ack, retry, ordering, all of it had to be written by hand, and every part not yet written was a part that did not work. I burned out on my own, and the system never got as far as a deploy.
If you are in that position right now, here is one thing worth believing without getting burned first: what you are building is not "a queue system". What you are building is the entire list of problems other people already solved, and you will meet them one at a time, in production, alone.
What actually got used
I learned Celery and RabbitMQ, then put them in front of FastAPI. Everything got easier there: work goes to a queue, a worker does it, and if it fails, it gets retried. What had taken weeks turned into days, with far less code.
And right there I was wrong once more: I thought the problem was solved. For a while it was.
First symptom: stock that fell a few steps behind
Then reports came in about stock that did not match. The shape was this: two updates arrive for the same item, stock 2 and then stock 0. The final result should be 0.
What happened: 2 workers processed the updates, the one saying 0 went first and finished, then the update saying 2 arrived and overwrote the number. Neither update was wrong; only the order was. The result? The item had zero stock but could still be bought.
This was not a disaster yet. The number was late, and a moment later it was right again.
Second symptom: stock at 0 that never updated, and goods bought too soon
The disaster was this one: stock was already 0, but the number never got updated, and several items got bought before anyone could stop it.
That is when it clicked that both symptoms shared one root, and the root was not the queue. A queue makes work finish fast; a queue does not make work finish correctly. My application was writing a value, "stock = 4", and that value is right or wrong depending on when it arrives and when it finishes. As long as arrival order is the only thing deciding, the system is correct by accident, not by design.
There was a second shape I ran into too, in another part of the system: two workers touched the same row at the same moment, and the one writing later was not the one with the freshest data. The symptom was identical, a stored number that was not the right number, and so was the cause: the number was computed in the application, not in the database.
And Celery adds one property that makes all of it worse: if a task fails, it gets retried. Retry is a feature, not a bug. It only becomes a problem when the work is not safe to run twice.
What I changed
Two changes, and both share one idea: if an event has a time, do not let the older one win.
1. Stock updates carry the time of their event. Every update carries the timestamp of its event, and only updates newer than what is already stored get applied. Older ones are rejected. So arrival order no longer decides the final result; the time of the event does.
2. The mechanism is made idempotent. After that, Celery retries stopped making me nervous: doing the same work again changes nothing. This is the part people skip most, because retry sounds like "if it fails, try again", when it actually means "this work will very likely run more than once, and that has to be safe".
3. Calls to the storefront use retry, and the receiver is idempotent. The storefront is a separate backend, so the direction is push: we call them. Those calls fail often, and the answer is retry, which is only safe if the receiving side can accept the same call twice without changing anything. Luckily it already worked that way there.
The result, and what I carry to this day
The system is still running today, two years on, and has been through plenty of updates.
On scale, these numbers are quoted from memory, not from a dashboard: around 24 queues running, an average of 8 workers per queue, some with just 1 for notifications, some with up to 24. The earlier claim was tens of thousands of tasks per hour; counting everything, as far as memory serves, it can reach millions of tasks per hour.
Three things carried out of that incident:
- The right tool for the right problem. The most expensive decision in this story was not the stock bug. The most expensive decision was deciding to build pub/sub myself. Now, before building anything from scratch, the first step is always to find out who has already finished building it.
- Retries are a normal state, not a sign the system is broken. In a system with queues, the same work will be done more than once. That is not a statement about probability; it is a certainty waiting for its moment.
- What you must ensure is not "no duplicates", but "a duplicate changes nothing". The first is impossible to guarantee. The second needs only one decision, and that decision is about who decides a piece of work has already been applied: your application, or the database.
Three ways the same event reaches you
If I tidy up the lessons above, they form three corners. The warehouse stock was only one of them:
- It arrives more than once. The gateway sends it again because the answer never got back. This is what I assumed was the only problem until the next shape hit me, and the lab is here, complete with measured numbers for every fix.
- It arrives from two paths. A webhook that is late, plus the morning reconciliation job that already closed the day: two keys, one fact, and neither path knows the other exists. This corner is also a lab now.
- It arrives out of order. A later update carries older data, and the older one wins. This is the corner I hit myself with warehouse stock.
All three share one root: the decision "has this event already been applied or not" is taken based on message arrival, inside the application. Until that moves somewhere able to decide correctly, a database with keys and a time anchor owned by the fact itself, you will meet the fourth corner, the fifth, and so on.
The two labs that already exist are repos you can run on your own laptop, and both have a finish line checked by a machine, not by feelings.