Your monolith writes an order successfully, but the email or search update that follows can fail halfway through. I would start with a transactional outbox and one database-backed worker, not a message broker, because the first problem to solve is losing work between a database commit and a side effect. You can prove that boundary locally before adding another service to operate.
A local outbox should work before you add a broker
Start with an order that changes to paid and an event that a second piece of code will use to build a read model. The order is the business record; the event records work that remains to be done. Keep both in the same database transaction, because a crash should leave either both records present or neither present.
Save the following as demo.py and run it with Python 3.12 using python demo.py. It uses SQLite so the first check needs no server, package installation, or credentials. The script is deliberately small: its projection stands in for a consumer, and its fixed IDs make rerunning the smoke test harmless.
import json, sqlite3
db = sqlite3.connect("demo.db")
db.executescript("""CREATE TABLE IF NOT EXISTS orders(id TEXT PRIMARY KEY, status TEXT);
CREATE TABLE IF NOT EXISTS outbox(id TEXT PRIMARY KEY, payload TEXT, delivered INTEGER DEFAULT 0);
CREATE TABLE IF NOT EXISTS projection(id TEXT PRIMARY KEY, status TEXT);""")
with db:
db.execute("INSERT OR IGNORE INTO orders VALUES (?, ?)", ("o-1", "paid"))
db.execute("INSERT OR IGNORE INTO outbox(id,payload) VALUES (?,?)", ("e-1", json.dumps({"order_id":"o-1","status":"paid"})))
for event_id, payload in db.execute("SELECT id,payload FROM outbox WHERE delivered=0").fetchall():
event = json.loads(payload)
with db:
db.execute("INSERT OR REPLACE INTO projection VALUES (?,?)", (event["order_id"], event["status"]))
db.execute("UPDATE outbox SET delivered=1 WHERE id=?", (event_id,))
print(db.execute("SELECT * FROM projection").fetchall())
The printed projection should contain the paid order. Delete demo.db and rerun to check the clean-start path; rerun without deleting it to check that the fixed event does not create another projection row. Neither check proves production reliability, because the consumer and the outbox share one SQLite connection in this demonstration.
Read Add event-driven architecture to a monolith without rewriting after this smoke test, because a migration approach becomes more useful once you can point to a working transaction and name the boundary you intend to move. I would not create a generic event framework yet, because one concrete event exposes missing IDs, retry behavior, and ownership rules faster than an abstraction does.
The database transaction must define what an event means
Move the example into the database your monolith already uses; for a concrete setup, assume PostgreSQL 16. Create an outbox table with an event ID as its primary key, an event type, an aggregate ID, a payload stored as jsonb, a creation timestamp, an attempt count, and a delivery state. Add an index on pending rows ordered by creation time, because the worker should find its next batch without scanning every completed event.
In the existing “mark order paid” handler, begin a transaction, update the order, insert an OrderPaid outbox row, and commit. Use the same transaction and connection for both writes; opening another connection for the event recreates the gap the outbox is meant to close. Give the event its own stable ID and include the order ID in its payload, because a retry must identify the same event while a consumer still needs to find the affected order.
Define the event as a fact about a committed state change, not as an instruction such as “send this email,” because more than one consumer may eventually react to the same fact in different ways. Keep the payload small and version its shape with a field such as schema_version, because consumers need an explicit way to distinguish formats when the producer changes. For this first event, include the order ID, new status, and event ID; fetch additional order details through an agreed API only if the consumer actually needs them, because copying an entire mutable order into every event makes later corrections harder to interpret.
Make a failure test before starting the worker. Force the order update to roll back and verify that no outbox row appears; then commit a paid order with the worker stopped and verify that its outbox row remains pending. Those tests establish the contract more directly than watching a successful request, because the risky cases occur on either side of the commit.
For the production handler, the paid transition also needs a rule for repeated requests. A conditional update that changes only an unpaid order, followed by an event insert only when that update affected a row, prevents a request retry from announcing the same transition twice. The fixed IDs and INSERT OR IGNORE in the local script are smoke-test conveniences, not substitutes for that business rule.
A worker should assume delivery can happen twice
Run the first production worker as a separate process using the same PostgreSQL database. Begin with a provisional batch of 20 rows; treat that size as a tuning choice, because a larger batch can increase throughput while holding claimed work longer. If you run multiple workers, claim pending rows with PostgreSQL FOR UPDATE SKIP LOCKED, then record a lease or claimed-until time before releasing the transaction, because a database lock disappears at commit and cannot protect the subsequent network call.
The worker should send each event to a consumer over HTTP, using a stable event ID in an Idempotency-Key header. The consumer must record that ID in a table with a unique constraint in the same transaction as its own database change, because an HTTP response can be lost after the consumer has committed. On a repeat request, it can return success without applying the change again. That is at-least-once delivery with idempotent handling, not exactly-once delivery.
After the consumer confirms success, mark the outbox row delivered. If the call times out, leave it eligible for retry after its lease expires, because a timeout does not tell you whether the consumer committed. Use an initial retry delay of 5 seconds as a value to adjust against your consumer’s recovery time; add backoff and jitter so many failed events do not retry together. After repeated failures, move the event to a reviewable failed state rather than discarding it, because a malformed payload will not become valid through more attempts.
Test the awkward boundary deliberately: make the consumer commit its projection, then terminate the worker before it records delivery. Restart the worker and confirm that the consumer’s unique event-ID constraint prevents a second change. This test matters because the local SQLite script commits the projection and delivery marker together, while two real processes cannot share that transaction.
A second process proves more than a second queue
Keep the first consumer modest: an HTTP endpoint that writes a paid-order projection is enough to test the boundary. Run it separately from the monolith and worker, because stopping one process then demonstrates whether pending work survives without preventing the order transaction from committing. Use OpenTelemetry trace context with the W3C Trace Context headers if your app already has tracing; otherwise, log the event ID, order ID, attempt count, and outcome in structured JSON, because those fields let you follow one event across processes without guessing from timestamps.
Measure event age from outbox creation to consumer confirmation and track the number of pending and failed rows. Set an initial acceptance target of 2 seconds at p95 as a threshold to revise after observing your application’s needs, not as a universal service objective. A growing pending count while the consumer is healthy points toward worker capacity or query performance; a growing failed count points toward payload or consumer errors, because those states represent different places where progress stopped.
Read Back-End Infrastructure Essentials for Scalable Systems after you have those measurements, because infrastructure choices are easier to justify with a visible queue age and failure rate. I would not add Kubernetes or a broker merely to call this setup scalable, because neither removes the need for an atomic producer write, consumer idempotency, or a way to inspect stuck events.
Database polling wins first; RabbitMQ wins when the measurements demand it
The explicit choice here is PostgreSQL outbox polling versus RabbitMQ 4.x. PostgreSQL polling wins when event volume is moderate and the team already operates PostgreSQL, because it introduces no new message service and makes the producer transaction straightforward. Its cost is repeated queries, database load, and delivery latency tied partly to the poll interval. Start with a 250 ms interval as a setting to tune, then measure event age and query load rather than assuming the shortest interval is best.
RabbitMQ wins when several independent consumers need routing, buffering, or different consumption rates, because exchanges and queues provide those behaviors without making every consumer poll the application database. Its cost is another service to deploy, monitor, secure, and recover; it also does not replace the outbox, because publishing to RabbitMQ after a database commit can still fail. If you introduce it, have an outbox publisher send events using publisher confirms, and keep consumers idempotent because publication and acknowledgment can still be retried.
Do not move just because one consumer is slow. First check whether its endpoint is failing, its database constraint is contended, or its processing time exceeds the worker’s lease, because changing transports will not fix those causes. Move when measured event age remains unacceptable after those fixes and database polling has become a real cost, not when the architecture diagram looks too simple.
Tomorrow, add one outbox row to one existing state-changing transaction and write the rollback test before starting a worker. Then stop the worker, commit a change, and inspect the pending row. That small exercise gives you a durable piece of work and a specific failure to investigate; it is a better starting point than provisioning a queue whose messages your application cannot yet produce reliably.


