...

/

Using an Outbox for Messages

Using an Outbox for Messages

Explore the implementation of the transactional outbox pattern, which involved setting up local processes to publish messages stored in an outbox table.

To implement the Transactional Outbox pattern, we will be splitting the existing publishing action into two parts. The first part will consist of saving the outgoing message into the database, and the second part will be implemented as a new processor that will receive or check for records that are written into the database so that it can publish them to where they need to go. The first part of the Transactional Outbox pattern is shown in the below figure. The transaction that we are creating for each request will be used so that all changes from whatever work we have done, and messages, are saved atomically:

Press + to interact
Saving messages into an outbox table
Saving messages into an outbox table

Outbox table schema

Starting the same way with the outbox as we did with the inbox, let’s take a look at the table schema:

CREATE TABLE depot.outbox(
id text NOT NULL,
name text NOT NULL,
subject text NOT NULL,
data bytea NOT NULL,
published_at timestamptz,
PRIMARY KEY (id)
);
CREATE INDEX depot_unpublished_idx
ON depot.outbox (published_at)
WHERE published_at IS NULL;
Table schema for depot.outbox

This table is very similar to the depot.inbox table, with only a couple of differences:

  • The received_at column is renamed published_at and it also allows null values.

  • We add an index to the published_at column to make finding unpublished records easier.

This table could also be updated to include more advanced columns, such as the aggregate information, which could be used for ordering or partitioning.

Outbox middleware

A middleware is ...