r/SoftwareEngineering • u/RaphaS9 • Mar 06 '25
The Outbox Pattern is doing a queue in DB
I've been wondering about using an external queue saas (such as gcp pubsub) in my project to hold webhooks that need to be dispatched.
But I need to guarantee that every event will be sent and have a log of it in DB.
So, I've come across the Dual Write problem and it's possible solution, the Outbox Pattern.
I've always listened people say that you should not do queues in DB, that polling is bad, that latency might skyrocket with time, that you might have BLOAT issues (in case of postgres).
But in those scenarios that you need to guarantee delivery with the Outbox Pattern you are literally doing a queue in db and making your job two times harder.
What are your thoughts on this?
4
u/pomariii Mar 06 '25
The Outbox Pattern is totally valid here. While DB queues aren't ideal, sometimes you gotta prioritize data consistency over perfect architecture. I've used it in production where we absolutely needed transaction guarantees + audit logs.
Quick tip: Use a separate schema/table for the outbox and set up regular cleanup jobs. Also, implement batch processing for the consumer to reduce DB load.
The performance hit is usually worth the guaranteed delivery + ability to replay failed events.
2
u/m1k3st4rr Mar 06 '25
What is your request QPS? I've used this pattern many times in large scale setups, but using a sharded DB for really high QPS.
Postgres SELECT FOR UPDATE ... SKIP LOCKED is your friend while dequeueing here.
If your request payloads are large, store them somewhere else and just the ID in your queue.
You can also immediately trigger processing at enqueue time, so you don't need an aggressive poll interval (which then only handles transient failures)
2
u/jvans Mar 10 '25
Transactional outbox is fine/necessarily with requirements like yours.
Also worth considering event sourcing where you write to an event stream as the source of truth and your database write and other processing logic are based on that stream. Not always appropriate but it's an option
7
u/RangePsychological41 Mar 06 '25
If you’re cool with Kafka instead then Debezium solves this very nicely. It publishes DB transactions to Kafka and you practically don’t have to do anything except have a config file. Worth having a look at that.
4
u/gkbrk Mar 06 '25
For 99% of cases a queue in the DB is just fine.
If you have so much utilization that you grow out of a DB queue, you'll probably have enough money to hire a few people just to work on your queue stuff.
1
u/Xilis Mar 06 '25
It doesn't need to be pooling, you can use the WAL stream in different ways to avoid most of the bloat/latency (especially using pg_logical_emit_message() or equivalent)
1
u/goldmanthisis Mar 11 '25
If your database is Postgres, we’ve been building Sequin (https://github.com/sequinstream/sequin) to solve this exact problem. Works with GCP Pub/Sub, RabbitMQ, Kafka - or some handy HTTP sinks.