Introduction
A B2B ordering platform starts as a single application. Placing an order reserves stock, creates an invoice draft, and emails the customer, all in one request handler. It works until it does not: the email provider is slow and checkout times out, an invoice bug blocks orders from being saved, and adding a new step means editing the most fragile code in the system.
Event-driven order management separates the decision from the reactions. The order service decides that an order was placed and records that fact. Other parts of the system react in their own time. The challenge is to do this without losing events or acting on orders that were never committed, which is where the transactional outbox comes in.
In this article you will learn how to model an order as an aggregate with versioned events, write those events through an outbox, relay them efficiently from Postgres, and coordinate multi-step processes such as reservation and payment with compensation when something fails.
Modelling Orders and Domain Events
Treat each order as an aggregate with a version number that increases on every change. Each change produces a domain event named in the past tense: OrderPlaced, StockReserved, PaymentCaptured, OrderShipped, and OrderCancelled. Events carry the order ID, the new version, the time and the data consumers need, such as line items or totals.
The version gives consumers a simple way to handle ordering: if an inventory service has already processed version 5 of an order, it can ignore a late-arriving version 4.
Writing Events Through the Outbox
Change the order and insert the event in the same transaction. A version check in the update prevents two concurrent changes from both succeeding.
`BEGIN;
UPDATE orders
SET status = 'placed', version = version + 1
WHERE id = $1 AND version = $2; -- optimistic concurrency
-- if 0 rows updated: someone else changed the order, abort and retry
INSERT INTO outbox (aggregate_type, aggregate_id, aggregate_version, event_type, payload)
VALUES ('order', $1, $2 + 1, 'OrderPlaced', $3);
COMMIT;`
No event exists unless the order change is committed, and no committed change exists without its event.
Relaying Events From Postgres
A relay process moves events from the outbox to consumers, usually through a message broker or a queue. The simplest relay polls for unpublished rows using FOR UPDATE SKIP LOCKED, which is enough for most B2B volumes. To reduce latency without hammering the database, add a trigger that calls pg_notify on insert, and have the relay LISTEN and poll immediately when notified, with a slower poll as a fallback.
CREATE FUNCTION notify_outbox() RETURNS trigger LANGUAGE plpgsql AS $$
BEGIN PERFORM pg_notify('outbox', NEW.id::text); RETURN NEW; END $$;
CREATE TRIGGER outbox_notify AFTER INSERT ON outbox
FOR EACH ROW EXECUTE FUNCTION notify_outbox();
For very high volumes or many consumers, change data capture from the Postgres write-ahead log; for example, with Debezium, it can read the outbox table without polling at all. Start with polling plus notify, and move to CDC when you have measured a need.
Coordinating Multi-Step Processes
Placing an order often starts a sequence: reserve stock, capture payment or check credit, then release to the warehouse. Model this as a process manager, sometimes called a saga, that listens for events and issues the next command. If a step fails, it issues compensating commands, such as releasing reserved stock when payment fails, and moves the order to a clear state like payment_failed.
Keep the process state in its own table so you can see every order's progress, find stuck processes, and restart them after a fix.
Practical Examples: Real-World Use Cases
A spare parts distributor moved invoice creation and customer emails out of the checkout request into event consumers. Checkout latency dropped and an email provider outage no longer affected order placement; emails were sent when the provider recovered.
A multi-warehouse B2B retailer uses StockReserved events from each warehouse to decide which warehouse ships which lines. When one warehouse cannot reserve, the process manager tries the next and records the decision on the order.
A SaaS ordering platform for food distributors added a new consumer that sends order data to customers' ERPs without touching the order service at all, because it simply subscribed to existing events.
Common Mistakes to Avoid
Publishing events directly to a broker inside the request without an outbox.
Events named as commands, such as ReserveStock, which blurs what has already happened.
Consumers that assume events arrive in order without checking versions.
Long-running sagas without a visible state table.
Putting entire database rows in events instead of what the data consumers need.
Best Practices: Actionable Tips
Version every aggregate and include the version in every event.
Document event schemas and evolve them additively.
Make every consumer idempotent, keyed on aggregate ID and version.
Monitor outbox lag and consumer lag separately.
Keep outbox rows for a retention period so you can replay to new consumers.
Conclusion
Event-driven order management on Postgres lets you add and change the steps around an order without making checkout fragile. Version your order aggregates, write events in the same transaction as the change through an outbox, relay them with polling and notifications, and coordinate multi-step processes with explicit state and compensation. The takeaway: let the order service record facts, and let every other part of the system react to them safely.
Build Event-Driven Order Systems With Parallaxis
Parallaxis builds order management and ERP solutions for B2B companies, with event-driven architectures on Postgres that keep inventory, billing, and fulfillment in sync. If your order flow has become hard to change, visit parallaxis.in to explore our ERP solutions or talk with our engineers.
Top comments (0)