<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Microservices on LoomLoops</title>
    <link>https://loomloops.com/tags/microservices/</link>
    <description>Rafi Loom&#39;s blog. Anything I find interesting.</description>
    <language>en-us</language>
    <lastBuildDate>Tue, 15 Sep 2026 07:55:00 +0000</lastBuildDate>
    <atom:link href="https://loomloops.com/tags/microservices/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>The Transactional Outbox Pattern, Explained Simply</title>
      <link>https://loomloops.com/post/transactional-outbox-pattern/</link>
      <pubDate>Tue, 15 Sep 2026 07:55:00 +0000</pubDate>
      <dc:creator>Rafi Loom</dc:creator>
      <guid>https://loomloops.com/post/transactional-outbox-pattern/</guid>
      <description>&lt;p&gt;Your order service saves a new order and then tells the rest of the company&#xA;about it by publishing an &lt;code&gt;order_created&lt;/code&gt; event to Kafka (or RabbitMQ, or&#xA;SNS). Billing listens. Shipping listens. Email listens.&lt;/p&gt;&#xA;&lt;p&gt;That is two writes to two different systems. Sooner or later one of them&#xA;works and the other doesn&amp;rsquo;t. This is called the &lt;strong&gt;dual write problem&lt;/strong&gt;, and&#xA;the transactional outbox is the standard fix. It needs one extra table and&#xA;one small background process.&lt;/p&gt;&#xA;&lt;p&gt;&lt;strong&gt;Short version:&lt;/strong&gt;&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;Don&amp;rsquo;t publish the event directly. Write it to an &lt;code&gt;outbox&lt;/code&gt; table in the same&#xA;database transaction as the order.&lt;/li&gt;&#xA;&lt;li&gt;A separate process reads the outbox and publishes the events.&lt;/li&gt;&#xA;&lt;li&gt;Events are delivered at least once, so consumers must handle duplicates.&#xA;In return, no event is ever lost, and no event is sent for data that was&#xA;never saved.&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h2 id=&#34;the-dual-write-problem&#34;&gt;The dual write problem&lt;/h2&gt;&#xA;&lt;p&gt;Here is the code most services start with:&lt;/p&gt;&#xA;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;save order to database&#xA;publish order_created to Kafka&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Look at what can go wrong between those two lines:&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;  &lt;a href=&#34;https://loomloops.com/images/transactional-outbox-pattern/dual-write-failures.svg&#34;&gt;&lt;img src=&#34;https://loomloops.com/images/transactional-outbox-pattern/dual-write-failures.svg&#34; alt=&#34;Two ways the dual write fails. Failure 1, lost event: the order is saved to the database, then publishing to Kafka fails because Kafka is down, so the order exists and shipping never hears about it. Failure 2, ghost event: the event is published to Kafka, then saving the order to the database fails, so billing charges for an order that does not exist.&#34; loading=&#34;lazy&#34; decoding=&#34;async&#34; width=&#34;640&#34; height=&#34;328&#34;&gt;&lt;/a&gt;&#xA;&lt;/figure&gt;&lt;p&gt;Swapping the order of the two lines only swaps which failure you get. The&#xA;process can also crash between the lines, which has the same effect as&#xA;Kafka being down.&lt;/p&gt;&#xA;&lt;h3 id=&#34;cant-i-publish-after-the-transaction-commits&#34;&gt;&amp;ldquo;Can&amp;rsquo;t I publish after the transaction commits?&amp;rdquo;&lt;/h3&gt;&#xA;&lt;p&gt;That is failure 1. The commit worked, then the publish failed or the&#xA;process was killed during a deploy. The event is gone and nothing will ever&#xA;retry it.&lt;/p&gt;&#xA;&lt;h3 id=&#34;cant-i-use-a-distributed-transaction&#34;&gt;&amp;ldquo;Can&amp;rsquo;t I use a distributed transaction?&amp;rdquo;&lt;/h3&gt;&#xA;&lt;p&gt;In theory, two-phase commit (2PC, also called XA) makes two systems commit&#xA;together. In practice most message brokers don&amp;rsquo;t support it, and the ones&#xA;that do make every write slower and harder to run. Almost nobody does this&#xA;for events.&lt;/p&gt;&#xA;&lt;h2 id=&#34;the-fix-write-the-event-to-your-own-database&#34;&gt;The fix: write the event to your own database&lt;/h2&gt;&#xA;&lt;p&gt;Your database can already save several rows atomically. So save the event as&#xA;a row, next to the order:&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;  &lt;a href=&#34;https://loomloops.com/images/transactional-outbox-pattern/outbox-flow.svg&#34;&gt;&lt;img src=&#34;https://loomloops.com/images/transactional-outbox-pattern/outbox-flow.svg&#34; alt=&#34;The service runs one database transaction that inserts into the orders table and the outbox table, so both rows or neither are saved. A relay process reads the outbox table, sends each event to Kafka and marks it sent. Kafka delivers the events to consumers.&#34; loading=&#34;lazy&#34; decoding=&#34;async&#34; width=&#34;640&#34; height=&#34;352&#34;&gt;&lt;/a&gt;&#xA;&lt;/figure&gt;&lt;p&gt;The order and the event now live or die together. If the transaction rolls&#xA;back, there is no event. If it commits, the event is safely stored, and the&#xA;relay will send it, even if Kafka is down for an hour.&lt;/p&gt;&#xA;&lt;h2 id=&#34;the-outbox-table&#34;&gt;The outbox table&lt;/h2&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#282a36;-moz-tab-size:2;-o-tab-size:2;tab-size:2;&#34;&gt;&lt;code class=&#34;language-sql&#34; data-lang=&#34;sql&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ff79c6&#34;&gt;CREATE&lt;/span&gt; &lt;span style=&#34;color:#ff79c6&#34;&gt;TABLE&lt;/span&gt; outbox (&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  id             bigserial   &lt;span style=&#34;color:#ff79c6&#34;&gt;PRIMARY&lt;/span&gt; &lt;span style=&#34;color:#ff79c6&#34;&gt;KEY&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  aggregate_type &lt;span style=&#34;color:#8be9fd;font-style:italic&#34;&gt;text&lt;/span&gt;        &lt;span style=&#34;color:#ff79c6&#34;&gt;NOT&lt;/span&gt; &lt;span style=&#34;color:#ff79c6&#34;&gt;NULL&lt;/span&gt;,   &lt;span style=&#34;color:#6272a4&#34;&gt;-- &amp;#39;order&amp;#39;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#6272a4&#34;&gt;&lt;/span&gt;  aggregate_id   &lt;span style=&#34;color:#8be9fd;font-style:italic&#34;&gt;text&lt;/span&gt;        &lt;span style=&#34;color:#ff79c6&#34;&gt;NOT&lt;/span&gt; &lt;span style=&#34;color:#ff79c6&#34;&gt;NULL&lt;/span&gt;,   &lt;span style=&#34;color:#6272a4&#34;&gt;-- &amp;#39;881&amp;#39;, also the Kafka key&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#6272a4&#34;&gt;&lt;/span&gt;  event_type     &lt;span style=&#34;color:#8be9fd;font-style:italic&#34;&gt;text&lt;/span&gt;        &lt;span style=&#34;color:#ff79c6&#34;&gt;NOT&lt;/span&gt; &lt;span style=&#34;color:#ff79c6&#34;&gt;NULL&lt;/span&gt;,   &lt;span style=&#34;color:#6272a4&#34;&gt;-- &amp;#39;order_created&amp;#39;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#6272a4&#34;&gt;&lt;/span&gt;  payload        jsonb       &lt;span style=&#34;color:#ff79c6&#34;&gt;NOT&lt;/span&gt; &lt;span style=&#34;color:#ff79c6&#34;&gt;NULL&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  created_at     timestamptz &lt;span style=&#34;color:#ff79c6&#34;&gt;NOT&lt;/span&gt; &lt;span style=&#34;color:#ff79c6&#34;&gt;NULL&lt;/span&gt; &lt;span style=&#34;color:#ff79c6&#34;&gt;DEFAULT&lt;/span&gt; now(),&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  sent_at        timestamptz&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;);&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ff79c6&#34;&gt;CREATE&lt;/span&gt; &lt;span style=&#34;color:#ff79c6&#34;&gt;INDEX&lt;/span&gt; outbox_unsent_idx &lt;span style=&#34;color:#ff79c6&#34;&gt;ON&lt;/span&gt; outbox (id) &lt;span style=&#34;color:#ff79c6&#34;&gt;WHERE&lt;/span&gt; sent_at &lt;span style=&#34;color:#ff79c6&#34;&gt;IS&lt;/span&gt; &lt;span style=&#34;color:#ff79c6&#34;&gt;NULL&lt;/span&gt;;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&amp;ldquo;Aggregate&amp;rdquo; is a fancy word for &amp;ldquo;the thing the event is about&amp;rdquo;. Here it is&#xA;the order. &lt;code&gt;aggregate_id&lt;/code&gt; will matter when we talk about ordering.&lt;/p&gt;&#xA;&lt;p&gt;Writing an event is a plain insert inside your existing transaction:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#282a36;-moz-tab-size:2;-o-tab-size:2;tab-size:2;&#34;&gt;&lt;code class=&#34;language-sql&#34; data-lang=&#34;sql&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ff79c6&#34;&gt;BEGIN&lt;/span&gt;;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ff79c6&#34;&gt;INSERT&lt;/span&gt; &lt;span style=&#34;color:#ff79c6&#34;&gt;INTO&lt;/span&gt; orders (id, customer_id, total) &lt;span style=&#34;color:#ff79c6&#34;&gt;VALUES&lt;/span&gt; (&lt;span style=&#34;color:#bd93f9&#34;&gt;881&lt;/span&gt;, &lt;span style=&#34;color:#bd93f9&#34;&gt;42&lt;/span&gt;, &lt;span style=&#34;color:#bd93f9&#34;&gt;25&lt;/span&gt;.&lt;span style=&#34;color:#bd93f9&#34;&gt;00&lt;/span&gt;);&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ff79c6&#34;&gt;INSERT&lt;/span&gt; &lt;span style=&#34;color:#ff79c6&#34;&gt;INTO&lt;/span&gt; outbox (aggregate_type, aggregate_id, event_type, payload)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ff79c6&#34;&gt;VALUES&lt;/span&gt; (&lt;span style=&#34;color:#f1fa8c&#34;&gt;&amp;#39;order&amp;#39;&lt;/span&gt;, &lt;span style=&#34;color:#f1fa8c&#34;&gt;&amp;#39;881&amp;#39;&lt;/span&gt;, &lt;span style=&#34;color:#f1fa8c&#34;&gt;&amp;#39;order_created&amp;#39;&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#f1fa8c&#34;&gt;&amp;#39;{&amp;#34;order_id&amp;#34;: 881, &amp;#34;customer_id&amp;#34;: 42, &amp;#34;total&amp;#34;: &amp;#34;25.00&amp;#34;}&amp;#39;&lt;/span&gt;);&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ff79c6&#34;&gt;COMMIT&lt;/span&gt;;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&#34;the-relay-two-ways-to-read-the-outbox&#34;&gt;The relay: two ways to read the outbox&lt;/h2&gt;&#xA;&lt;h3 id=&#34;option-1-polling&#34;&gt;Option 1: polling&lt;/h3&gt;&#xA;&lt;p&gt;A small loop asks the table for unsent events every second or so:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#282a36;-moz-tab-size:2;-o-tab-size:2;tab-size:2;&#34;&gt;&lt;code class=&#34;language-sql&#34; data-lang=&#34;sql&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ff79c6&#34;&gt;BEGIN&lt;/span&gt;;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ff79c6&#34;&gt;SELECT&lt;/span&gt; id, aggregate_id, event_type, payload&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ff79c6&#34;&gt;FROM&lt;/span&gt; outbox&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ff79c6&#34;&gt;WHERE&lt;/span&gt; sent_at &lt;span style=&#34;color:#ff79c6&#34;&gt;IS&lt;/span&gt; &lt;span style=&#34;color:#ff79c6&#34;&gt;NULL&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ff79c6&#34;&gt;ORDER&lt;/span&gt; &lt;span style=&#34;color:#ff79c6&#34;&gt;BY&lt;/span&gt; id&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ff79c6&#34;&gt;LIMIT&lt;/span&gt; &lt;span style=&#34;color:#bd93f9&#34;&gt;100&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ff79c6&#34;&gt;FOR&lt;/span&gt; &lt;span style=&#34;color:#ff79c6&#34;&gt;UPDATE&lt;/span&gt; SKIP LOCKED;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#6272a4&#34;&gt;-- publish each row to Kafka and wait for Kafka to confirm&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#6272a4&#34;&gt;&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ff79c6&#34;&gt;UPDATE&lt;/span&gt; outbox &lt;span style=&#34;color:#ff79c6&#34;&gt;SET&lt;/span&gt; sent_at &lt;span style=&#34;color:#ff79c6&#34;&gt;=&lt;/span&gt; now() &lt;span style=&#34;color:#ff79c6&#34;&gt;WHERE&lt;/span&gt; id &lt;span style=&#34;color:#ff79c6&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ff79c6&#34;&gt;ANY&lt;/span&gt;($sent_ids);&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ff79c6&#34;&gt;COMMIT&lt;/span&gt;;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;code&gt;FOR UPDATE SKIP LOCKED&lt;/code&gt; lets you run more than one relay without two of&#xA;them grabbing the same rows. It is the same trick used to build a &lt;a href=&#34;https://loomloops.com/post/postgres-job-queue-skip-locked/&#34;&gt;job queue&#xA;in Postgres&lt;/a&gt;, and that post explains&#xA;how it works.&lt;/p&gt;&#xA;&lt;p&gt;One detail that catches people out: don&amp;rsquo;t track progress with &amp;ldquo;give me rows&#xA;with &lt;code&gt;id&lt;/code&gt; greater than the last one I sent&amp;rdquo;. A &lt;code&gt;bigserial&lt;/code&gt; id is handed out&#xA;when the row is inserted, not when the transaction commits. A slow&#xA;transaction can commit id 100 after id 101 is already sent, and a &amp;ldquo;greater&#xA;than&amp;rdquo; relay will skip 100 forever. Querying &lt;code&gt;sent_at IS NULL&lt;/code&gt; avoids that.&lt;/p&gt;&#xA;&lt;h3 id=&#34;option-2-change-data-capture-cdc&#34;&gt;Option 2: change data capture (CDC)&lt;/h3&gt;&#xA;&lt;p&gt;Every database keeps a log of changes for crash recovery and replication:&#xA;the WAL in Postgres, the binlog in MySQL and MariaDB. A CDC tool such as&#xA;&lt;a href=&#34;https://debezium.io/documentation/reference/stable/transformations/outbox-event-router.html&#34;&gt;Debezium&lt;/a&gt;&#xA;reads that log and publishes each new outbox row as it is committed. Your&#xA;code doesn&amp;rsquo;t poll at all.&lt;/p&gt;&#xA;&lt;h3 id=&#34;which-one&#34;&gt;Which one?&lt;/h3&gt;&#xA;&lt;table&gt;&#xA;  &lt;thead&gt;&#xA;      &lt;tr&gt;&#xA;          &lt;th&gt;&lt;/th&gt;&#xA;          &lt;th&gt;Polling&lt;/th&gt;&#xA;          &lt;th&gt;CDC (log reading)&lt;/th&gt;&#xA;      &lt;/tr&gt;&#xA;  &lt;/thead&gt;&#xA;  &lt;tbody&gt;&#xA;      &lt;tr&gt;&#xA;          &lt;td&gt;Setup&lt;/td&gt;&#xA;          &lt;td&gt;A loop in your service&lt;/td&gt;&#xA;          &lt;td&gt;A separate tool to run and watch&lt;/td&gt;&#xA;      &lt;/tr&gt;&#xA;      &lt;tr&gt;&#xA;          &lt;td&gt;Delay&lt;/td&gt;&#xA;          &lt;td&gt;Your poll interval (~1 second)&lt;/td&gt;&#xA;          &lt;td&gt;Usually under a second&lt;/td&gt;&#xA;      &lt;/tr&gt;&#xA;      &lt;tr&gt;&#xA;          &lt;td&gt;Load on DB&lt;/td&gt;&#xA;          &lt;td&gt;One small query per poll&lt;/td&gt;&#xA;          &lt;td&gt;Reads the log, no queries&lt;/td&gt;&#xA;      &lt;/tr&gt;&#xA;      &lt;tr&gt;&#xA;          &lt;td&gt;Commit order&lt;/td&gt;&#xA;          &lt;td&gt;Needs care (see above)&lt;/td&gt;&#xA;          &lt;td&gt;Follows commit order&lt;/td&gt;&#xA;      &lt;/tr&gt;&#xA;      &lt;tr&gt;&#xA;          &lt;td&gt;Best for&lt;/td&gt;&#xA;          &lt;td&gt;Starting out, most services&lt;/td&gt;&#xA;          &lt;td&gt;High volume, many services&lt;/td&gt;&#xA;      &lt;/tr&gt;&#xA;  &lt;/tbody&gt;&#xA;&lt;/table&gt;&#xA;&lt;p&gt;Start with polling. It is boring and easy to debug. Move to CDC when you can&#xA;measure that polling delay or database load is a problem.&lt;/p&gt;&#xA;&lt;h2 id=&#34;duplicates-are-part-of-the-deal&#34;&gt;Duplicates are part of the deal&lt;/h2&gt;&#xA;&lt;p&gt;Look at the relay again. It publishes to Kafka, then marks the rows as sent.&#xA;What if it crashes between those two steps?&lt;/p&gt;&#xA;&lt;figure&gt;&#xA;  &lt;a href=&#34;https://loomloops.com/images/transactional-outbox-pattern/duplicate-timeline.svg&#34;&gt;&lt;img src=&#34;https://loomloops.com/images/transactional-outbox-pattern/duplicate-timeline.svg&#34; alt=&#34;Timeline of a relay crash between three columns: relay, Kafka and outbox row 77. The relay reads row 77 and publishes it, and Kafka stores event 77. The relay crashes before marking the row sent, so sent_at stays NULL. After the restart it reads row 77 again and publishes it again, and Kafka stores a second copy of event 77. Then it marks the row sent and sent_at becomes 10:04. The event went out twice.&#34; loading=&#34;lazy&#34; decoding=&#34;async&#34; width=&#34;640&#34; height=&#34;520&#34;&gt;&lt;/a&gt;&#xA;&lt;/figure&gt;&lt;p&gt;The event went out twice. You can&amp;rsquo;t fully prevent this, so the outbox gives&#xA;you &lt;strong&gt;at-least-once&lt;/strong&gt; delivery: every event arrives, maybe more than once.&lt;/p&gt;&#xA;&lt;p&gt;The answer is to make consumers safe to repeat. Include a unique event id in&#xA;the payload (the outbox &lt;code&gt;id&lt;/code&gt; works), and have each consumer remember what it&#xA;has handled:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#282a36;-moz-tab-size:2;-o-tab-size:2;tab-size:2;&#34;&gt;&lt;code class=&#34;language-sql&#34; data-lang=&#34;sql&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#6272a4&#34;&gt;-- in the consumer&amp;#39;s database&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#6272a4&#34;&gt;&lt;/span&gt;&lt;span style=&#34;color:#ff79c6&#34;&gt;CREATE&lt;/span&gt; &lt;span style=&#34;color:#ff79c6&#34;&gt;TABLE&lt;/span&gt; processed_events (&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  event_id     &lt;span style=&#34;color:#8be9fd;font-style:italic&#34;&gt;bigint&lt;/span&gt;      &lt;span style=&#34;color:#ff79c6&#34;&gt;PRIMARY&lt;/span&gt; &lt;span style=&#34;color:#ff79c6&#34;&gt;KEY&lt;/span&gt;,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;  processed_at timestamptz &lt;span style=&#34;color:#ff79c6&#34;&gt;NOT&lt;/span&gt; &lt;span style=&#34;color:#ff79c6&#34;&gt;NULL&lt;/span&gt; &lt;span style=&#34;color:#ff79c6&#34;&gt;DEFAULT&lt;/span&gt; now()&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;);&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ff79c6&#34;&gt;BEGIN&lt;/span&gt;;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ff79c6&#34;&gt;INSERT&lt;/span&gt; &lt;span style=&#34;color:#ff79c6&#34;&gt;INTO&lt;/span&gt; processed_events (event_id) &lt;span style=&#34;color:#ff79c6&#34;&gt;VALUES&lt;/span&gt; (&lt;span style=&#34;color:#bd93f9&#34;&gt;77&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ff79c6&#34;&gt;ON&lt;/span&gt; CONFLICT (event_id) &lt;span style=&#34;color:#ff79c6&#34;&gt;DO&lt;/span&gt; &lt;span style=&#34;color:#ff79c6&#34;&gt;NOTHING&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;RETURNING event_id;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#6272a4&#34;&gt;-- no row returned: already handled, COMMIT and stop&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#6272a4&#34;&gt;-- row returned: do the work in this same transaction&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#6272a4&#34;&gt;&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ff79c6&#34;&gt;COMMIT&lt;/span&gt;;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This is sometimes called an &amp;ldquo;inbox&amp;rdquo; table. It is the same idea as an&#xA;&lt;a href=&#34;https://loomloops.com/post/idempotency-keys-explained/&#34;&gt;idempotency key&lt;/a&gt;: save &amp;ldquo;I am handling&#xA;this&amp;rdquo; in the same transaction as the work.&lt;/p&gt;&#xA;&lt;h2 id=&#34;ordering-same-order-same-key&#34;&gt;Ordering: same order, same key&lt;/h2&gt;&#xA;&lt;p&gt;Consumers often care about order. &lt;code&gt;order_shipped&lt;/code&gt; arriving before&#xA;&lt;code&gt;order_created&lt;/code&gt; will confuse shipping.&lt;/p&gt;&#xA;&lt;p&gt;Two rules keep the order right:&lt;/p&gt;&#xA;&lt;ol&gt;&#xA;&lt;li&gt;&lt;strong&gt;Publish with &lt;code&gt;aggregate_id&lt;/code&gt; as the message key.&lt;/strong&gt; Kafka keeps messages&#xA;with the same key in one partition, in order. Events for order 881 stay&#xA;in order, while events for different orders can be handled in parallel.&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;Don&amp;rsquo;t let two relays send events for the same order at the same time.&lt;/strong&gt;&#xA;With several polling relays and &lt;code&gt;SKIP LOCKED&lt;/code&gt;, relay A might hold&#xA;&lt;code&gt;order_created&lt;/code&gt; while relay B sends &lt;code&gt;order_shipped&lt;/code&gt; for the same order.&#xA;Either run one relay, or split rows between relays by&#xA;&lt;code&gt;hash(aggregate_id)&lt;/code&gt; so each order always goes through the same relay.&lt;/li&gt;&#xA;&lt;/ol&gt;&#xA;&lt;figure&gt;&#xA;  &lt;a href=&#34;https://loomloops.com/images/transactional-outbox-pattern/ordering-by-aggregate-id.svg&#34;&gt;&lt;img src=&#34;https://loomloops.com/images/transactional-outbox-pattern/ordering-by-aggregate-id.svg&#34; alt=&#34;Three orders flow through relays to Kafka partitions, each in order. Order 881 goes through relay 1 to partition p3 as created, paid, shipped. Order 882 goes through relay 2 to partition p0 as created, cancelled. Order 883 goes through relay 1 to partition p3 as created.&#34; loading=&#34;lazy&#34; decoding=&#34;async&#34; width=&#34;640&#34; height=&#34;272&#34;&gt;&lt;/a&gt;&#xA;&lt;/figure&gt;&lt;p&gt;Order is only kept per order, not across all orders. That is almost always&#xA;what you want, and it is what lets you scale.&lt;/p&gt;&#xA;&lt;h2 id=&#34;keeping-the-outbox-small&#34;&gt;Keeping the outbox small&lt;/h2&gt;&#xA;&lt;p&gt;The outbox only needs to hold events until they are sent. Delete sent rows&#xA;on a schedule:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#282a36;-moz-tab-size:2;-o-tab-size:2;tab-size:2;&#34;&gt;&lt;code class=&#34;language-sql&#34; data-lang=&#34;sql&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ff79c6&#34;&gt;DELETE&lt;/span&gt; &lt;span style=&#34;color:#ff79c6&#34;&gt;FROM&lt;/span&gt; outbox&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ff79c6&#34;&gt;WHERE&lt;/span&gt; sent_at &lt;span style=&#34;color:#ff79c6&#34;&gt;&amp;lt;&lt;/span&gt; now() &lt;span style=&#34;color:#ff79c6&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#8be9fd;font-style:italic&#34;&gt;interval&lt;/span&gt; &lt;span style=&#34;color:#f1fa8c&#34;&gt;&amp;#39;3 days&amp;#39;&lt;/span&gt;;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Keeping a few days helps when you need to check what was sent. In Postgres,&#xA;a table that gets lots of inserts and deletes also needs autovacuum to keep&#xA;up. The same tuning tips from the &lt;a href=&#34;https://loomloops.com/post/postgres-job-queue-skip-locked/&#34;&gt;job queue&#xA;post&lt;/a&gt; apply.&lt;/p&gt;&#xA;&lt;h2 id=&#34;the-one-metric-to-watch-outbox-lag&#34;&gt;The one metric to watch: outbox lag&lt;/h2&gt;&#xA;&lt;p&gt;If the relay stops, nothing errors. Orders save fine. Events just stop&#xA;flowing, and you find out when a customer asks where their parcel is.&lt;/p&gt;&#xA;&lt;p&gt;So watch the age of the oldest unsent event:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#282a36;-moz-tab-size:2;-o-tab-size:2;tab-size:2;&#34;&gt;&lt;code class=&#34;language-sql&#34; data-lang=&#34;sql&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ff79c6&#34;&gt;SELECT&lt;/span&gt; now() &lt;span style=&#34;color:#ff79c6&#34;&gt;-&lt;/span&gt; &lt;span style=&#34;color:#ff79c6&#34;&gt;min&lt;/span&gt;(created_at) &lt;span style=&#34;color:#ff79c6&#34;&gt;AS&lt;/span&gt; outbox_lag,&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;       &lt;span style=&#34;color:#ff79c6&#34;&gt;count&lt;/span&gt;(&lt;span style=&#34;color:#ff79c6&#34;&gt;*&lt;/span&gt;)                &lt;span style=&#34;color:#ff79c6&#34;&gt;AS&lt;/span&gt; unsent&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ff79c6&#34;&gt;FROM&lt;/span&gt; outbox&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#ff79c6&#34;&gt;WHERE&lt;/span&gt; sent_at &lt;span style=&#34;color:#ff79c6&#34;&gt;IS&lt;/span&gt; &lt;span style=&#34;color:#ff79c6&#34;&gt;NULL&lt;/span&gt;;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;figure&gt;&#xA;  &lt;a href=&#34;https://loomloops.com/images/transactional-outbox-pattern/outbox-lag-scale.svg&#34;&gt;&lt;img src=&#34;https://loomloops.com/images/transactional-outbox-pattern/outbox-lag-scale.svg&#34; alt=&#34;Outbox lag scale. Under 5 seconds: healthy. 30 seconds to a few minutes: relay slow, or broker struggling. Growing steadily: relay stopped or stuck, page someone. Unsent equals 0: fine, lag shows NULL.&#34; loading=&#34;lazy&#34; decoding=&#34;async&#34; width=&#34;640&#34; height=&#34;346&#34;&gt;&lt;/a&gt;&#xA;&lt;/figure&gt;&lt;p&gt;Alert when the lag passes a limit that fits your business, for example two&#xA;minutes.&lt;/p&gt;&#xA;&lt;h2 id=&#34;when-you-dont-need-an-outbox&#34;&gt;When you don&amp;rsquo;t need an outbox&lt;/h2&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;strong&gt;Only one system is involved.&lt;/strong&gt; If the consumer reads the same database,&#xA;a &lt;a href=&#34;https://loomloops.com/post/postgres-job-queue-skip-locked/&#34;&gt;job in a jobs table&lt;/a&gt; does the&#xA;same thing with less machinery.&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;Losing an event is fine.&lt;/strong&gt; Analytics clicks, &amp;ldquo;user viewed page&amp;rdquo; counters.&#xA;Fire and forget is simpler.&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;The event is the source of truth.&lt;/strong&gt; In event sourcing, you write only the&#xA;event, so there is no second write to keep in sync.&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h2 id=&#34;questions-people-ask&#34;&gt;Questions people ask&lt;/h2&gt;&#xA;&lt;p&gt;&lt;strong&gt;Is the outbox the same as event sourcing?&lt;/strong&gt;&#xA;No. With an outbox, your normal tables are the truth and events are&#xA;announcements about changes. With event sourcing, the events themselves are&#xA;the truth.&lt;/p&gt;&#xA;&lt;p&gt;&lt;strong&gt;Doesn&amp;rsquo;t this double my database writes?&lt;/strong&gt;&#xA;Each event adds one insert, and later one update and one delete. For most&#xA;services that is small next to the business writes. If it ever matters, CDC&#xA;removes the update, because it doesn&amp;rsquo;t need a &lt;code&gt;sent_at&lt;/code&gt; column.&lt;/p&gt;&#xA;&lt;p&gt;&lt;strong&gt;Can the relay live inside my service?&lt;/strong&gt;&#xA;Yes, and that is the easy way to start. Run it as a background loop. With&#xA;&lt;code&gt;SKIP LOCKED&lt;/code&gt;, every copy of your service can run one safely, as long as you&#xA;handle the ordering rule above.&lt;/p&gt;&#xA;&lt;p&gt;&lt;strong&gt;What about MySQL?&lt;/strong&gt;&#xA;The pattern is the same. MySQL 8.0 supports &lt;code&gt;SKIP LOCKED&lt;/code&gt; for polling, and&#xA;Debezium can read the MySQL binlog for CDC.&lt;/p&gt;&#xA;&lt;h2 id=&#34;cheat-sheet&#34;&gt;Cheat sheet&lt;/h2&gt;&#xA;&lt;figure&gt;&#xA;  &lt;a href=&#34;https://loomloops.com/images/transactional-outbox-pattern/cheat-sheet.svg&#34;&gt;&lt;img src=&#34;https://loomloops.com/images/transactional-outbox-pattern/cheat-sheet.svg&#34; alt=&#34;Transactional outbox in one page. Problem: a database write and a broker publish cannot commit together. Fix: insert the event into the outbox table in the same transaction. Relay: poll WHERE sent_at IS NULL with SKIP LOCKED, or use CDC from the WAL or binlog. Never: track progress with id greater than last sent. Delivery: at least once, so consumers dedupe by event id. Ordering: message key is aggregate_id, one relay per key. Cleanup: delete rows sent more than a few days ago. Watch: outbox lag is now() minus the oldest unsent created_at.&#34; loading=&#34;lazy&#34; decoding=&#34;async&#34; width=&#34;640&#34; height=&#34;464&#34;&gt;&lt;/a&gt;&#xA;&lt;/figure&gt;&lt;h2 id=&#34;further-reading&#34;&gt;Further reading&lt;/h2&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;a href=&#34;https://microservices.io/patterns/data/transactional-outbox&#34;&gt;microservices.io: Transactional outbox&lt;/a&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;a href=&#34;https://docs.aws.amazon.com/prescriptive-guidance/latest/cloud-design-patterns/transactional-outbox.html&#34;&gt;AWS Prescriptive Guidance: Transactional outbox pattern&lt;/a&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;a href=&#34;https://debezium.io/documentation/reference/stable/transformations/outbox-event-router.html&#34;&gt;Debezium: Outbox Event Router&lt;/a&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;a href=&#34;https://loomloops.com/post/payment-reconciliation/&#34;&gt;Payment reconciliation in SQL&lt;/a&gt; on this site, for checking that what you sent matches what the other side recorded&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;</description>
    </item>
  </channel>
</rss>
