Transactional outbox
This tutorial implements a transactional outbox using examples/outbox.sql from the pgredis source repository (Apache-2.0). A producer inserts an order row and appends a Redis Stream entry with XADD in one PostgreSQL transaction; a consumer reads from a Stream consumer group over RESP, then writes its shipment row and runs XACK in one PostgreSQL transaction.
On commit 715827aa0 (Linux arm64, PostgreSQL 18.6, 2026-10-21), the examples/outbox.sql integration suite passed all 46/46 checks across each of the 4 authentication and protocol profiles (native and redis-acl × RESP2 and RESP3), covering rollback, savepoints, binary payloads, concurrent deduplication, consumer takeover and lost COMMIT replies.
1. Install the outbox schema and consumer group
Use the pgredis_demo database and pgredis_app role from Quick start. Make sure logical database 0 does not already contain the key pgredis:outbox:orders and that schema pgredis_outbox_demo does not exist.
Connect as a superuser (postgres) and run examples/outbox.sql:
The script creates three tables (orders, events, shipments), two SECURITY INVOKER functions (place_order, apply_order), and the Stream consumer group shipping on pgredis:outbox:orders:
Grant access on the outbox schema to pgredis_app:
2. Produce an order and Stream event in one transaction
Connect with psql as pgredis_app:
Assign a stable request_id UUID to the business operation and call place_order inside a READ COMMITTED transaction:
place_order inserts into orders, calls redis.command(ARRAY['XADD', 'pgredis:outbox:orders', '*', 'request_id', p_request::text]), and stores the returned Stream entry ID in events:
- Calling
place_orderagain with the samerequest_idand identicalpayloadbytes returns the originalevent_idwithout appending a duplicate Stream entry. - A concurrent duplicate waits on the
ordersprimary key until the first transaction finishes. - Passing a different payload for the same
request_idraises SQLSTATE22023. - Rolling back the SQL transaction removes the
ordersrow, theeventsmapping and the Stream entry together.
3. Consume and acknowledge the event
In a separate terminal, connect with redis-cli and read the next unread entry for consumer group shipping:
Record the returned Stream entry ID (for example 1761955200000-0) and the request_id field. Complete the XREADGROUP call before starting the consumer’s SQL transaction. Until acknowledged, the entry stays in the group’s Pending Entries List (PEL).
In your psql session, apply the order and acknowledge the Stream entry in one SQL transaction:
apply_order checks that event_id matches events, copies the order payload into shipments (ON CONFLICT DO NOTHING), and calls XACK inside the same transaction:
- On the first execution,
apply_orderreturnstrueand requiresXACKto acknowledge1pending entry (ifXACKreturns:0,apply_orderraises an exception and rolls backshipments). - On a repeated call after commit,
apply_orderreturnsfalsewithout inserting a second shipment.
Verify the committed shipment row:
4. Recover after worker failures or lost commit replies
- Worker crash before
COMMIT: Ifworker-1stops beforeapply_ordercommits, inspect its pending entries withXREADGROUP GROUP shipping worker-1 COUNT 10 STREAMS pgredis:outbox:orders 0, or let another worker reclaim idle entries withXAUTOCLAIM/XCLAIM, then callapply_orderwith the reclaimedrequest_idandevent_id. KeepXREADGROUPandXAUTOCLAIMoutside the SQL transaction, and never send a standaloneXACKbeforeapply_order. - Lost
COMMITreply: If the network drops duringCOMMIT, the client can safely re-runplace_order(request_id, payload)orapply_order(request_id, event_id)on a new connection. The primary-key lock waits for any still-running transaction and resolves the duplicate deterministically.
