Skip to main content

The Transactional Outbox

When posts-api publishes a post, it needs two things to happen together or not at all: the document changes to status: published, and an event announcing it goes out. If the write succeeds but the event is lost, nothing downstream ever hears about the publish. If the event goes out but the write then fails, subscribers react to something that never happened. This page builds the mechanism that makes "both or neither" the only possible outcome.

You will learn
  • Why an event can't just be published after the write commits
  • events.With: attaching an event to a write explicitly
  • outbox.OnFieldChange + events.ConditionalHooks: making a write cause an event by itself, with no if in the endpoint

The problem with "publish after you write"​

The obvious approach — write the document, then call a Pub/Sub client — has a gap: the process can die between the two calls. hyrule's answer is the outbox pattern: the event is written to an outbox collection in the same MongoDB transaction as the document write. Both are stored together, or neither is. A separate Relay reads that collection after the fact and publishes to Pub/Sub, retrying until it succeeds.

box := outbox.New(env.DB)
res.Hooks.Use(events.Hooks(box))

events.Hooks(box) is a hook bundle — the kind you met on the previous page — that flushes whatever events were attached to the write's context into box, inside the transaction.

Attaching an event by hand​

The simplest way to use it: business code decides, and attaches the event before writing.

ctx := events.With(r.Context(), outbox.Event{Topic: "posts-to-unpublish", Key: id, Body: id})
e.svc.Patch(ctx, lookup, changes, opts)

posts-api's /unpublish endpoint does exactly this. It has to: "no longer published" isn't a value a write lands on — draft is also the status of a brand new post — so there's no way to tell from the document alone whether this write is the unpublish. The endpoint knows; the document doesn't.

Letting the write decide​

/publish is the opposite case: the document does know. Any write that lands status on "published" should fire a post.published event — whether that write came through /publish, or through a plain PATCH nobody designed for this. That's outbox.Condition:

func publishCondition(topic string) outbox.Condition {
return outbox.OnFieldChange("status", []string{"published"}, func(doc map[string]any) outbox.Event {
id, _ := doc["_id"].(bson.ObjectID)
return outbox.Event{Topic: topic, Type: "post.published", Key: id.Hex(), Body: doc}
})
}

res.Hooks.Use(events.Hooks(box), events.ConditionalHooks(box, publishCondition(toPublish)))

OnFieldChange fires the event when status's value after the write is "published" and it wasn't already — a document created directly as published, or patched from draft/scheduled into published, both count; resending published when it's already published does not.

With that hook registered, /publish shrinks to the one thing it's actually responsible for — the write itself:

func (e *endpoints) publish(w http.ResponseWriter, r *http.Request) {
_, lookup, _, ok := e.target(w, r)
if !ok {
return
}
changes := rest.Doc{"status": "published", "_published": time.Now().UTC().Format(rest.DateFormat)}
if _, err := e.svc.Patch(r.Context(), lookup, changes, rest.WriteOptions{SkipConcurrency: true}); err != nil {
rest.WriteError(w, r, err)
return
}
e.respond(w, r, lookup)
}

No box.Add call anywhere in this function. The event isn't the endpoint's job anymore — it's a consequence of the write, which also means a plain PATCH /posts/{id} that happens to set status: published fires the exact same event, with nobody having to remember to wire it up twice. examples/blog-apis/services/posts-api/posts_test.go's TestPlainPatchAlsoPublishes is that claim, as a test.

Deep dive: local destinations, for events that aren't Pub/Sub

posts-api's /schedule endpoint stores an outbox event in the same Mongo transaction as the status change. After commit the relay updates a Redis hash so a scheduler can scan Redis instead of polling Mongo; Redis is not part of the originating database transaction. outbox.Dispatcher is what makes a topic name like "local:posts-schedule" resolve to a Redis write instead of a real publish. See Reference → outbox for the full mechanism.

Recap
  • outbox.New + events.Hooks store events in the same transaction as the write; a Relay publishes them afterwards.
  • events.With is for a condition only business code knows about.
  • outbox.OnFieldChange + events.ConditionalHooks is for a condition the document itself expresses — any write path that reaches it fires the event, automatically.
  • Next: Testing tests exactly this kind of hook, with no HTTP server involved.

The same event flow on Postgres​

Use outboxpostgres.Outbox with the adapter's same pool and env.PostgresTx; events.Hooks, events.ConditionalHooks and publishers stay shared. The Postgres relay supports SKIP LOCKED workers and dead-event replay, with no cross-worker Key ordering. See the parallel Mongo/Postgres integrations and the runnable comments-api example. These differences do not require a second event contract or a generic storage abstraction.