← build log
reliabilitypattern 01Reliability & contracts

The transactional outbox: making "commit" and "publish" one thing

post-service writes a post and announces it in the same breath — or not at all.

The problem

post-service does two things when a post is created: it writes the row to Postgres, and it tells the rest of the system about it by publishing post.created to RabbitMQ. Those are two separate operations against two separate systems, and nothing forces them to succeed or fail together. If the process crashes after the Postgres commit but before the publish call returns, the post exists and no downstream service — fanout-service, ingestion-service, graph-service — ever finds out. It’s a silent failure: nothing errors, nothing retries, the post is just invisible everywhere except the one table it landed in.

Approaches considered

Dual write, best effort. Write to Postgres, then publish. Simplest possible code, and exactly the version with the crash window described above.

Publish first, write second. Flip the order so the event goes out before the row is committed. This trades one failure mode for a worse one — now a consumer can process post.created for a post that doesn’t exist yet, or never will if the Postgres write then fails.

Two-phase commit across Postgres and RabbitMQ. Technically closes the gap, but XA transactions across a database and a message broker are slow, and RabbitMQ’s 2PC support is thin enough that nobody actually runs this in production.

Transactional outbox. Write the post and a row describing the event to publish, in the same Postgres transaction. A separate relay reads unpublished outbox rows and pushes them to RabbitMQ.

Trade-offs

The outbox adds a table, a relay process, and a small delay between commit and publish — the event goes out on the relay’s next poll, not synchronously. It also means every writer that needs this guarantee has to implement the same relay pattern, which is repeated infrastructure, not free.

The decision

Transactional outbox, relayed by a poller that marks rows published only after RabbitMQ acknowledges.

Why

It’s the only option in the list that makes “the post exists” and “the event will eventually be published” the same fact, guaranteed by one Postgres transaction instead of two independent calls hoping to agree. A relay crash mid-batch just means the row is still there — unpublished — next time the relay starts. There’s no window where the two states can permanently disagree.

Implementation

// post-service — creating a post writes the row and its outbox
// entry in one transaction, so they can never disagree.
async function createPost(db: DrizzleDb, input: CreatePostInput) {
  return db.transaction(async (tx) => {
    const [post] = await tx
      .insert(posts)
      .values({ authorId: input.authorId, body: input.body })
      .returning();

    await tx.insert(outbox).values({
      aggregateId: post.id,
      eventType: "post.created",
      payload: JSON.stringify({ postId: post.id, authorId: post.authorId }),
      publishedAt: null,
    });

    return post;
  });
}

// A separate relay polls unpublished rows and only marks them
// sent after RabbitMQ confirms the publish.
async function relayOutbox(db: DrizzleDb, channel: Channel) {
  const pending = await db
    .select()
    .from(outbox)
    .where(isNull(outbox.publishedAt))
    .limit(100);

  for (const row of pending) {
    await channel.publish("domain-events", row.eventType, Buffer.from(row.payload));
    await db.update(outbox).set({ publishedAt: new Date() }).where(eq(outbox.id, row.id));
  }
}