Member-only story
How To Use Postgres as a Message Queue
It’s not the best tool for the job, but it might be the best for your situation
A message queue is a foundational architectural building block. Typically, it’s used for offloading workloads with high latency and needs resilience. For example, sending a receipt via email when a customer has placed an order.
Many queue systems are available: RabbitMQ, Kafka, or Amazon SQS. Using a relational database as a queue is known to be an anti-pattern. However, there are reasons why it’s beneficial to use a relational database as a queue:
- Reusing existing infrastructure: There’s a high chance that your system already has a relational database. Generally, the less varied your tech stack is, the simpler it is to operate.
- Low messages throughput: Not every system needs tens of thousands of messages to process per second.
- Transaction: Sometimes, you must modify the database and push a message atomically. Achieving this is not too complicated, but not that easy either.
This article shows a technical design for building a queueing system on top of Postgres. Based on performance testing, the design can handle up to ~660 messages (1KB each) per second on an AWS Aurora Postgres db.r5.large instance.

