Transactional outbox
Local baseline: Node.js 22.16.0, ECMAScript modules, synthetic data. Run each complete example independently as node main.mjs in a disposable directory. Static ByteMind blocks do not imply live Node execution. SQLite is experimental in this baseline. Third-party integrations below are staged candidates, not installed or validated dependencies.
Persist a committed event and reject a rolled-back event
import {DatabaseSync} from 'node:sqlite';import {mkdtempSync,rmSync} from 'node:fs';import {tmpdir} from 'node:os';import {join} from 'node:path';const dir=mkdtempSync(join(tmpdir(),'bm-outbox-'));let db;try{ const path=join(dir,'lab.db');db=new DatabaseSync(path); db.exec('CREATE TABLE orders(id TEXT PRIMARY KEY NOT NULL); CREATE TABLE outbox(id TEXT PRIMARY KEY NOT NULL,order_id TEXT NOT NULL,sent INTEGER DEFAULT 0)'); function order(id,event,fail){db.exec('BEGIN');try{ db.prepare('INSERT INTO orders VALUES(?)').run(id); db.prepare('INSERT INTO outbox(id,order_id) VALUES(?,?)').run(event,id); if(fail)throw new Error('simulated failure');db.exec('COMMIT'); }catch(e){db.exec('ROLLBACK');}} order('o1','e1',false);order('o2','e2',true); db.close();db=new DatabaseSync(path); console.log(JSON.stringify(db.prepare('SELECT id FROM orders ORDER BY id').all())); console.log(JSON.stringify(db.prepare('SELECT id FROM outbox WHERE sent=0 ORDER BY id').all()));}finally{if(db?.isOpen)db.close();rmSync(dir,{recursive:true,force:true});}Expected output:
[{"id":"o1"}][{"id":"e1"}]Only the committed order and its outbox record survive reopening. The temporary database is removed.
How it works
Use a stable event ID, aggregate ID, sequence or version where ordering matters, event type and payload. The relay selects pending events with a bounded batch and an ownership mechanism, publishes and records successful delivery. A crash between publication and marking sent may produce duplicate messages; consumers therefore need idempotent handling. Never mark a record sent before the required broker confirmation. Keep retention, retry and poison-event policies. The first fixture uses a temporary on-disk database to test committed records surviving a close/reopen; the second models duplicate delivery, not broker acknowledgment.
Model relay retry after publication
const event={id:'e1',order:'o1'};const broker=[];let sent=false;function relay(crash){if(sent)return;broker.push(event.id);if(!crash)sent=true;}relay(true);relay(false);relay(false);const unique=new Set(broker);console.log(JSON.stringify(broker));console.log(unique.size,sent);Expected output:
["e1","e1"]1 trueA repeated publish is possible when the sent marker is absent. The Set is only a conceptual consumer model.
Common mistakes
Do not write the outbox in an unrelated transaction. Do not assume one outbox row means only one transport delivery. Do not delete pending events without an explicit recovery policy.
Practice and next steps
Draw a timeline for crashes before commit, after commit before publish, and after publish before sent marking. Specify which records remain and which duplicate event a consumer should tolerate.
Further reading: https://microservices.io/patterns/data/transactional-outbox.html. Related staged reference: node-transactional-outbox-lookup (route not verified).
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Transactional outbox — knowledge check
Apply the lesson to a fresh scenario and identify the failure boundary.
Which writes belong in one local transaction?
When can the relay publish a duplicate?
What must a consumer do with repeated outbox messages?