Prequel runs data transfers between databases and warehouses for B2B SaaS companies. Each transfer is a job that is put on a queue and processed by workers; the fastest jobs take at least a few seconds, a backfill can take several hours, and thousands of jobs are enqueued and dequeued every day. After the change the queue is a single table in the application's Postgres database: publishers write to it and the workers read from it.
Section: Some useful context | Near: a new queue backed by a single
Figures, as the source states them
Figure
As stated
Note
Where
Net change in lines of code from replacing RabbitMQ with the Postgres queue
RabbitMQ consumers prefetched the next job and held it behind a multi-hour transfer
prequel-2023-postgres-job-queue#f1at corpus version 26a1b3a7e3dd
Trigger
A worker picked up a message for a multi-hour transfer, having prefetched the next message along with it.
Near: a worker would pick up a message, and
How it failed
Each RabbitMQ consumer prefetched the next message when it picked up the current one, and no other consumer could then take that message. Jobs here can run for hours, so a worker busy with a multi-hour transfer kept the job it had prefetched waiting until the current one finished.
Near: each RabbitMQ consumer was prefetching the next message | Near: hold the next one effectively hostage until
Why defences did not hold
Nothing pointed at the cause: the queue showed no major backlog, the workers looked healthy and most jobs were processed normally, with only occasional stragglers. Finding the root cause took hours of investigation.
Near: our queue wasn’t showing any major backlogs | Near: After hours of investigating
Impact
A customer paged the team one evening because some of its transfers were being delayed, seemingly arbitrarily, by up to a few hours, sometimes for long enough that the system marked them as stale and cancelled them. The problem affected production customers.
Near: transfers arbitrarily delayed by up to a few | Near: This was causing issues for production customers
Fix
RabbitMQ was removed and replaced with a queue built in SQL on the team's Postgres database.
Near: We recently ripped out RabbitMQ from our system
Consumers that timed out reconnected twice, multiplying queue threads
prequel-2023-postgres-job-queue#f2at corpus version 26a1b3a7e3dd
Trigger
A consumer timing out from the queue.
Near: a consumer timing out from the queue
How it failed
A few months in, the team hit a handful of multi-threading bugs in which a consumer that timed out from the queue reconnected twice, so the number of queue threads grew exponentially, which showed up in the memory utilization graphs. The team also spent a lot of time adjusting what happens when a worker loses its connection to the queue.
Section: Issues started popping up
Fix
The team says it dealt with these bugs and moved on; the post does not say how.
Replace RabbitMQ with a queue kept in one table of the application's Postgres database
prequel-2023-postgres-job-queue#d1at corpus version 26a1b3a7e3dd
Chosen
The team built a queue on a single Postgres table that publishers write to and workers read from. Ordering comes from an ORDER BY clause in the query the workers read with, and row-level locks guarantee that a job is not picked up by more than one worker. The change took half a day to implement and test and had a net delta of -580 lines of code.
Section: Earning our stripes as SQL maxis | Near: took half a day to implement + test
What forced it
Prefetching by RabbitMQ consumers was delaying production customers' transfers by up to a few hours. The team found no way to turn prefetching off (a prefetch count of 1 lets each worker prefetch at most one message, and 0 means an unlimited number), had no immediate fix, and says waiting was not an option.
Section: Issues started popping up | Near: This was causing issues for production customers
Reasoning
The same queue functionality could be recreated in Postgres and adjusted to the team's exact requirements, in a way it judged much simpler and fully modifiable later. The team wrote it, is fluent in SQL and already had tooling for Postgres, which it says makes monitoring, debugging and changing the queue easier, most of all during an outage.
Near: recreate the same queue functionality in our Postgres | Near: we all understand this queuing system really well | Near: We already have plenty of tooling set up
What worked before
RabbitMQ, used through the team's own Go wrapper around an existing client, with each message processed by only one consumer and manual acknowledgements. The team says it worked well for a while; problems began a few months in.
Section: RabbitMQ: good enough to start | Near: We started experiencing issues and challenges a few
Outcome
Application state is no longer spread over RabbitMQ's storage and Postgres but held in the application database, which the team says makes disaster recovery easier. By the authors' own account the new queue has behaved flawlessly so far and the change drastically improved the system's reliability and resiliency.
Near: the application state is no longer spread out | Near: It’s also behaved flawlessly so far | Near: it drastically improved the reliability and resiliency