<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Daniel Cruz</title>
    <description>The latest articles on DEV Community by Daniel Cruz (@daniel_cruz_aaf7dd957e107).</description>
    <link>https://dev.to/daniel_cruz_aaf7dd957e107</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4234646%2Fa548c369-7784-4c62-b8f4-eacf6396fa40.png</url>
      <title>DEV Community: Daniel Cruz</title>
      <link>https://dev.to/daniel_cruz_aaf7dd957e107</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/daniel_cruz_aaf7dd957e107"/>
    <language>en</language>
    <item>
      <title>Server-Side Ecommerce Tracking: An Architecture for Accurate KPIs</title>
      <dc:creator>Daniel Cruz</dc:creator>
      <pubDate>Fri, 27 Nov 2026 17:00:49 +0000</pubDate>
      <link>https://dev.to/daniel_cruz_aaf7dd957e107/server-side-ecommerce-tracking-an-architecture-for-accurate-kpis-3ml7</link>
      <guid>https://dev.to/daniel_cruz_aaf7dd957e107/server-side-ecommerce-tracking-an-architecture-for-accurate-kpis-3ml7</guid>
      <description>&lt;p&gt;Every Black Friday KPI is only as good as the events behind it. If your purchase data comes from a browser tag that never fired, fired twice, or reported an order that was refunded the next day, your dashboard is telling a confident story about numbers that never happened. Server-side ecommerce tracking fixes this by making your backend, not the shopper's browser, the source of truth for what was sold, refunded and attributed. This article covers how to design that system: the data model, the event pipeline, deduplication across destinations, failure handling under peak load and the trade-offs between approaches. If you want the business side, which KPIs to compute and how to read them, see our companion guide on &lt;a href="https://disruptivestudio.com/blog/black-friday-metrics-kpis" rel="noopener noreferrer"&gt;Black Friday metrics and the 12 KPIs to measure your results&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why browser-only tracking breaks on your busiest day
&lt;/h2&gt;

&lt;p&gt;A client-side tag runs in an environment you do not control. Ad blockers, privacy settings, a shopper closing the tab before the thank-you page finishes loading, a payment redirect that never returns, a slow third-party script competing with your checkout: each one silently drops or duplicates events. On a normal day the gap is noise. On the day you compare against last year, it is the difference between a good decision and a bad one.&lt;/p&gt;

&lt;p&gt;There is a second, quieter problem: the browser only knows about the moment of purchase. It does not know about the refund issued on Tuesday, the chargeback three weeks later or the order cancelled for stock. Revenue metrics built only on browser events are gross by construction, and most of the KPIs that matter after Black Friday are net.&lt;/p&gt;

&lt;h2&gt;
  
  
  Server-side ecommerce tracking architecture: the order ledger as source of truth
&lt;/h2&gt;

&lt;p&gt;The pattern that holds up is simple to describe and requires discipline to implement:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Your database records every money movement (capture, refund, chargeback) as an append-only ledger row.&lt;/li&gt;
&lt;li&gt;In the same transaction, it writes one outbox row per destination that needs to hear about it: analytics, ad platforms, your warehouse.&lt;/li&gt;
&lt;li&gt;A separate dispatcher process reads the outbox and delivers events with retries, backoff and a dead-letter state.&lt;/li&gt;
&lt;li&gt;Dashboards and KPIs read from the ledger or the warehouse, never from an ad platform's reporting.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The browser tag can still exist. It becomes a supplement for behavioral signals (product views, add-to-cart, session attribution) while the server owns the money.&lt;/p&gt;

&lt;h3&gt;
  
  
  Data model
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt;              &lt;span class="kt"&gt;UUID&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;order_number&lt;/span&gt;    &lt;span class="kt"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;UNIQUE&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;      &lt;span class="c1"&gt;-- the transaction_id you send everywhere&lt;/span&gt;
  &lt;span class="n"&gt;customer_id&lt;/span&gt;     &lt;span class="kt"&gt;UUID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;currency&lt;/span&gt;        &lt;span class="kt"&gt;CHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;gross_amount&lt;/span&gt;    &lt;span class="kt"&gt;NUMERIC&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;fx_rate_to_base&lt;/span&gt; &lt;span class="kt"&gt;NUMERIC&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;    &lt;span class="c1"&gt;-- frozen at order time&lt;/span&gt;
  &lt;span class="n"&gt;placed_at&lt;/span&gt;       &lt;span class="kt"&gt;TIMESTAMPTZ&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;      &lt;span class="c1"&gt;-- always UTC on disk&lt;/span&gt;
  &lt;span class="n"&gt;store_tz&lt;/span&gt;        &lt;span class="kt"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;             &lt;span class="c1"&gt;-- e.g. 'America/Mexico_City'&lt;/span&gt;
  &lt;span class="n"&gt;tracking_event_id&lt;/span&gt; &lt;span class="kt"&gt;UUID&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;           &lt;span class="c1"&gt;-- generated once at checkout&lt;/span&gt;
  &lt;span class="n"&gt;consent_ads&lt;/span&gt;     &lt;span class="kt"&gt;BOOLEAN&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;-- Append-only ledger: every money movement is a row, never an UPDATE&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;order_ledger&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt;            &lt;span class="kt"&gt;BIGSERIAL&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;order_id&lt;/span&gt;      &lt;span class="kt"&gt;UUID&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;REFERENCES&lt;/span&gt; &lt;span class="nf"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;source&lt;/span&gt;        &lt;span class="kt"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;               &lt;span class="c1"&gt;-- 'checkout', 'psp_webhook', 'admin'&lt;/span&gt;
  &lt;span class="n"&gt;source_ref&lt;/span&gt;    &lt;span class="kt"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;               &lt;span class="c1"&gt;-- provider event id / refund id&lt;/span&gt;
  &lt;span class="n"&gt;kind&lt;/span&gt;          &lt;span class="kt"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;               &lt;span class="c1"&gt;-- 'capture', 'refund', 'chargeback'&lt;/span&gt;
  &lt;span class="n"&gt;amount&lt;/span&gt;        &lt;span class="kt"&gt;NUMERIC&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;      &lt;span class="c1"&gt;-- negative for refunds&lt;/span&gt;
  &lt;span class="n"&gt;occurred_at&lt;/span&gt;   &lt;span class="kt"&gt;TIMESTAMPTZ&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="k"&gt;UNIQUE&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;source_ref&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                &lt;span class="c1"&gt;-- idempotency guard&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;tracking_outbox&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt;            &lt;span class="kt"&gt;BIGSERIAL&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;destination&lt;/span&gt;   &lt;span class="kt"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;               &lt;span class="c1"&gt;-- 'ga4', 'meta', 'warehouse'&lt;/span&gt;
  &lt;span class="n"&gt;dedup_key&lt;/span&gt;     &lt;span class="kt"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;payload&lt;/span&gt;       &lt;span class="kt"&gt;JSONB&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;status&lt;/span&gt;        &lt;span class="kt"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="s1"&gt;'pending'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;attempts&lt;/span&gt;      &lt;span class="kt"&gt;INT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;next_try_at&lt;/span&gt;   &lt;span class="kt"&gt;TIMESTAMPTZ&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;last_error&lt;/span&gt;    &lt;span class="kt"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="k"&gt;UNIQUE&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;destination&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dedup_key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three decisions in this schema carry most of the weight. First, &lt;strong&gt;order_number&lt;/strong&gt; is the single identifier sent to every destination, so deduplication is possible everywhere. Second, &lt;strong&gt;tracking_event_id&lt;/strong&gt; is generated once at checkout and stored, never regenerated at send time. Third, the ledger is append-only with a unique constraint on the source reference, which makes every write idempotent.&lt;/p&gt;

&lt;h2&gt;
  
  
  Deduplication: one ID, generated once, sent everywhere
&lt;/h2&gt;

&lt;p&gt;If you run browser and server tracking in parallel (the usual hybrid setup), each platform receives the same purchase twice and must collapse it. Each platform does this differently, and your design has to satisfy all of them.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Meta Conversions API.&lt;/strong&gt; Meta's documentation on &lt;a href="https://developers.facebook.com/documentation/ads-commerce/conversions-api/deduplicate-pixel-and-server-events" rel="noopener noreferrer"&gt;handling duplicate Pixel and Conversions API events&lt;/a&gt; recommends matching on two fields: the Pixel's eventID must equal the server's event_id, and the event names must match. The same page notes that deduplication only applies when the duplicate arrives within 48 hours of the first event with that event_id.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;GA4 Measurement Protocol.&lt;/strong&gt; Purchase deduplication relies on transaction_id. As &lt;a href="https://www.endframe.io/blog/ga4-measurement-protocol-server-side-conversions" rel="noopener noreferrer"&gt;EndFrame's guide to server-side GA4 conversions&lt;/a&gt; explains, there is no Meta-style event_id, so transaction_id is the only handle and it only applies to purchases. The same guide notes events should be sent within 72 hours using timestamp_micros. &lt;a href="https://www.owox.com/blog/articles/ga4-measurement-protocol" rel="noopener noreferrer"&gt;OWOX's Measurement Protocol walkthrough&lt;/a&gt; shows a complete purchase request with transaction_id, currency and items.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The practical rule: generate the ID on the server when the order is created, render it into the thank-you page for the browser tag, and reuse the stored value in every server payload. The most common bug is a server that generates a fresh UUID at send time, which makes deduplication impossible and doubles reported conversions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Writing events atomically with a transactional outbox
&lt;/h2&gt;

&lt;p&gt;The tempting implementation is to call the analytics API right after saving the order. It fails in two ways: if the API call fails, the event is lost; if the API call succeeds and your transaction rolls back, you reported a sale that does not exist. The transactional outbox removes both failure modes because the order and the intent to notify are committed together or not at all.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;BEGIN&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;order_ledger&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;source_ref&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;kind&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;occurred_at&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;order_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'checkout'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;payment_intent_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'capture'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="k"&gt;CONFLICT&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;source_ref&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;DO&lt;/span&gt; &lt;span class="k"&gt;NOTHING&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;tracking_outbox&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;destination&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dedup_key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;VALUES&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'ga4'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;       &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;order_number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;            &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;ga4_payload&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'meta'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;      &lt;span class="s1"&gt;'Purchase:'&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;event_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;meta_payload&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'warehouse'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;order_number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;            &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;row_payload&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="k"&gt;CONFLICT&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;destination&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dedup_key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;DO&lt;/span&gt; &lt;span class="k"&gt;NOTHING&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;COMMIT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that nothing in the checkout request talks to a third party. Your checkout latency on Black Friday no longer depends on an ad platform's response time.&lt;/p&gt;

&lt;h2&gt;
  
  
  Idempotent webhook ingestion
&lt;/h2&gt;

&lt;p&gt;Refunds, chargebacks and asynchronous payment confirmations usually arrive as webhooks from your payment provider. Design as if any webhook can arrive twice, late, or before the event it logically follows. The unique constraint on the ledger does the heavy lifting:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;POST /webhooks/psp
  verify_signature(request)  or return 400     # reject before touching the DB
  evt = parse(request.body)

  BEGIN
    inserted = INSERT INTO order_ledger (...)
               VALUES (..., source='psp_webhook', source_ref=evt.id, ...)
               ON CONFLICT (source, source_ref) DO NOTHING
               RETURNING id
    if inserted:
      enqueue outbox rows for this ledger entry   # same transaction
  COMMIT

  return 200        # duplicates also get 200, so the provider stops retrying
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For out-of-order delivery, avoid state machines that reject a refund because the capture has not been seen yet. With a ledger, order does not matter: rows sum correctly whenever they arrive. If you also keep a status column on the order, derive it from the ledger instead of mutating it from each webhook.&lt;/p&gt;

&lt;h2&gt;
  
  
  The dispatcher: retries, backoff and dead letters
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;loop:
  rows = SELECT * FROM tracking_outbox
         WHERE status = 'pending' AND next_try_at &amp;lt;= now()
         ORDER BY id
         LIMIT 100
         FOR UPDATE SKIP LOCKED            -- many workers, no double pickup

  for row in rows:
    result = send(row.destination, row.payload, timeout = 5s)
    if result.ok:
      mark row 'sent'
    else if result.retryable (timeout, 429, 5xx):
      row.attempts += 1
      row.next_try_at = now() + backoff(row.attempts) + random_jitter()
      if row.attempts &amp;gt; MAX or event_too_old(row, destination):
        mark row 'dead'                    -- alert, inspect, replay by hand
    else:                                  -- 4xx: bad payload, don't retry blindly
      mark row 'dead' with last_error
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A few details matter at peak traffic:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;SKIP LOCKED&lt;/strong&gt; lets you scale workers horizontally without a separate queue broker. If you already run one (SQS, RabbitMQ, Pub/Sub), the outbox can feed it through a relay instead.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Jitter&lt;/strong&gt; prevents every worker from retrying at the same instant after a destination recovers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Destination-aware expiry.&lt;/strong&gt; Given the 48-hour deduplication window on Meta and the 72-hour guidance for GA4 cited above, an event retried after those windows can create a duplicate or be ignored. Mark it dead and decide deliberately.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Separate retryable from permanent errors.&lt;/strong&gt; Retrying a malformed payload a hundred times only hides the bug.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Refunds, time zones and currency: where KPIs quietly go wrong
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Refunds as negative ledger rows
&lt;/h3&gt;

&lt;p&gt;Send refunds to destinations that support them, but compute net revenue yourself from the ledger. Ad platforms and analytics tools each have their own rules for late adjustments; your ledger has one.&lt;/p&gt;

&lt;h3&gt;
  
  
  Time zones
&lt;/h3&gt;

&lt;p&gt;Store timestamps in UTC and the store's IANA time zone alongside them. Bucket by local day only at query time. A sale at 11:30 p.m. in Mexico City on Black Friday belongs to Black Friday, even though in UTC it is already Saturday. Stores selling in several countries need to decide which calendar a given report uses, and write it down.&lt;/p&gt;

&lt;h3&gt;
  
  
  Currency
&lt;/h3&gt;

&lt;p&gt;Freeze the exchange rate on the order at the time of sale. Recomputing last year's revenue with today's rate makes year-over-year comparisons meaningless.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- Net revenue per local store day, in base currency&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;occurred_at&lt;/span&gt; &lt;span class="no"&gt;AT&lt;/span&gt; &lt;span class="kt"&gt;TIME&lt;/span&gt; &lt;span class="no"&gt;ZONE&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;store_tz&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;date&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;local_day&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nb"&gt;SUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fx_rate_to_base&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;              &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;net_revenue_base&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nb"&gt;COUNT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;DISTINCT&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;FILTER&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;kind&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'capture'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;order_ledger&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;order_id&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;occurred_at&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;from_utc&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;occurred_at&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;to_utc&lt;/span&gt;
&lt;span class="k"&gt;GROUP&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Trade-offs: which approach fits your stack
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Browser-only tags.&lt;/strong&gt; Fastest to set up, zero backend work. Undercounts, cannot see refunds, and depends on third-party scripts in your checkout. Acceptable for a small store; weak for year-over-year decisions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Managed server-side tagging (for example, a server container).&lt;/strong&gt; Moves collection to infrastructure you control without writing a pipeline. Still event-driven from the browser, so the ledger problems (refunds, cancellations) remain unless you feed it from the backend.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Backend outbox plus hybrid browser tag.&lt;/strong&gt; The most accurate and the most work: schema, dispatcher, monitoring. Worth it when revenue decisions, ad spend or commissions depend on the numbers.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The third option matters most when other systems consume the same orders. In &lt;a href="https://disruptivestudio.com/mlm-platform" rel="noopener noreferrer"&gt;network marketing platforms with built-in compensation plans&lt;/a&gt;, for example, commissions, ranks and analytics all read the same purchase and refund data; a single ledger keeps them consistent instead of reconciling three versions of the truth.&lt;/p&gt;

&lt;h2&gt;
  
  
  Security and privacy
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Keep API secrets (Measurement Protocol api_secret, Conversions API access tokens) on the server only. Never ship them to the browser.&lt;/li&gt;
&lt;li&gt;Verify webhook signatures before any database write, and reject unsigned requests.&lt;/li&gt;
&lt;li&gt;Hash user-provided identifiers with SHA-256 before sending them, as the EndFrame guide cited above recommends for GA4, and follow each platform's normalization rules.&lt;/li&gt;
&lt;li&gt;Store consent with the order and have the dispatcher check it per destination. Analytics in your own warehouse and ad-platform sharing can have different legal bases depending on your jurisdiction.&lt;/li&gt;
&lt;li&gt;Log payloads with personal data redacted; your dead-letter table will be read by humans.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Common mistakes
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Generating the deduplication ID at send time instead of at order creation.&lt;/li&gt;
&lt;li&gt;Calling third-party APIs synchronously inside the checkout request.&lt;/li&gt;
&lt;li&gt;Updating order totals in place on refund, which destroys the audit trail and breaks historical KPIs.&lt;/li&gt;
&lt;li&gt;Grouping by UTC day for a store that sells in another time zone.&lt;/li&gt;
&lt;li&gt;Treating ad-platform conversion counts as revenue. They are attribution estimates, not your books.&lt;/li&gt;
&lt;li&gt;No alerting on outbox depth, so a broken destination is discovered a week after the sale.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Pre-Black Friday checklist
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Every order stores a transaction ID and an event ID generated at creation.&lt;/li&gt;
&lt;li&gt;Ledger table is append-only with a unique (source, source_ref) constraint.&lt;/li&gt;
&lt;li&gt;Outbox rows are written in the same transaction as the order or ledger row.&lt;/li&gt;
&lt;li&gt;Dispatcher has backoff with jitter, per-destination expiry and a dead-letter state.&lt;/li&gt;
&lt;li&gt;Webhook endpoint verifies signatures and returns success for duplicates.&lt;/li&gt;
&lt;li&gt;Dashboards query the ledger with explicit time zone and frozen exchange rates.&lt;/li&gt;
&lt;li&gt;Alerts exist for outbox backlog, dead-letter growth and destination error rates.&lt;/li&gt;
&lt;li&gt;A load test has exercised checkout with tracking enabled at your expected peak.&lt;/li&gt;
&lt;li&gt;You can replay a day of events to a destination without creating duplicates.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If your current stack cannot meet most of this list, it is usually a design problem rather than a tooling one. Our team builds this kind of order and event infrastructure as part of &lt;a href="https://disruptivestudio.com/custom-software" rel="noopener noreferrer"&gt;custom software development for ecommerce and sales operations&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Frequently asked questions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Do I still need the browser tag if I track purchases on the server?
&lt;/h3&gt;

&lt;p&gt;Usually yes. The server is the right source for money, but the browser still provides session, campaign and behavioral context. Run both and deduplicate with shared IDs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Can I just use my payment provider's dashboard as the source of truth?
&lt;/h3&gt;

&lt;p&gt;It is a good reconciliation check, but it rarely has your product, campaign and customer context. Keep your own ledger and reconcile it against the provider's reports periodically.&lt;/p&gt;

&lt;h3&gt;
  
  
  How do I backfill events that failed during a peak?
&lt;/h3&gt;

&lt;p&gt;Reset dead outbox rows to pending once the cause is fixed, provided they are still within each destination's accepted time window. Outside that window, report them from your warehouse rather than forcing them into the ad platform.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally published on &lt;a href="https://disruptivestudio.com/blog/server-side-ecommerce-tracking" rel="noopener noreferrer"&gt;Disruptive Studio&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>ecommerce</category>
      <category>analytics</category>
      <category>architecture</category>
    </item>
    <item>
      <title>Three-Way Match Automation: Build an Invoice Verification Engine</title>
      <dc:creator>Daniel Cruz</dc:creator>
      <pubDate>Wed, 25 Nov 2026 17:00:31 +0000</pubDate>
      <link>https://dev.to/daniel_cruz_aaf7dd957e107/three-way-match-automation-build-an-invoice-verification-engine-4j8o</link>
      <guid>https://dev.to/daniel_cruz_aaf7dd957e107/three-way-match-automation-build-an-invoice-verification-engine-4j8o</guid>
      <description>&lt;p&gt;Most invoice fraud does not beat a clever detection model. It beats a payables system that pays whatever lands in the inbox once someone clicks "approve". &lt;strong&gt;Three-way match automation&lt;/strong&gt; fixes that at the engineering level. Before money moves, the software checks three things. The invoice must match a purchase order your company issued. A receipt must show that the goods or services actually arrived. And the bank account being paid must be one you verified. This guide covers the data model, the matching algorithm, duplicate detection, vendor bank account controls, failure handling and the trade-offs between approaches.&lt;/p&gt;

&lt;p&gt;The risk is well documented. The FBI's &lt;a href="https://www.ic3.gov/AnnualReport/Reports/2024_IC3Report.pdf" rel="noopener noreferrer"&gt;2024 IC3 Internet Crime Report&lt;/a&gt; lists $2,770,151,146 in reported losses from business email compromise (BEC). &lt;a href="https://abnormal.ai/blog/2024-fbi-ic3-report" rel="noopener noreferrer"&gt;Abnormal's summary of the same report&lt;/a&gt; counts 21,442 BEC incidents that year. For the non-technical side (red flags and a process for the finance team), read our guide on &lt;a href="https://disruptivestudio.com/blog/fake-invoice-scam-small-business" rel="noopener noreferrer"&gt;how to spot and stop a fake invoice scam&lt;/a&gt;. This article covers the system behind that process.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the engine has to decide
&lt;/h2&gt;

&lt;p&gt;The engine does not output a score. It returns one of three decisions per invoice, each with a reason code that software can read:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;PAY&lt;/strong&gt;: every line matches within tolerance, the vendor is active, and the bank account is verified and has not changed recently.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;HOLD&lt;/strong&gt;: a person needs to look at it (price variance above tolerance, partial receipt, new bank account, possible duplicate).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;REJECT&lt;/strong&gt;: there is no matching purchase order, the vendor is blocked, or the invoice is an exact duplicate of one already paid.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Three fixed outcomes make the system auditable. An auditor, a developer and an accounts payable clerk can all act on a reason like "Held because the unit price on line 3 is above the PO price by more than the vendor tolerance".&lt;/p&gt;

&lt;h2&gt;
  
  
  Data model
&lt;/h2&gt;

&lt;p&gt;The key design choice: invoices, purchase orders (POs) and receipts are separate aggregates linked line by line. Vendor bank accounts are versioned records with their own lifecycle, never a column on the vendor table.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;vendors&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt;            &lt;span class="kt"&gt;uuid&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;legal_name&lt;/span&gt;    &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;tax_id&lt;/span&gt;        &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;UNIQUE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;status&lt;/span&gt;        &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;CHECK&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'active'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'blocked'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'pending'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;vendor_bank_accounts&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt;               &lt;span class="kt"&gt;uuid&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;vendor_id&lt;/span&gt;        &lt;span class="kt"&gt;uuid&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;REFERENCES&lt;/span&gt; &lt;span class="nf"&gt;vendors&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;account_hash&lt;/span&gt;     &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;          &lt;span class="c1"&gt;-- hash of normalized routing + account&lt;/span&gt;
  &lt;span class="n"&gt;account_last4&lt;/span&gt;    &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;status&lt;/span&gt;           &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;CHECK&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt;
                     &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'requested'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'verifying'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'verified'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'rejected'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'retired'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;requested_by&lt;/span&gt;     &lt;span class="kt"&gt;uuid&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;verified_by&lt;/span&gt;      &lt;span class="kt"&gt;uuid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;verified_via&lt;/span&gt;     &lt;span class="kt"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;                   &lt;span class="c1"&gt;-- e.g. 'callback_known_number'&lt;/span&gt;
  &lt;span class="n"&gt;verified_at&lt;/span&gt;      &lt;span class="kt"&gt;timestamptz&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;active_from&lt;/span&gt;      &lt;span class="kt"&gt;timestamptz&lt;/span&gt;             &lt;span class="c1"&gt;-- verified_at + cooling-off period&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;purchase_orders&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="kt"&gt;uuid&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;vendor_id&lt;/span&gt; &lt;span class="kt"&gt;uuid&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;currency&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;po_lines&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="kt"&gt;uuid&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;po_id&lt;/span&gt; &lt;span class="kt"&gt;uuid&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;sku&lt;/span&gt; &lt;span class="kt"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;qty&lt;/span&gt; &lt;span class="kt"&gt;numeric&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;unit_price&lt;/span&gt; &lt;span class="kt"&gt;numeric&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;receipts&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="kt"&gt;uuid&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;po_id&lt;/span&gt; &lt;span class="kt"&gt;uuid&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;received_at&lt;/span&gt; &lt;span class="kt"&gt;timestamptz&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;received_by&lt;/span&gt; &lt;span class="kt"&gt;uuid&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;receipt_lines&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="kt"&gt;uuid&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;receipt_id&lt;/span&gt; &lt;span class="kt"&gt;uuid&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;po_line_id&lt;/span&gt; &lt;span class="kt"&gt;uuid&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;qty_received&lt;/span&gt; &lt;span class="kt"&gt;numeric&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;invoices&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt;              &lt;span class="kt"&gt;uuid&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;vendor_id&lt;/span&gt;       &lt;span class="kt"&gt;uuid&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;REFERENCES&lt;/span&gt; &lt;span class="nf"&gt;vendors&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;invoice_number&lt;/span&gt;  &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;           &lt;span class="c1"&gt;-- as printed&lt;/span&gt;
  &lt;span class="n"&gt;invoice_number_norm&lt;/span&gt; &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;       &lt;span class="c1"&gt;-- normalized for duplicate checks&lt;/span&gt;
  &lt;span class="n"&gt;invoice_date&lt;/span&gt;    &lt;span class="kt"&gt;date&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;currency&lt;/span&gt;        &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;total&lt;/span&gt;           &lt;span class="kt"&gt;numeric&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;source&lt;/span&gt;          &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;           &lt;span class="c1"&gt;-- 'email_ocr','portal','edi'&lt;/span&gt;
  &lt;span class="n"&gt;status&lt;/span&gt;          &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;invoice_lines&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="kt"&gt;uuid&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;invoice_id&lt;/span&gt; &lt;span class="kt"&gt;uuid&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;po_line_id&lt;/span&gt; &lt;span class="kt"&gt;uuid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;qty&lt;/span&gt; &lt;span class="kt"&gt;numeric&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;unit_price&lt;/span&gt; &lt;span class="kt"&gt;numeric&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;match_results&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt;           &lt;span class="kt"&gt;uuid&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;invoice_id&lt;/span&gt;   &lt;span class="kt"&gt;uuid&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;REFERENCES&lt;/span&gt; &lt;span class="nf"&gt;invoices&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;decision&lt;/span&gt;     &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;CHECK&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;decision&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'PAY'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'HOLD'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'REJECT'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;reasons&lt;/span&gt;      &lt;span class="kt"&gt;jsonb&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;rules_version&lt;/span&gt; &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;evaluated_at&lt;/span&gt; &lt;span class="kt"&gt;timestamptz&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three details matter more than they seem to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Bank details never come from the invoice.&lt;/strong&gt; An attacker can type any account number on an invoice. Payment instructions come from &lt;code&gt;vendor_bank_accounts&lt;/code&gt; rows with &lt;code&gt;status = 'verified'&lt;/code&gt;. If the invoice shows a different account, that is a reason to HOLD, not an update.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Store &lt;code&gt;rules_version&lt;/code&gt; on every result.&lt;/strong&gt; When you change tolerances, you still need to explain why an invoice was paid last month under the old rules.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use &lt;code&gt;numeric&lt;/code&gt;, not floats,&lt;/strong&gt; for money and quantities, and store the currency on every document. A PO in MXN and an invoice in USD is a mismatch, not something to convert.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Three-way match automation: the matching algorithm
&lt;/h2&gt;

&lt;p&gt;Match each line, not just the invoice header. If you only compare totals (invoice total equals PO total), an attacker can rearrange lines or bill for items that never arrived and still keep a believable total.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;evaluate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;invoice&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;po&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;receipts&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;vendor&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bank&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rules&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;reasons&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;vendor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="s2"&gt;"active"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Decision&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"REJECT"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"vendor_not_active"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;po&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;po&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;vendor_id&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;invoice&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;vendor_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Decision&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"REJECT"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"no_matching_po"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;invoice&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;currency&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;po&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;currency&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;reasons&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"currency_mismatch"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;received&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;sum_received_by_po_line&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;receipts&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;        &lt;span class="c1"&gt;# {po_line_id: qty}&lt;/span&gt;
    &lt;span class="n"&gt;already_billed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;sum_billed_by_po_line&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;po&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;        &lt;span class="c1"&gt;# excludes this invoice&lt;/span&gt;
                                           &lt;span class="n"&gt;exclude&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;invoice&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;invoice&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;lines&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;pol&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;po&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;lines&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;po_line_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;pol&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;reasons&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"line_without_po:{line.id}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;continue&lt;/span&gt;
        &lt;span class="n"&gt;billable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;received&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pol&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;already_billed&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pol&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;qty&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;billable&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;rules&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;qty_tolerance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pol&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;reasons&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"qty_exceeds_received:{line.id}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="nf"&gt;within&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;unit_price&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pol&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;unit_price&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rules&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;price_tolerance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vendor&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pol&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;reasons&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"price_variance:{line.id}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;bank&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;bank&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="s2"&gt;"verified"&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;bank&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;active_from&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;reasons&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"bank_account_not_cleared"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Decision&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"HOLD"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;reasons&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;reasons&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nc"&gt;Decision&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"PAY"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Design notes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Billable quantity is what was received minus what was already billed.&lt;/strong&gt; Without the &lt;code&gt;already_billed&lt;/code&gt; term, one receipt can justify two invoices. That is exactly how split or repeated invoices get through.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tolerances are data, not code.&lt;/strong&gt; Store them per vendor or category with both a percentage and an absolute cap (for example, "2% but never more than X"). That way a small percentage on a very large line cannot hide a big overpayment.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Keep &lt;code&gt;evaluate&lt;/code&gt; a pure function.&lt;/strong&gt; It takes documents and rules and returns a decision. Loading data and saving results happen elsewhere. That makes it easy to unit test and to re-run when a late receipt arrives.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Two-way vs three-way vs four-way
&lt;/h3&gt;

&lt;p&gt;Two-way matching (invoice vs PO) is cheaper to build and works for services with no physical receipt, but it cannot tell you whether anything was delivered. Three-way adds the receipt and is the sensible default for goods. Four-way adds an inspection or quality record, and is only worth it where acceptance is a separate step. A practical approach is to let each PO declare its match type, so a software subscription and a pallet of product do not go through the same rules.&lt;/p&gt;

&lt;h2&gt;
  
  
  Duplicate invoice detection
&lt;/h2&gt;

&lt;p&gt;Both fraudsters and honest vendors resend invoices. Exact duplicates are easy to catch. The hard cases are near-duplicates: &lt;code&gt;INV-00123&lt;/code&gt; vs &lt;code&gt;inv 123&lt;/code&gt;, or the same amount and date with a new number.&lt;/p&gt;

&lt;p&gt;Normalize the number first, then enforce uniqueness in the database, not only in application code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;normalize_invoice_number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;upper&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="s2"&gt;"[^A-Z0-9]"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;          &lt;span class="c1"&gt;# drop spaces, dashes, slashes&lt;/span&gt;
    &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="s2"&gt;"^(INV|FACT|FACTURA|NO)"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lstrip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"0"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="s2"&gt;"0"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;UNIQUE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;uniq_invoice_per_vendor&lt;/span&gt;
  &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;invoices&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vendor_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;invoice_number_norm&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'void'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;a href="https://www.postgresql.org/docs/current/indexes-partial.html" rel="noopener noreferrer"&gt;PostgreSQL partial index&lt;/a&gt; lets a voided invoice be issued again without breaking the constraint. On top of that hard rule, run softer checks that add HOLD reasons instead of raising errors. Look for the same vendor and total within a few days, the same total across vendors that share a bank account hash, and PO lines billed twice. Soft checks will flag some legitimate invoices. That is fine, because the result is a human review, not a rejection.&lt;/p&gt;

&lt;h2&gt;
  
  
  Vendor bank account changes: the real attack surface
&lt;/h2&gt;

&lt;p&gt;Most fake invoice schemes end with a request to "update our bank details". Treat a bank account change as a security event with its own state machine:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;requested -&amp;gt; verifying -&amp;gt; verified -&amp;gt; (cooling-off) -&amp;gt; active
     \            \
      -&amp;gt; rejected  -&amp;gt; rejected
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Verify through a separate channel.&lt;/strong&gt; Confirm using contact details you already had on file, such as a phone number saved before the request. Never use contact details that came with the change request itself. Record who verified, how and when in &lt;code&gt;verified_by&lt;/code&gt;, &lt;code&gt;verified_via&lt;/code&gt; and &lt;code&gt;verified_at&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Maker-checker.&lt;/strong&gt; The person who requests the change cannot be the one who verifies it. Enforce this in the database or the service layer, not in the UI.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cooling-off period.&lt;/strong&gt; &lt;code&gt;active_from&lt;/code&gt; delays the first use of a new account. The first payment to that account goes to HOLD whatever the match result.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cross-vendor checks.&lt;/strong&gt; If a new account hash already belongs to another vendor, flag it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Account validation where available.&lt;/strong&gt; The ACH network already uses this idea in another context. Nacha's WEB debit rule, &lt;a href="https://www.nacha.org/content/account-validation-resource-center" rel="noopener noreferrer"&gt;effective March 19, 2021&lt;/a&gt;, requires validating consumer account information the first time it is used for an online debit. If your bank or payment provider offers account validation for payees, call it during &lt;code&gt;verifying&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The same pattern applies beyond suppliers. A &lt;a href="https://disruptivestudio.com/mlm-platform" rel="noopener noreferrer"&gt;network marketing platform&lt;/a&gt; that pays commissions to thousands of distributors has the same risk when someone changes a payout account, and the same state machine works there.&lt;/p&gt;

&lt;h2&gt;
  
  
  Architecture and trade-offs
&lt;/h2&gt;

&lt;p&gt;A workable pipeline has four stages:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Ingestion&lt;/strong&gt; (email with OCR, vendor portal, EDI or e-invoicing feeds). Treat everything here as untrusted input. Store the original file and its hash.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Normalization and enrichment&lt;/strong&gt;: find the vendor by tax ID, link invoice lines to PO lines and normalize the invoice number.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Evaluation&lt;/strong&gt;: the pure &lt;code&gt;evaluate&lt;/code&gt; function, which writes a &lt;code&gt;match_results&lt;/code&gt; row.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Payment release&lt;/strong&gt;: a separate service that pays only invoices whose latest result is PAY, and looks up bank details at the moment of payment.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Trade-offs to decide on purpose:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Inside the ERP vs a separate service.&lt;/strong&gt; Building inside the ERP or accounting tool keeps one source of truth, but you are limited to what its extension model allows. A separate service is easier to test and version, but you need reliable sync of POs, receipts and vendors. If you build your own, have a team experienced in &lt;a href="https://disruptivestudio.com/custom-software" rel="noopener noreferrer"&gt;custom software development&lt;/a&gt; define the sync contract before anyone writes matching code.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rules vs ML scoring.&lt;/strong&gt; Rules are explainable and predictable, which auditors value. Anomaly scoring can catch patterns that rules miss, but it should only add HOLD reasons. It should never approve a payment on its own.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Strict vs tolerant.&lt;/strong&gt; Tight tolerances mean more manual reviews. Loose ones let more overpayments through. Make tolerances visible, track how often people override a HOLD, and adjust.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Failure handling and concurrency
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Late receipts.&lt;/strong&gt; Invoices often arrive before the goods. Re-run the check on held invoices when a receipt for the same PO is recorded (through an event or a scheduled job), so a clerk does not have to re-check by hand.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Concurrent evaluation.&lt;/strong&gt; If two invoices for the same PO line are evaluated at the same time, both can see the full billable quantity. Lock the PO row (&lt;code&gt;SELECT ... FOR UPDATE&lt;/code&gt;) during evaluation, or use serializable transactions and retry on conflict.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Idempotent payment release.&lt;/strong&gt; Send the invoice ID plus the bank account ID as an idempotency key to your payment provider, so a retried job never pays twice.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Re-check at release.&lt;/strong&gt; The bank account or vendor status can change between evaluation and payment. The release service must read both again and stop if either differs from what was evaluated.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Append-only audit log.&lt;/strong&gt; Record every decision, override and bank change with who did it and when. Overrides need a reason, and above a set amount, a second approver.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Common mistakes
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Updating a vendor's bank account with data parsed from an invoice or email.&lt;/li&gt;
&lt;li&gt;Matching on header totals only.&lt;/li&gt;
&lt;li&gt;Enforcing segregation of duties in the UI while the API lets the same user create and approve.&lt;/li&gt;
&lt;li&gt;Using floats for money, or ignoring currency.&lt;/li&gt;
&lt;li&gt;Checking for duplicates on the raw invoice number.&lt;/li&gt;
&lt;li&gt;Letting an anomaly model approve payments.&lt;/li&gt;
&lt;li&gt;Not storing &lt;code&gt;rules_version&lt;/code&gt;, so past decisions cannot be explained.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Implementation checklist
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Versioned bank accounts with a state machine, verification through a separate channel and a cooling-off period.&lt;/li&gt;
&lt;li&gt;Line-by-line matching, with billable quantity = received minus already billed.&lt;/li&gt;
&lt;li&gt;Tolerances stored as data, with percentage and absolute caps.&lt;/li&gt;
&lt;li&gt;Normalized invoice numbers and a partial unique index per vendor.&lt;/li&gt;
&lt;li&gt;Soft duplicate checks that add HOLD reasons.&lt;/li&gt;
&lt;li&gt;A pure evaluation function with unit tests for each reason code.&lt;/li&gt;
&lt;li&gt;Row locking or serializable transactions on PO lines.&lt;/li&gt;
&lt;li&gt;A separate payment release service with idempotency keys and a final re-check.&lt;/li&gt;
&lt;li&gt;Maker-checker enforced in the API and the database.&lt;/li&gt;
&lt;li&gt;An append-only audit log and &lt;code&gt;rules_version&lt;/code&gt; on every decision.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Frequently asked questions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Can three-way match automation work for services with no physical delivery?
&lt;/h3&gt;

&lt;p&gt;Yes, if you define what counts as a receipt: an approved timesheet, a milestone sign-off or a service confirmation entered by the person who requested it. Without that, use two-way matching for those POs and require a named approver.&lt;/p&gt;

&lt;h3&gt;
  
  
  Should we use OCR to read invoices?
&lt;/h3&gt;

&lt;p&gt;OCR is fine for capturing data, but treat what it reads as untrusted. Use it to suggest line links and amounts, then let the matching rules and a human reviewer decide. Never use OCR output to change vendor master data.&lt;/p&gt;

&lt;h3&gt;
  
  
  Where should the payment decision live: in the ERP or a separate service?
&lt;/h3&gt;

&lt;p&gt;Either can work. What matters is that only one component can release payments, that it reads bank details from verified records and that it re-checks everything right before paying.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally published on &lt;a href="https://disruptivestudio.com/blog/three-way-match-automation-invoice-verification-engine" rel="noopener noreferrer"&gt;Disruptive Studio&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>fintech</category>
      <category>architecture</category>
      <category>database</category>
      <category>security</category>
    </item>
    <item>
      <title>Core Web Vitals Real User Monitoring: Design Your Own RUM Pipeline</title>
      <dc:creator>Daniel Cruz</dc:creator>
      <pubDate>Mon, 23 Nov 2026 17:00:59 +0000</pubDate>
      <link>https://dev.to/daniel_cruz_aaf7dd957e107/core-web-vitals-real-user-monitoring-design-your-own-rum-pipeline-3g78</link>
      <guid>https://dev.to/daniel_cruz_aaf7dd957e107/core-web-vitals-real-user-monitoring-design-your-own-rum-pipeline-3g78</guid>
      <description>&lt;p&gt;Lighthouse tells you how a page behaves on one machine, once. Your users get slower phones, worse networks and late third-party scripts. &lt;strong&gt;Core Web Vitals real user monitoring&lt;/strong&gt; (RUM) closes that gap by measuring LCP, INP and CLS in the browsers of actual visitors and sending the data to a backend you control. This guide is about designing that system: the client collector, the payload, the ingestion endpoint, the data model, the aggregation queries and the alerting, with the trade-offs at each step.&lt;/p&gt;

&lt;p&gt;If you are looking for the optimization work itself (images, caching, scripts, fonts), start with our guide on &lt;a href="https://disruptivestudio.com/blog/improve-website-speed" rel="noopener noreferrer"&gt;how to improve website speed in 9 steps&lt;/a&gt;. This article is about knowing, with evidence, which fixes matter for your real traffic and whether a deploy made things worse.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why build RUM when CrUX already exists
&lt;/h2&gt;

&lt;p&gt;Google's Chrome UX Report (CrUX) is free and based on real Chrome users, but it has two limits that matter for engineering teams. First, the CrUX API values are &lt;a href="https://developer.chrome.com/docs/crux/methodology/tools" rel="noopener noreferrer"&gt;calculated over the previous 28 days as a rolling window&lt;/a&gt;, so a regression shipped today blends in slowly and a fix takes weeks to show. Second, it only knows about URLs and origins: it cannot tell you which release, which page template, which A/B variant or which DOM element caused the problem.&lt;/p&gt;

&lt;p&gt;Your options, roughly:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;CrUX only.&lt;/strong&gt; Zero build cost, no per-release or per-element detail, slow feedback.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A commercial RUM product.&lt;/strong&gt; Fast to adopt, good dashboards; you pay per volume and accept their data model and retention.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Your own pipeline.&lt;/strong&gt; More work, but you choose the dimensions (release, route template, tenant, experiment), keep the raw data and can join it with business events like checkout completion.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Building your own makes sense when you deploy often, need data per tenant or customer, or must keep the data in your infrastructure.&lt;/p&gt;

&lt;h2&gt;
  
  
  The targets you are measuring against
&lt;/h2&gt;

&lt;p&gt;Keep the definitions straight before designing storage. INP &lt;a href="https://searchengineland.com/google-core-web-vitals-to-add-interaction-to-next-paint-on-march-12-437072" rel="noopener noreferrer"&gt;replaced First Input Delay as a Core Web Vital on March 12, 2024&lt;/a&gt;, so a modern pipeline collects LCP, INP and CLS (plus TTFB as a diagnostic). The "good" thresholds are &lt;a href="https://www.corewebvitals.io/core-web-vitals" rel="noopener noreferrer"&gt;LCP of 2.5 s or less, INP under 200 ms and CLS under 0.1, assessed at the 75th percentile, with phones and desktop evaluated separately&lt;/a&gt;. Google explains that it chose the &lt;a href="https://web.dev/articles/defining-core-web-vitals-thresholds" rel="noopener noreferrer"&gt;75th percentile partly because it is less sensitive to outliers&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;So: aggregate with percentiles, not averages, and make device class a first-class dimension.&lt;/p&gt;

&lt;h2&gt;
  
  
  Core Web Vitals real user monitoring: the architecture
&lt;/h2&gt;

&lt;p&gt;A RUM pipeline has five parts:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Collector&lt;/strong&gt; in the page: measures metrics and attribution, batches them.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Beacon endpoint&lt;/strong&gt;: accepts small, untrusted POSTs, validates them, responds fast.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Buffer&lt;/strong&gt;: a queue or an in-memory batch so ingestion never blocks on the database.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Store&lt;/strong&gt;: one row per metric per page view, upserted by metric ID.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Aggregation and alerting&lt;/strong&gt;: p75 by route, device and release, with minimum sample sizes.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Start simple: one small service writing to Postgres, and swap the buffer and store later if volume demands it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: the client collector
&lt;/h2&gt;

&lt;p&gt;Do not write the metric logic yourself; INP and CLS have edge cases (back/forward cache, prerendering, background tabs) that Google's &lt;a href="https://github.com/GoogleChrome/web-vitals" rel="noopener noreferrer"&gt;web-vitals library&lt;/a&gt; already handles. Use its &lt;strong&gt;attribution build&lt;/strong&gt;, which adds an &lt;code&gt;attribution&lt;/code&gt; object to each metric with diagnostic data and is imported from &lt;code&gt;web-vitals/attribution&lt;/code&gt; instead of &lt;code&gt;web-vitals&lt;/code&gt;. Recent releases also expose the target element in INP attribution.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;onCLS&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;onINP&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;onLCP&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;onTTFB&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'web-vitals/attribution'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="no"&gt;ENDPOINT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'/rum'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="no"&gt;SAMPLE_RATE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__RUM_SAMPLE_RATE__&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Sample per session, not per page view, so a user is either in or out.&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;inSample&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;flag&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sessionStorage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'rum'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;flag&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;flag&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;random&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;SAMPLE_RATE&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="s1"&gt;'1'&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'0'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;sessionStorage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'rum'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;flag&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;flag&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s1"&gt;'1'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;pending&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// keyed by metric.id: only the latest value is kept&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;summarize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;attribution&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s1"&gt;'LCP'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ttfb&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;timeToFirstByte&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;loadDelay&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;resourceLoadDelay&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;loadDuration&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;resourceLoadDuration&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;renderDelay&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;elementRenderDelay&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s1"&gt;'INP'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;interactionTarget&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;interactionType&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;inputDelay&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;inputDelay&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;processing&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;processingDuration&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;presentation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;presentationDelay&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s1"&gt;'CLS'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;largestShiftTarget&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;enqueue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;metric&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;pending&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;metric&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;metric&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;metric&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;metric&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;rating&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;metric&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;rating&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nav&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;metric&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;navigationType&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;attr&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;summarize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;metric&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;flush&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pending&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;route&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__ROUTE__&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;          &lt;span class="c1"&gt;// template, e.g. "/product/:slug"&lt;/span&gt;
    &lt;span class="n"&gt;release&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__RELEASE__&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;      &lt;span class="c1"&gt;// git SHA or build number&lt;/span&gt;
    &lt;span class="n"&gt;ect&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;navigator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;connection&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;effectiveType&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;metrics&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pending&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;values&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;pending&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;clear&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;navigator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sendBeacon&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;ENDPOINT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nb"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;ENDPOINT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'POST'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;keepalive&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;inSample&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;onLCP&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;enqueue&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nf"&gt;onINP&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;enqueue&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nf"&gt;onCLS&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;enqueue&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nf"&gt;onTTFB&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;enqueue&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'visibilitychange'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;visibilityState&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s1"&gt;'hidden'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;flush&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Design notes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Flush on &lt;code&gt;visibilitychange&lt;/code&gt; to hidden&lt;/strong&gt;, not on &lt;code&gt;unload&lt;/code&gt;. Hidden is the last moment you can reliably send data on mobile, and INP and CLS keep changing until then.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The same metric can be reported more than once.&lt;/strong&gt; If the user comes back to the tab and interacts again, INP for the same page view gets a new value with the same &lt;code&gt;id&lt;/code&gt;. The library's own examples pass &lt;code&gt;id&lt;/code&gt; precisely so events can be aggregated per page view. Your backend must treat &lt;code&gt;id&lt;/code&gt; as an upsert key.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Send a string body to a first-party path.&lt;/strong&gt; A string body goes out as &lt;code&gt;text/plain&lt;/code&gt; (no CORS preflight), and first-party paths are less likely to be blocked.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Send the route template, not the raw URL.&lt;/strong&gt; &lt;code&gt;/product/:slug&lt;/code&gt; aggregates; &lt;code&gt;/product/blue-shirt-xl?utm_source=…&lt;/code&gt; explodes your cardinality and can leak personal data in query strings.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Keep the attribution small:&lt;/strong&gt; selectors and a few numbers, not full performance entries.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The attribution build is a little heavier than the standard build, so load the collector after the critical path, for example with a dynamic &lt;code&gt;import()&lt;/code&gt; once the page is interactive.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: the beacon endpoint
&lt;/h2&gt;

&lt;p&gt;Treat every beacon as hostile input and do as little as possible synchronously.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Express-style pseudo-code&lt;/span&gt;
&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'/rum'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;express&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'*/*'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;limit&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'16kb'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;204&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;end&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                 &lt;span class="c1"&gt;// respond first, never make the page wait&lt;/span&gt;

  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;metrics&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;metrics&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;isBot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'user-agent'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;route&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;KNOWN_ROUTES&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;has&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;route&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;route&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'other'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;device&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;deviceClass&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'user-agent'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    &lt;span class="c1"&gt;// 'mobile' | 'desktop' | 'tablet'&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;country&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'cf-ipcountry'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;       &lt;span class="c1"&gt;// CDN geo header; never store the IP&lt;/span&gt;

  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;metrics&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="no"&gt;ALLOWED&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;has&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;continue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="s1"&gt;'number'&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;MAX&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;continue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;metric_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;64&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;rating&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;rating&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nav&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;nav&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;route&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;device&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;country&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;release&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;release&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;attr&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;truncateStrings&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;attr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nb"&gt;setInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;writeBatch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;splice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// batch inserts&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Key decisions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Allow-list routes and metric names.&lt;/strong&gt; Anything else becomes &lt;code&gt;other&lt;/code&gt; or is dropped. This is your main defense against cardinality explosions and junk data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cap values per metric&lt;/strong&gt; (&lt;code&gt;MAX&lt;/code&gt;) at a ceiling you choose; values beyond it are usually broken clocks or tabs left open.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Derive device class and country on the server.&lt;/strong&gt; Do not trust the client for dimensions you can compute, and avoid storing IP addresses at all.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rate-limit at the edge&lt;/strong&gt;: a RUM endpoint is an open write API.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Batch writes.&lt;/strong&gt; At higher volume, replace the in-memory buffer with a queue (SQS, Pub/Sub, Kafka, Redis Streams) so a database incident does not drop the service.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step 3: the data model
&lt;/h2&gt;

&lt;p&gt;Store one row per metric per page view. Keep it narrow and append-friendly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;rum_metric&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;metric_id&lt;/span&gt;    &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;           &lt;span class="c1"&gt;-- web-vitals id, unique per page view + metric&lt;/span&gt;
  &lt;span class="n"&gt;received_at&lt;/span&gt;  &lt;span class="kt"&gt;timestamptz&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;name&lt;/span&gt;         &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;              &lt;span class="c1"&gt;-- LCP | INP | CLS | TTFB&lt;/span&gt;
  &lt;span class="n"&gt;value&lt;/span&gt;        &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="kt"&gt;precision&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;rating&lt;/span&gt;       &lt;span class="kt"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;                       &lt;span class="c1"&gt;-- good | needs-improvement | poor&lt;/span&gt;
  &lt;span class="n"&gt;nav&lt;/span&gt;          &lt;span class="kt"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;                       &lt;span class="c1"&gt;-- navigate | reload | back-forward-cache | prerender ...&lt;/span&gt;
  &lt;span class="n"&gt;route&lt;/span&gt;        &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;device&lt;/span&gt;       &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;country&lt;/span&gt;      &lt;span class="kt"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;release&lt;/span&gt;      &lt;span class="kt"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;attr&lt;/span&gt;         &lt;span class="kt"&gt;jsonb&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;rum_metric&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;received_at&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;-- Upsert so later reports for the same page view replace earlier ones&lt;/span&gt;
&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;rum_metric&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;metric_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rating&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nav&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;route&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;device&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;country&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;release&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;attr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$1&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="k"&gt;CONFLICT&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;metric_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;DO&lt;/span&gt; &lt;span class="k"&gt;UPDATE&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;EXCLUDED&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rating&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;EXCLUDED&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;rating&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;attr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;EXCLUDED&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;attr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;received_at&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;EXCLUDED&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;received_at&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Storage trade-offs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Postgres with raw rows&lt;/strong&gt; is the simplest and exact. Partition by day and drop old partitions to control retention.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A columnar store&lt;/strong&gt; (ClickHouse, BigQuery) handles much larger volumes and has built-in quantile functions, at the cost of another system to run. Columnar stores usually prefer appends over upserts; there you insert every report and keep the latest per &lt;code&gt;metric_id&lt;/code&gt; at query time (for example with &lt;code&gt;argMax&lt;/code&gt; in ClickHouse).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pre-aggregated histograms&lt;/strong&gt; (fixed buckets per metric, route, device and hour) are tiny and merge cheaply, but percentiles become approximate and you lose per-element attribution. A common hybrid: histograms for long-term trends, raw rows with short retention for debugging.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step 4: aggregation queries that match how Google scores you
&lt;/h2&gt;

&lt;p&gt;Compute p75 per metric, per device class, per route, with a minimum sample size so small groups do not trigger noise.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;route&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;device&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) AS samples,
       percentile_cont(0.75) WITHIN GROUP (ORDER BY value) AS p75
FROM rum_metric
WHERE name = 'INP'
  AND received_at &amp;gt; now() - interval '7 days'
GROUP BY route, device
HAVING count(*)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;min_samples&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;p75&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To find what to fix, group by attribution instead of by route:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;attr&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="s1"&gt;'target'&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;element&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) AS poor_interactions,
       avg((attr-&amp;gt;&amp;gt;'processing')::float) AS avg_processing_ms
FROM rum_metric
WHERE name = 'INP' AND rating = 'poor'
  AND received_at &amp;gt; now() - interval '7 days'
GROUP BY 1
ORDER BY poor_interactions DESC
LIMIT 20;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The INP sub-parts tell you where the time went: a long &lt;strong&gt;input delay&lt;/strong&gt; points to main-thread work that was already running (often third-party scripts), long &lt;strong&gt;processing&lt;/strong&gt; points to your event handlers, and long &lt;strong&gt;presentation delay&lt;/strong&gt; points to rendering cost after the handler. For LCP, comparing TTFB, resource load delay, load duration and render delay tells you whether to look at the server, the discovery of the hero image, its size, or render-blocking code. If you need to name the guilty script, the attribution build can also surface Long Animation Frame data, which &lt;a href="https://github.com/GoogleChrome/web-vitals" rel="noopener noreferrer"&gt;only reports script entries above 5 ms&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 5: release-aware alerting
&lt;/h2&gt;

&lt;p&gt;Owning the pipeline lets you catch regressions in hours, not weeks. Every row carries &lt;code&gt;release&lt;/code&gt;, so compare the new build with the previous one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for each (route, device, metric) with samples &amp;gt;= min_samples in both releases:
    delta = p75(new_release) - p75(previous_release)
    if delta &amp;gt; tolerance[metric] and p75(new_release) crosses the "good" threshold:
        alert(owner_of(route), route, device, metric, top attribution targets)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Compare like with like (same routes, device class and comparable time windows) and include the top attribution targets so whoever is on call starts from a selector, not a dashboard.&lt;/p&gt;

&lt;h2&gt;
  
  
  Common mistakes
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Averaging instead of using percentiles.&lt;/strong&gt; The mean hides the slow tail that the 75th percentile is designed to capture.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Mixing mobile and desktop&lt;/strong&gt; in a single number, which hides mobile problems behind fast desktops.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Raw URLs as a dimension.&lt;/strong&gt; Unbounded cardinality, slow queries, personal data in query strings.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Inserting every report as a new page view.&lt;/strong&gt; Without upserting by &lt;code&gt;id&lt;/code&gt;, page views with several INP updates are counted more than once.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Sending on &lt;code&gt;unload&lt;/code&gt;&lt;/strong&gt; or only once at load time, which misses late INP and CLS updates.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Loading the collector render-blocking&lt;/strong&gt;, so the measurement becomes the regression.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Alerting without minimum samples&lt;/strong&gt;, which trains the team to ignore alerts.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Implementation checklist
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;web-vitals attribution build loaded after the critical path&lt;/li&gt;
&lt;li&gt;Session-level sampling with a configurable rate&lt;/li&gt;
&lt;li&gt;Route template and release ID injected at build or render time&lt;/li&gt;
&lt;li&gt;Flush on &lt;code&gt;visibilitychange&lt;/code&gt; to hidden with &lt;code&gt;sendBeacon&lt;/code&gt; and a &lt;code&gt;keepalive&lt;/code&gt; fallback&lt;/li&gt;
&lt;li&gt;First-party endpoint that answers 204 immediately&lt;/li&gt;
&lt;li&gt;Allow-lists, value caps and size limits; dimensions derived server-side; no IP storage&lt;/li&gt;
&lt;li&gt;Upsert (or latest-per-ID at query time) keyed by metric ID&lt;/li&gt;
&lt;li&gt;p75 queries per metric, route and device with a minimum sample size&lt;/li&gt;
&lt;li&gt;Release comparison alerts that include top attribution targets&lt;/li&gt;
&lt;li&gt;Retention policy and a documented list of collected fields&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When the pipeline lives inside a larger product, such as a multi-tenant store or a customer portal, design it with the rest of your observability stack; that is the kind of work we do in &lt;a href="https://disruptivestudio.com/custom-software" rel="noopener noreferrer"&gt;custom software development&lt;/a&gt;. For marketing sites, the same principles apply in our &lt;a href="https://disruptivestudio.com/websites" rel="noopener noreferrer"&gt;website design and development&lt;/a&gt; projects.&lt;/p&gt;

&lt;h2&gt;
  
  
  Frequently asked questions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Will my RUM numbers match CrUX exactly?
&lt;/h3&gt;

&lt;p&gt;No. CrUX is built from real Chrome users over a rolling 28-day window; your pipeline uses your own browsers, sampling and windows. Use your data for diagnosis and CrUX as the external reference.&lt;/p&gt;

&lt;h3&gt;
  
  
  What sample rate should I use?
&lt;/h3&gt;

&lt;p&gt;It depends on traffic and how fine-grained your groups are. Pick a rate that lets each important route and device class clear your minimum sample size, and keep it configurable.&lt;/p&gt;

&lt;h3&gt;
  
  
  Should I send RUM data to Google Analytics instead of my own backend?
&lt;/h3&gt;

&lt;p&gt;It is a valid shortcut; the web-vitals documentation shows how to send metrics as analytics events. You trade control over dimensions, deduplication and retention for speed of setup.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally published on &lt;a href="https://disruptivestudio.com/blog/core-web-vitals-real-user-monitoring" rel="noopener noreferrer"&gt;Disruptive Studio&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>performance</category>
      <category>javascript</category>
      <category>observability</category>
    </item>
    <item>
      <title>Shopify vs Ariani for Network Marketing: Who's in Control</title>
      <dc:creator>Daniel Cruz</dc:creator>
      <pubDate>Sat, 21 Nov 2026 22:00:30 +0000</pubDate>
      <link>https://dev.to/daniel_cruz_aaf7dd957e107/shopify-vs-ariani-for-network-marketing-whos-in-control-4le3</link>
      <guid>https://dev.to/daniel_cruz_aaf7dd957e107/shopify-vs-ariani-for-network-marketing-whos-in-control-4le3</guid>
      <description>&lt;p&gt;&lt;strong&gt;On Shopify, a network marketing company rents a store and hangs its business from the outside.&lt;/strong&gt; The network, ranks, volume and commissions live in an external app that learns about each sale through notifications Shopify doesn't guarantee. &lt;a href="https://ariani.net" rel="noopener noreferrer"&gt;Ariani&lt;/a&gt; was built the other way around: the store, the network, commissions and wallets are one system.&lt;/p&gt;

&lt;p&gt;A regular store can live with that. A network marketing company can't, because every order moves people's money: volume, rank, bonuses and payouts. If an order gets lost, someone gets paid wrong.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Shopify's core isn't yours
&lt;/h2&gt;

&lt;p&gt;Shopify decides how far in you can go, and at the part that matters most to a network marketing company, checkout, the door is narrow:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Customizing the information, shipping and payment steps is &lt;a href="https://shopify.dev/docs/api/checkout-ui-extensions" rel="noopener noreferrer"&gt;available only on Shopify Plus&lt;/a&gt;, which &lt;a href="https://www.shopify.com/pricing" rel="noopener noreferrer"&gt;starts at $2,300 a month&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Custom apps with their own checkout logic (Functions) &lt;a href="https://shopify.dev/docs/apps/build/functions" rel="noopener noreferrer"&gt;also require Plus&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;That logic runs inside a cage: &lt;a href="https://shopify.dev/docs/api/functions/latest" rel="noopener noreferrer"&gt;11 million instructions, 128 kB of input and a 256 KiB program&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Calling an outside system from there is &lt;a href="https://shopify.dev/docs/apps/build/functions" rel="noopener noreferrer"&gt;limited to Enterprise stores and requires requesting access&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In plain terms: if your price depends on a member's rank, country, history and starter kit, you don't write that rule where you want it. You fit it where Shopify lets you.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Your money depends on a notification that may not arrive
&lt;/h2&gt;

&lt;p&gt;The commission app learns about sales through webhooks. Here is what Shopify's own documentation says:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://shopify.dev/docs/apps/build/webhooks" rel="noopener noreferrer"&gt;“Webhook delivery isn't always guaranteed.”&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://shopify.dev/docs/apps/build/webhooks/verify-deliveries" rel="noopener noreferrer"&gt;The same webhook can arrive more than once&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://shopify.dev/docs/apps/build/webhooks" rel="noopener noreferrer"&gt;“Shopify doesn't guarantee ordering within a topic.”&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If your system doesn't answer within 5 seconds it counts as a failure, and Shopify &lt;a href="https://shopify.dev/docs/apps/build/webhooks/verify-deliveries" rel="noopener noreferrer"&gt;retries 8 times over 4 hours&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Think about month-end close. A lost notification is volume that never counted and a rank that never moved. A duplicate is a commission paid twice. A refund that arrives before the purchase is a reversal of something your system doesn't know about yet.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  3. Your history has locks on it
&lt;/h2&gt;

&lt;p&gt;A network marketing company must be able to explain any commission, even one from two years ago. On Shopify, an app's standard permissions &lt;a href="https://shopify.dev/docs/api/usage/access-scopes" rel="noopener noreferrer"&gt;cover only orders from the last 60 days&lt;/a&gt;. To see more, a third-party app has to request approval from Shopify.&lt;/p&gt;

&lt;p&gt;Reading at volume has more ceilings: &lt;a href="https://shopify.dev/docs/api/usage/limits" rel="noopener noreferrer"&gt;250 records per page and 25,000 objects deep&lt;/a&gt; when paginating lists.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Your sponsor data can end up in the browser's hands
&lt;/h2&gt;

&lt;p&gt;The Shopify documentation we reviewed has no concept of sponsor, genealogy or rank. If an integration stores the sponsor as a cart attribute, there is a problem: those attributes &lt;a href="https://shopify.dev/docs/api/ajax/reference/cart" rel="noopener noreferrer"&gt;can be changed with a call from the browser&lt;/a&gt;, and “private” ones are &lt;a href="https://shopify.dev/docs/api/ajax/reference/cart" rel="noopener noreferrer"&gt;only visually hidden at checkout&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In a business where the sponsor decides who gets paid, that data should never be editable from the buyer's side.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Your autoship is borrowed too
&lt;/h2&gt;

&lt;p&gt;Autoship is a network marketing company's recurring revenue. On Shopify it &lt;a href="https://help.shopify.com/en/manual/products/purchase-options/subscriptions" rel="noopener noreferrer"&gt;runs on apps&lt;/a&gt;, and third-party ones &lt;a href="https://help.shopify.com/en/manual/products/purchase-options/subscriptions/considerations.md" rel="noopener noreferrer"&gt;work with only five payment gateways&lt;/a&gt;. If your bank or country needs another, that's the end of the conversation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Shopify vs Ariani, point by point
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;What network marketing needs&lt;/th&gt;
&lt;th&gt;Shopify&lt;/th&gt;
&lt;th&gt;Ariani&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Compensation plan&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Not found in its documentation; lives in an app&lt;/td&gt;
&lt;td&gt;Native: 30 bonus types, including binary, unilevel, matrix, residual and custom rules&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Network and sponsor&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Outside the platform&lt;/td&gt;
&lt;td&gt;Native, with placement movement history&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Volume and ranks&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Outside the platform&lt;/td&gt;
&lt;td&gt;Its own volume ledger and ranks in the same system&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;From sale to commission&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Connected by webhooks without guaranteed delivery&lt;/td&gt;
&lt;td&gt;Same system, no external app in between&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Refunds&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Depends on what the app does&lt;/td&gt;
&lt;td&gt;Built-in commission reversal&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Wallets and payouts&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Depends on the app&lt;/td&gt;
&lt;td&gt;Native wallets, ledger and payouts&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Member pricing&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Subject to checkout and plan limits&lt;/td&gt;
&lt;td&gt;Its own pricing rules and member tiers&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Autoship&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Apps; five gateways for third-party ones&lt;/td&gt;
&lt;td&gt;Native&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Payment gateways&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Whatever Shopify supports for each feature&lt;/td&gt;
&lt;td&gt;More than 25 integrated&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Changing the core&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;No: only through permitted extensions&lt;/td&gt;
&lt;td&gt;Yes: the team that builds it adapts it to your plan&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  The real question isn't which platform looks better
&lt;/h2&gt;

&lt;p&gt;It's who is in charge when something breaks. On Shopify you depend on Shopify, on the commission app and on the connection between them. With Ariani there is one system and one accountable team.&lt;/p&gt;

&lt;p&gt;Shopify is a great product for selling online. If your brand doesn't pay network commissions, use it. If your business lives on paying thousands of people every month, don't build it on something that was never designed for that.&lt;/p&gt;

&lt;h2&gt;
  
  
  Frequently asked questions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Can a network marketing company run on Shopify?
&lt;/h3&gt;

&lt;p&gt;It can sell on Shopify and calculate commissions with an app or external system. The risk is in that separation: operations depend on keeping two systems in sync.&lt;/p&gt;

&lt;h3&gt;
  
  
  Does Shopify Plus solve the problem?
&lt;/h3&gt;

&lt;p&gt;It opens up more checkout customization, but it doesn't add a network, ranks, volume or commissions.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is Ariani?
&lt;/h3&gt;

&lt;p&gt;It's Disruptive Studio's platform for network marketing companies: store, network, compensation plans, payments, wallets, CRM and AI in one system.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;See Ariani at &lt;a href="https://ariani.net" rel="noopener noreferrer"&gt;ariani.net&lt;/a&gt;.&lt;/strong&gt;&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally published on &lt;a href="https://disruptivestudio.com/blog/shopify-vs-ariani-network-marketing" rel="noopener noreferrer"&gt;Disruptive Studio&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>shopify</category>
      <category>ecommerce</category>
      <category>architecture</category>
      <category>saas</category>
    </item>
    <item>
      <title>Payment Reconciliation System Design: Data Model to Matching Engine</title>
      <dc:creator>Daniel Cruz</dc:creator>
      <pubDate>Sat, 21 Nov 2026 17:01:43 +0000</pubDate>
      <link>https://dev.to/daniel_cruz_aaf7dd957e107/payment-reconciliation-system-design-data-model-to-matching-engine-4db0</link>
      <guid>https://dev.to/daniel_cruz_aaf7dd957e107/payment-reconciliation-system-design-data-model-to-matching-engine-4db0</guid>
      <description>&lt;p&gt;Most teams start reconciliation with a spreadsheet and a script, and both break the first month volume doubles. This guide covers &lt;strong&gt;payment reconciliation system design&lt;/strong&gt; from an engineer's point of view: the data model, how to ingest from processors and banks without duplicates, how to build a matching engine you can trust, and how to handle the failures that make reconciliation hard. If you need the operational process first (what to match, in what order, and the finance checklist), read our &lt;a href="https://disruptivestudio.com/blog/automated-payment-reconciliation" rel="noopener noreferrer"&gt;7-step guide to automated payment reconciliation&lt;/a&gt;. This article is about the software underneath it.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the system actually has to prove
&lt;/h2&gt;

&lt;p&gt;A reconciliation system answers one question with evidence: for every unit of money that should have moved, did it move, in the right amount, to the right place, and can we show why we believe that?&lt;/p&gt;

&lt;p&gt;In practice that means linking three independent sources of truth:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Your ledger&lt;/strong&gt;: orders, invoices, refunds and credits your application created.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The processor&lt;/strong&gt;: charges, fees, refunds, disputes and the payouts that bundle them.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The bank&lt;/strong&gt;: deposits and withdrawals as they actually landed on the statement.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;None of these sources share a primary key end to end. The order ID lives in your database and maybe in processor metadata; the payout ID lives at the processor and maybe in the bank deposit description; the bank line has an amount, a date and a free-text reference. The system's job is to build and keep the links between them, and to make every link explainable.&lt;/p&gt;

&lt;h2&gt;
  
  
  Payment reconciliation system design: the data model
&lt;/h2&gt;

&lt;p&gt;The single most important decision is to separate &lt;strong&gt;what a source said&lt;/strong&gt; from &lt;strong&gt;what you concluded about it&lt;/strong&gt;. Raw records are immutable facts; matches are conclusions that can be revised.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- 1. Raw, immutable records exactly as received&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;source_record&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt;            &lt;span class="kt"&gt;BIGSERIAL&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;source&lt;/span&gt;        &lt;span class="kt"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;        &lt;span class="c1"&gt;-- 'ledger' | 'stripe' | 'bank_x'&lt;/span&gt;
  &lt;span class="n"&gt;external_id&lt;/span&gt;   &lt;span class="kt"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;        &lt;span class="c1"&gt;-- provider's own ID or a stable hash&lt;/span&gt;
  &lt;span class="n"&gt;payload&lt;/span&gt;       &lt;span class="kt"&gt;JSONB&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;       &lt;span class="c1"&gt;-- original data, never edited&lt;/span&gt;
  &lt;span class="n"&gt;ingested_at&lt;/span&gt;   &lt;span class="kt"&gt;TIMESTAMPTZ&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="k"&gt;UNIQUE&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;external_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;-- 2. Normalized view used by the matcher&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;txn&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt;              &lt;span class="kt"&gt;BIGSERIAL&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;source_record_id&lt;/span&gt; &lt;span class="kt"&gt;BIGINT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;REFERENCES&lt;/span&gt; &lt;span class="nf"&gt;source_record&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;kind&lt;/span&gt;            &lt;span class="kt"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;      &lt;span class="c1"&gt;-- charge | fee | refund | dispute | payout | deposit&lt;/span&gt;
  &lt;span class="n"&gt;amount_minor&lt;/span&gt;    &lt;span class="kt"&gt;BIGINT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;    &lt;span class="c1"&gt;-- signed, in minor units (cents)&lt;/span&gt;
  &lt;span class="n"&gt;currency&lt;/span&gt;        &lt;span class="kt"&gt;CHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;occurred_on&lt;/span&gt;     &lt;span class="kt"&gt;DATE&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;reference&lt;/span&gt;       &lt;span class="kt"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;               &lt;span class="c1"&gt;-- order id, payout id, bank reference&lt;/span&gt;
  &lt;span class="n"&gt;group_ref&lt;/span&gt;       &lt;span class="kt"&gt;TEXT&lt;/span&gt;                &lt;span class="c1"&gt;-- e.g. payout id that bundles this line&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;-- 3. Conclusions: a match group links N txns that must net to zero&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;match_group&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt;          &lt;span class="kt"&gt;BIGSERIAL&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;rule&lt;/span&gt;        &lt;span class="kt"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;          &lt;span class="c1"&gt;-- which rule or person created it&lt;/span&gt;
  &lt;span class="n"&gt;status&lt;/span&gt;      &lt;span class="kt"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;          &lt;span class="c1"&gt;-- active | reversed&lt;/span&gt;
  &lt;span class="n"&gt;created_by&lt;/span&gt;  &lt;span class="kt"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;created_at&lt;/span&gt;  &lt;span class="kt"&gt;TIMESTAMPTZ&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;match_line&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;match_group_id&lt;/span&gt; &lt;span class="kt"&gt;BIGINT&lt;/span&gt; &lt;span class="k"&gt;REFERENCES&lt;/span&gt; &lt;span class="nf"&gt;match_group&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;txn_id&lt;/span&gt;         &lt;span class="kt"&gt;BIGINT&lt;/span&gt; &lt;span class="k"&gt;REFERENCES&lt;/span&gt; &lt;span class="nf"&gt;txn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;match_group_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;txn_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A few choices here are deliberate:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Integer minor units plus a currency code.&lt;/strong&gt; Floating point will eventually produce a one-cent difference that nobody can explain. Store amounts as signed integers and carry the currency on every row. Be aware that not every currency has two decimal places, so keep the exponent in a currency table rather than hard-coding a divide by 100.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Signed amounts.&lt;/strong&gt; Money in is positive, money out is negative, from the point of view of one account. A match group is valid when its lines sum to zero (or to an explicitly recorded, approved difference). That single invariant replaces a lot of special-case code.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Many-to-many matches.&lt;/strong&gt; One bank deposit usually corresponds to one payout, which corresponds to hundreds of charges minus fees and refunds. One order can also be paid in two installments. A &lt;code&gt;match_group&lt;/code&gt; with N lines handles all of these shapes; a &lt;code&gt;matched_to_id&lt;/code&gt; column on the transaction table handles none of them well.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reversible conclusions.&lt;/strong&gt; Unmatching never deletes rows. You mark the group &lt;code&gt;reversed&lt;/code&gt; and create a new one. Auditors and your future self will ask why something was matched on a given date.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Ingestion: webhooks for speed, pulls for truth
&lt;/h2&gt;

&lt;p&gt;Webhooks are great for near-real-time status, but they are a notification channel, not a complete record. Events can arrive late, twice or out of order, and you will have outages on your side. A robust design uses both:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Webhooks&lt;/strong&gt; update operational state (order paid, refund issued) and enqueue a fetch.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scheduled pulls&lt;/strong&gt; of the processor's settlement data are the reconciliation source of truth. On Stripe, for example, settlement data is exposed through the Balance Transactions API, and the docs show how to &lt;a href="https://docs.stripe.com/capital/reporting-and-reconciliation?fetch-transactions-api=balance-transactions" rel="noopener noreferrer"&gt;retrieve balance transactions and filter them by type&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Bank statements&lt;/strong&gt; arrive as files or through a bank API, on the bank's schedule, not yours.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Every ingestion path writes through the same idempotent upsert keyed on &lt;code&gt;(source, external_id)&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;source_record&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;external_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="k"&gt;CONFLICT&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;external_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;DO&lt;/span&gt; &lt;span class="k"&gt;NOTHING&lt;/span&gt;
&lt;span class="k"&gt;RETURNING&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Bank lines are the hard case because many banks do not give you a stable line ID. Derive one from fields that do not change between exports (account, booking date, amount, reference, and a per-day sequence number), and keep the original file so you can re-derive it if the bank changes its format.&lt;/p&gt;

&lt;p&gt;The same discipline applies to calls you make &lt;em&gt;outward&lt;/em&gt;, like issuing refunds. Stripe's &lt;a href="https://stripe.com/docs/idempotency" rel="noopener noreferrer"&gt;idempotency documentation&lt;/a&gt; explains that it saves the status code and body of the first request for a given key and that keys can be removed once they are at least 24 hours old. That makes the key a retry guard for a single request, not a business-level duplicate check, so your own database still needs a unique constraint on "refund for order X, attempt Y".&lt;/p&gt;

&lt;h2&gt;
  
  
  The matching engine
&lt;/h2&gt;

&lt;p&gt;Run rules from most certain to least certain, and stop considering a transaction as soon as it is matched. A typical tier structure:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Exact reference match.&lt;/strong&gt; Your order ID stored in processor metadata equals the ledger order ID, same amount and currency. Fully automatic.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Settlement roll-up.&lt;/strong&gt; All processor lines with the same &lt;code&gt;group_ref&lt;/code&gt; (the payout) sum to the payout amount, and the payout amount equals a bank deposit within a date window. Fully automatic when the sums are exact.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Candidate match.&lt;/strong&gt; Same amount, same currency, date within N days, reference similarity above a threshold. Proposed, not committed, unless the rule has a proven track record.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Exception.&lt;/strong&gt; Anything left goes to a queue for a human.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;reconcile_payout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;payout&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bank_lines&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;processor_lines&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;lines&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;processor_lines&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;group_ref&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;payout&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;external_id&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;amount_minor&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;lines&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;payout&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;amount_minor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;exception&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"payout_lines_do_not_sum"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;payout&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;lines&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;candidates&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;bank_lines&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;currency&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;payout&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;currency&lt;/span&gt;
        &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;amount_minor&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;payout&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;amount_minor&lt;/span&gt;
        &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="nb"&gt;abs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;occurred_on&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;payout&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;arrival_date&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;days&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="no"&gt;DATE_WINDOW&lt;/span&gt;
        &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;is_matched&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;candidates&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;commit_group&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rule&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"payout_rollup"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;txns&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;payout&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;lines&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;candidates&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;candidates&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;exception&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"ambiguous_bank_line"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;payout&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;candidates&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;pending&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;payout&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# bank line may not have arrived yet&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice three outcomes besides success: a real exception, an ambiguity, and "not yet". Treating "not yet" as an error floods the queue every morning; treating it as fine forever hides missing money. Give pending items an age and escalate them after a threshold you agree with finance.&lt;/p&gt;

&lt;h3&gt;
  
  
  Deterministic rules vs. scoring
&lt;/h3&gt;

&lt;p&gt;A scoring model (weighted similarity on amount, date and reference) catches more matches, but it is harder to explain and to audit. Deterministic rules are boring and explainable. A practical compromise: deterministic rules auto-commit, scoring only &lt;em&gt;proposes&lt;/em&gt;, and a person accepts or rejects. Log every acceptance; after enough consistent decisions you have evidence to promote a pattern into a deterministic rule.&lt;/p&gt;

&lt;h2&gt;
  
  
  Architecture: batch, event-driven or hybrid
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Nightly batch.&lt;/strong&gt; Simple, easy to rerun, and matches how banks deliver statements. The downside is that problems surface a day late.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Event-driven.&lt;/strong&gt; Each new record triggers matching for its candidates. Faster feedback, but you must handle concurrency: two workers can try to claim the same bank line. Use row locks (&lt;code&gt;SELECT ... FOR UPDATE SKIP LOCKED&lt;/code&gt;) or a unique constraint so a transaction can belong to only one active group.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hybrid (usually the right answer).&lt;/strong&gt; Event-driven for tier 1 matches that only need ledger and processor data, plus a scheduled run when bank data lands, plus a full rerun command for any date range.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Whatever you choose, make the matcher a &lt;strong&gt;pure function of the stored data&lt;/strong&gt;: same inputs, same outputs. That is what lets you replay a month after fixing a rule bug and diff the results before committing them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Failure handling: the cases that cause most of the work
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Late and reordered data.&lt;/strong&gt; A refund can arrive after the payout that included it. Never assume the payout is final until its lines sum correctly.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Partial refunds and disputes.&lt;/strong&gt; Model them as separate negative transactions linked to the original charge, not as edits to the charge.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Currency conversion.&lt;/strong&gt; If the processor settles in a different currency than the charge, store both amounts and the rate that the processor reported. Do not recompute it with your own rate.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Bank format changes.&lt;/strong&gt; Version your parsers and keep raw files. When a parser changes, reprocess from raw and compare.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Manual fixes.&lt;/strong&gt; A person matching two items is a match group with &lt;code&gt;created_by&lt;/code&gt; set to that person and a required note. Consider requiring a second approver for groups that carry a non-zero difference.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Performance
&lt;/h2&gt;

&lt;p&gt;Matching is mostly a search problem. Avoid comparing every unmatched transaction with every other one:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Block candidates&lt;/strong&gt; by currency, amount and a date window before applying any fuzzy logic. An index on &lt;code&gt;(currency, amount_minor, occurred_on)&lt;/code&gt; filtered to unmatched rows does most of the work.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Partition by date&lt;/strong&gt; once tables grow, since nearly every query is bounded by a date range.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Batch writes&lt;/strong&gt; inside a transaction per match group, so a crash never leaves half a group committed.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Security and audit
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Use the narrowest API credentials each processor allows, read-only wherever reconciliation does not need to write.&lt;/li&gt;
&lt;li&gt;Never store card numbers; processor IDs are enough to reconcile.&lt;/li&gt;
&lt;li&gt;Encrypt raw bank files at rest and restrict who can download them.&lt;/li&gt;
&lt;li&gt;Keep an append-only audit log of rule changes, manual matches and reversals.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Common mistakes
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Using floats or decimals with inconsistent scale for money.&lt;/li&gt;
&lt;li&gt;Storing one &lt;code&gt;matched_to&lt;/code&gt; column, which cannot represent payouts or installments.&lt;/li&gt;
&lt;li&gt;Treating webhooks as a complete record of what happened.&lt;/li&gt;
&lt;li&gt;Deleting or editing raw records to "fix" a mismatch.&lt;/li&gt;
&lt;li&gt;Auto-committing fuzzy matches with no human review or log.&lt;/li&gt;
&lt;li&gt;Having no "pending" state, so normal settlement delays look like errors.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Implementation checklist
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Raw records stored immutably with a unique &lt;code&gt;(source, external_id)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Amounts as signed integers in minor units, with currency on every row.&lt;/li&gt;
&lt;li&gt;Many-to-many match groups that must net to zero.&lt;/li&gt;
&lt;li&gt;Webhooks for freshness, scheduled pulls for truth.&lt;/li&gt;
&lt;li&gt;Rules ordered from exact to fuzzy; fuzzy only proposes.&lt;/li&gt;
&lt;li&gt;Pending, ambiguous and exception states with aging.&lt;/li&gt;
&lt;li&gt;Replayable matcher and a rerun command for any date range.&lt;/li&gt;
&lt;li&gt;Audit log for every manual action and rule change.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Building this well takes careful engineering, and the payoff is a finance team that trusts the numbers. If you would rather have a team that has done it before, see how we approach &lt;a href="https://disruptivestudio.com/custom-software" rel="noopener noreferrer"&gt;custom software development for payment-heavy operations&lt;/a&gt;. Direct-selling companies have one more leg to reconcile, commission payouts to distributors, which is something our &lt;a href="https://disruptivestudio.com/mlm-platform" rel="noopener noreferrer"&gt;direct-selling platform with built-in payments and commissions&lt;/a&gt; is designed around.&lt;/p&gt;

&lt;h2&gt;
  
  
  Frequently asked questions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Should I build reconciliation or buy a tool?
&lt;/h3&gt;

&lt;p&gt;Buy if your sources are standard and a tool supports your processors and banks out of the box. Build, or extend, when you have unusual flows such as marketplace splits, installments or commission payouts, or when reconciliation must feed your own product data.&lt;/p&gt;

&lt;h3&gt;
  
  
  Can I reconcile in real time?
&lt;/h3&gt;

&lt;p&gt;Partially. Ledger-to-processor matches can be near real time through webhooks plus confirmation pulls. Bank-side matching runs at the pace your bank delivers statement data.&lt;/p&gt;

&lt;h3&gt;
  
  
  How do I handle a one-cent difference?
&lt;/h3&gt;

&lt;p&gt;Do not hide it in a rounding rule. Record it as an explicit difference line inside the match group, with a reason and an approver, so the group still nets to zero and the difference is reportable.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally published on &lt;a href="https://disruptivestudio.com/blog/payment-reconciliation-system-design" rel="noopener noreferrer"&gt;Disruptive Studio&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>payments</category>
      <category>architecture</category>
      <category>backend</category>
      <category>fintech</category>
    </item>
    <item>
      <title>Stripe Dispute Webhooks: Building a Chargeback Evidence Pipeline</title>
      <dc:creator>Daniel Cruz</dc:creator>
      <pubDate>Thu, 19 Nov 2026 16:00:08 +0000</pubDate>
      <link>https://dev.to/daniel_cruz_aaf7dd957e107/stripe-dispute-webhooks-building-a-chargeback-evidence-pipeline-1233</link>
      <guid>https://dev.to/daniel_cruz_aaf7dd957e107/stripe-dispute-webhooks-building-a-chargeback-evidence-pipeline-1233</guid>
      <description>&lt;p&gt;Most teams discover their dispute handling is broken the same way: a chargeback closes as lost because nobody saw it in time, or someone submitted half the evidence by accident. This guide is about &lt;strong&gt;Stripe dispute webhooks&lt;/strong&gt; and the system you build behind them: how to receive the events safely, model disputes in your own database, assemble evidence from order data automatically, stage it for review and never miss a deadline. It is written for developers, CTOs and technical founders who own the payments code, and it focuses on Stripe because its Disputes API is well documented, but the architecture applies to any processor.&lt;/p&gt;

&lt;p&gt;If you want the business side first (which policies, descriptors and customer-service habits reduce chargebacks before they happen), read our &lt;a href="https://disruptivestudio.com/blog/prevent-chargebacks-online-store" rel="noopener noreferrer"&gt;9-step guide to preventing chargebacks in an online store&lt;/a&gt;. Here we assume the dispute already exists and the question is how your software responds.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the platform actually tells you
&lt;/h2&gt;

&lt;p&gt;Before designing anything, pin down the facts your system has to respect:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Stripe emits &lt;code&gt;charge.dispute.created&lt;/code&gt;, &lt;code&gt;charge.dispute.updated&lt;/code&gt; and &lt;code&gt;charge.dispute.closed&lt;/code&gt; for the dispute lifecycle, and &lt;code&gt;radar.early_fraud_warning.created&lt;/code&gt; when an issuer or network flags a charge as potentially fraudulent, &lt;a href="https://docs.stripe.com/billing/subscriptions/webhooks" rel="noopener noreferrer"&gt;typically before a formal dispute&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;The Dispute object exposes &lt;code&gt;charge&lt;/code&gt; as an ID and a nullable &lt;code&gt;payment_intent&lt;/code&gt;, so you usually need to retrieve the Charge to link the dispute to your order (&lt;a href="https://docs.stripe.com/billing/subscriptions/webhooks" rel="noopener noreferrer"&gt;Stripe docs&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;Stripe does not guarantee delivery order, and event timestamps are in whole seconds, so it advises against ordering by &lt;code&gt;created&lt;/code&gt; and recommends fetching the current object from the API instead (&lt;a href="https://www.chargeflow.io/blog/dispute-webhooks" rel="noopener noreferrer"&gt;summary by Chargeflow&lt;/a&gt;, citing &lt;a href="https://docs.stripe.com/webhooks" rel="noopener noreferrer"&gt;Stripe's webhook docs&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;According to Stripe, you usually have 7 to 21 days to respond depending on the card network, and the issuer then usually takes 60 to 75 days to decide (&lt;a href="https://www.chargeflow.io/blog/dispute-webhooks" rel="noopener noreferrer"&gt;via Chargeflow&lt;/a&gt;). Your dispute records live for months, not minutes.&lt;/li&gt;
&lt;li&gt;On the update endpoint, "updating any field in the hash will submit all fields in the hash for review", the combined character count of all evidence fields is limited to 150,000, and &lt;code&gt;submit&lt;/code&gt; defaults to &lt;code&gt;true&lt;/code&gt; (&lt;a href="https://docs.stripe.com/api/disputes/update" rel="noopener noreferrer"&gt;Update a dispute&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;A dispute can, in rare cases, move from lost to won outside the normal lifecycle, and a single payment can have more than one dispute (&lt;a href="https://www.chargeflow.io/blog/dispute-webhooks" rel="noopener noreferrer"&gt;Chargeflow&lt;/a&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each of these turns into a design rule below.&lt;/p&gt;

&lt;h2&gt;
  
  
  Architecture: inbox, worker, evidence builder, scheduler
&lt;/h2&gt;

&lt;p&gt;A dispute pipeline that survives real traffic has four parts:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Webhook inbox.&lt;/strong&gt; A thin HTTP endpoint that verifies the signature, stores the raw event once, and returns 2xx. No business logic.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Sync worker.&lt;/strong&gt; Reads pending events, fetches the current Dispute from the API, upserts your &lt;code&gt;disputes&lt;/code&gt; table and triggers side effects (pause fulfillment, revoke access, notify).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Evidence builder.&lt;/strong&gt; Maps your order, shipping, login and policy data to Stripe's evidence fields and stages them without submitting.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scheduler and reconciler.&lt;/strong&gt; Escalates disputes approaching &lt;code&gt;evidence_details.due_by&lt;/code&gt; and periodically lists disputes from the API to catch anything a webhook missed.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The split matters because the four parts fail differently. The inbox must be fast and almost never fail; the worker can retry; the evidence builder depends on several internal systems that may be slow; the reconciler is your safety net when everything else goes wrong.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1: a webhook inbox that does almost nothing
&lt;/h3&gt;

&lt;p&gt;Verify the signature against the raw request body (parsing JSON first breaks verification), then insert the event with its ID as a unique key. The Stripe libraries apply a default timestamp tolerance of five minutes to the signature, which also limits replay attacks (&lt;a href="https://www.chargeflow.io/blog/dispute-webhooks" rel="noopener noreferrer"&gt;Chargeflow&lt;/a&gt;).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/webhooks/stripe"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;express&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"application/json"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;event&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stripe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;webhooks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;constructEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"stripe-signature"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nb"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;STRIPE_WEBHOOK_SECRET&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"invalid signature"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// Idempotent insert: duplicates are ignored, not errors&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="sb"&gt;`INSERT INTO webhook_inbox (event_id, type, object_id, payload, received_at)
     VALUES ($1, $2, $3, $4, now())
     ON CONFLICT (event_id) DO NOTHING`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sendStatus&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// acknowledge fast; the worker does the real work&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Trade-off: processing inline is simpler and fine at low volume, but any slow call (fetching the charge, uploading a file, posting to Slack) risks timeouts and redeliveries. An inbox table costs one migration and buys you replayability, an audit trail and a clean place to measure lag.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2: a data model that mirrors Stripe without depending on event order
&lt;/h3&gt;

&lt;p&gt;Store Stripe's status as-is and keep your own operational fields next to it. Do not try to compute status from the sequence of events.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;disputes&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;stripe_dispute_id&lt;/span&gt;   &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;stripe_charge_id&lt;/span&gt;    &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;payment_intent_id&lt;/span&gt;   &lt;span class="kt"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;             &lt;span class="c1"&gt;-- nullable in Stripe's object&lt;/span&gt;
  &lt;span class="n"&gt;order_id&lt;/span&gt;            &lt;span class="kt"&gt;bigint&lt;/span&gt; &lt;span class="k"&gt;REFERENCES&lt;/span&gt; &lt;span class="nf"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;amount&lt;/span&gt;              &lt;span class="kt"&gt;integer&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;-- minor units, may differ from the charge&lt;/span&gt;
  &lt;span class="n"&gt;currency&lt;/span&gt;            &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;reason&lt;/span&gt;              &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;stripe_status&lt;/span&gt;       &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;    &lt;span class="c1"&gt;-- copied verbatim from the API&lt;/span&gt;
  &lt;span class="n"&gt;due_by&lt;/span&gt;              &lt;span class="kt"&gt;timestamptz&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;      &lt;span class="c1"&gt;-- evidence_details.due_by&lt;/span&gt;
  &lt;span class="n"&gt;evidence_state&lt;/span&gt;      &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="s1"&gt;'none'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;-- none | drafted | staged | submitted&lt;/span&gt;
  &lt;span class="n"&gt;owner_user_id&lt;/span&gt;       &lt;span class="kt"&gt;bigint&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;last_synced_at&lt;/span&gt;      &lt;span class="kt"&gt;timestamptz&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;closed_at&lt;/span&gt;           &lt;span class="kt"&gt;timestamptz&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;disputes&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;due_by&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;evidence_state&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'submitted'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;disputes&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stripe_charge_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;-- one charge can have several disputes&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note the index on &lt;code&gt;stripe_charge_id&lt;/code&gt; rather than a unique constraint: since a payment can be disputed more than once, a one-to-one assumption between orders and disputes will eventually break.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 3: the worker fetches, then upserts
&lt;/h3&gt;

&lt;p&gt;The worker treats each event as a notification that "something changed", not as the source of truth. It retrieves the object and writes whatever the API returns.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;processDisputeEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;dispute&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;stripe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;disputes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;retrieve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;object_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;charge&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;stripe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;charges&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;retrieve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dispute&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;charge&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;orderId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;findOrderByCharge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;charge&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// e.g. from charge.metadata.order_id&lt;/span&gt;

  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="sb"&gt;`INSERT INTO disputes (stripe_dispute_id, stripe_charge_id, payment_intent_id, order_id,
       amount, currency, reason, stripe_status, due_by, last_synced_at)
     VALUES ($1,$2,$3,$4,$5,$6,$7,$8,to_timestamp($9),now())
     ON CONFLICT (stripe_dispute_id) DO UPDATE SET
       stripe_status = EXCLUDED.stripe_status,
       due_by = EXCLUDED.due_by,
       amount = EXCLUDED.amount,
       last_synced_at = now()`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;dispute&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;charge&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dispute&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;payment_intent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;orderId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dispute&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
     &lt;span class="n"&gt;dispute&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;currency&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dispute&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;reason&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dispute&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dispute&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;evidence_details&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;due_by&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;runSideEffectsOnce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dispute&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;orderId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// keyed by dispute id + effect name&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;markProcessed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;event_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because the worker always reads the latest state, a late &lt;code&gt;charge.dispute.updated&lt;/code&gt; arriving after &lt;code&gt;charge.dispute.closed&lt;/code&gt; simply writes the same closed status again. Side effects (pausing a shipment, suspending a subscription, notifying the team) should be recorded in a separate table keyed by dispute ID and effect name so a retry never sends two alerts or cancels twice.&lt;/p&gt;

&lt;h2&gt;
  
  
  Building the evidence: capture at checkout, not after the dispute
&lt;/h2&gt;

&lt;p&gt;The evidence builder can only use data you stored when the order happened. This is the part most teams cannot fix retroactively, so design the order data model with disputes in mind:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Identity and device:&lt;/strong&gt; customer email, name, billing address, purchase IP. These map to fields such as &lt;code&gt;customer_email_address&lt;/code&gt;, &lt;code&gt;customer_name&lt;/code&gt;, &lt;code&gt;billing_address&lt;/code&gt; and &lt;code&gt;customer_purchase_ip&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fulfillment:&lt;/strong&gt; carrier, tracking number, ship date and proof of delivery, mapping to &lt;code&gt;shipping_carrier&lt;/code&gt;, &lt;code&gt;shipping_tracking_number&lt;/code&gt;, &lt;code&gt;shipping_date&lt;/code&gt; and &lt;code&gt;shipping_documentation&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Digital goods and services:&lt;/strong&gt; login and usage events with timestamps, mapping to &lt;code&gt;access_activity_log&lt;/code&gt; and &lt;code&gt;service_date&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Policies:&lt;/strong&gt; the exact version of the refund and cancellation policy the customer accepted, with a timestamp, mapping to &lt;code&gt;refund_policy&lt;/code&gt; and &lt;code&gt;cancellation_policy&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Communication:&lt;/strong&gt; support tickets and emails with the customer, as files or text.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Files such as delivery confirmations or receipts are uploaded first with the Files API (purpose &lt;code&gt;dispute_evidence&lt;/code&gt;) and then referenced by ID in the evidence hash. The &lt;a href="https://docs.stripe.com/api/disputes" rel="noopener noreferrer"&gt;Disputes API reference&lt;/a&gt; lists every field, and Stripe's dispute guides explain which fields matter for which reason, so the builder should branch on &lt;code&gt;dispute.reason&lt;/code&gt; rather than send everything every time.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;buildEvidence&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dispute&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;base&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;customer_email_address&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;customer_name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;customerName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;customer_purchase_ip&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;purchaseIp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;product_description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;summary&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dispute&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;reason&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s2"&gt;"product_not_received"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;base&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;shipping_carrier&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;carrier&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;shipping_tracking_number&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;trackingNumber&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;shipping_date&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;shippedOn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;shipping_documentation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;deliveryProofFileId&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s2"&gt;"subscription_canceled"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;base&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;cancellation_policy&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cancellationPolicyFileId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;access_activity_log&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;accessLogText&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;base&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;uncategorized_text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;timelineText&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Keep generated text well below the 150,000-character combined limit. A concise timeline with dates beats a dump of raw logs, both for the limit and for the person at the bank who reads it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Stage first, submit deliberately
&lt;/h2&gt;

&lt;p&gt;The most dangerous default in this API is &lt;code&gt;submit: true&lt;/code&gt;. If any process updates evidence without explicitly passing &lt;code&gt;submit: false&lt;/code&gt;, Stripe submits everything immediately, and the &lt;a href="https://docs.stripe.com/disputes/arbitration" rel="noopener noreferrer"&gt;arbitration docs&lt;/a&gt; warn that adding or updating evidence without &lt;code&gt;submit=false&lt;/code&gt; submits all staged evidence. In practice that means a half-finished automated draft can become your final answer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Automated builder: always stage&lt;/span&gt;
&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;stripe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;disputes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dispute&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;evidence&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;buildEvidence&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dispute&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;submit&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Human approval (or an explicit rule) triggers the real submission&lt;/span&gt;
&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;stripe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;disputes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dispute&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;submit&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Wrap the Stripe client so that the only code path able to send &lt;code&gt;submit: true&lt;/code&gt; is the approval action, and make that path check &lt;code&gt;evidence_state&lt;/code&gt; and current &lt;code&gt;stripe_status&lt;/code&gt; first. Pass an idempotency key on these POST requests so a retried approval does not cause a confusing double update.&lt;/p&gt;

&lt;h3&gt;
  
  
  Smart Disputes versus your own builder
&lt;/h3&gt;

&lt;p&gt;Stripe offers &lt;a href="https://docs.stripe.com/disputes" rel="noopener noreferrer"&gt;Smart Disputes&lt;/a&gt; to automate evidence collection and submission for eligible disputes, and the Dispute object exposes a setting with values like &lt;code&gt;manual&lt;/code&gt;, &lt;code&gt;prefer_manual&lt;/code&gt;, &lt;code&gt;prefer_smart_disputes&lt;/code&gt; and &lt;code&gt;smart_disputes&lt;/code&gt; that decide which packet is used (&lt;a href="https://docs.stripe.com/api/disputes/object" rel="noopener noreferrer"&gt;Dispute object&lt;/a&gt;). The trade-off is straightforward: Smart Disputes uses what Stripe already knows and needs little engineering; your own builder can include evidence Stripe never sees, such as delivery photos, in-app usage or support conversations. Many teams combine them: let automation handle low-value cases and route high-value or ambiguous ones to the manual pipeline. Whatever you choose, make the choice explicit in configuration instead of letting it depend on whichever code happened to touch the dispute first.&lt;/p&gt;

&lt;h2&gt;
  
  
  Deadlines, reconciliation and early warnings
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Deadline scheduler.&lt;/strong&gt; Run a job that selects disputes where &lt;code&gt;evidence_state &amp;lt;&amp;gt; 'submitted'&lt;/code&gt; and &lt;code&gt;due_by&lt;/code&gt; is approaching, and escalates in tiers (owner, then team lead). Always compute and display deadlines in UTC alongside the reviewer's local time; teams split between Mexico City, Bogotá and the US will otherwise argue about what "due tomorrow" means.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reconciler.&lt;/strong&gt; Webhooks can fail: an endpoint was down longer than the retry window, a secret was rotated incorrectly, or an event type was never subscribed. A daily job that lists recent disputes and compares them with your table catches all of these.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="n"&gt;stripe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;disputes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;created&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;gte&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;thirtyDaysAgo&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;limit&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;local&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;getDispute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;local&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;local&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stripe_status&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;enqueueSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// same code path as the webhook worker&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Reusing the worker's code path is important: reconciliation that writes to the database through a different function will drift from it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Early fraud warnings.&lt;/strong&gt; &lt;code&gt;radar.early_fraud_warning.created&lt;/code&gt; arrives before a formal dispute in many cases. Route it to the same inbox and decide by rule whether to refund proactively, cancel fulfillment or just flag the order. Refunding avoids the dispute but gives up the revenue; fighting keeps the option to win but risks a dispute that counts against you. Make the rule depend on amount, whether the order already shipped and whether the customer has history with you.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Appeals.&lt;/strong&gt; If a dispute goes to pre-arbitration, Stripe accepts evidence through &lt;code&gt;evidence.appeal&lt;/code&gt;, and according to its docs, not submitting within 20 days means you implicitly assume liability (&lt;a href="https://docs.stripe.com/disputes/arbitration" rel="noopener noreferrer"&gt;Stripe arbitration docs&lt;/a&gt;). Model this as another evidence round on the same dispute, with its own deadline.&lt;/p&gt;

&lt;h2&gt;
  
  
  Common mistakes
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Parsing the JSON body before signature verification, so every signature check fails in production.&lt;/li&gt;
&lt;li&gt;Doing business logic inside the webhook handler and timing out under load.&lt;/li&gt;
&lt;li&gt;Deriving dispute status from event order instead of fetching the object.&lt;/li&gt;
&lt;li&gt;Assuming one dispute per order, or that the disputed amount equals the charge amount.&lt;/li&gt;
&lt;li&gt;Letting automated code update evidence without &lt;code&gt;submit: false&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Storing no purchase IP, policy version or delivery proof at checkout, then trying to reconstruct it weeks later.&lt;/li&gt;
&lt;li&gt;Having no reconciliation job, so a missed webhook becomes a missed deadline.&lt;/li&gt;
&lt;li&gt;Sending side effects (emails, cancellations) without deduplication.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Implementation checklist
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Subscribe to &lt;code&gt;charge.dispute.created&lt;/code&gt;, &lt;code&gt;charge.dispute.updated&lt;/code&gt;, &lt;code&gt;charge.dispute.closed&lt;/code&gt; and &lt;code&gt;radar.early_fraud_warning.created&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Verify signatures on the raw body; store events in an inbox with a unique event ID.&lt;/li&gt;
&lt;li&gt;Process asynchronously; always retrieve the current Dispute and Charge from the API.&lt;/li&gt;
&lt;li&gt;Model disputes with Stripe status verbatim, &lt;code&gt;due_by&lt;/code&gt;, evidence state, owner and order link.&lt;/li&gt;
&lt;li&gt;Capture evidence data at checkout and fulfillment time, including policy versions.&lt;/li&gt;
&lt;li&gt;Build evidence per reason code; upload files with purpose &lt;code&gt;dispute_evidence&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Stage with &lt;code&gt;submit: false&lt;/code&gt;; restrict &lt;code&gt;submit: true&lt;/code&gt; to an approval path.&lt;/li&gt;
&lt;li&gt;Escalate approaching deadlines; reconcile daily against the API.&lt;/li&gt;
&lt;li&gt;Deduplicate side effects; log every evidence change for audit.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If your store runs on a custom stack and this pipeline spans checkout, fulfillment and support systems, this is the kind of integration we design in our &lt;a href="https://disruptivestudio.com/custom-software" rel="noopener noreferrer"&gt;custom software projects&lt;/a&gt;. For stores still being planned, it is far cheaper to build the evidence fields into the &lt;a href="https://disruptivestudio.com/websites" rel="noopener noreferrer"&gt;online store&lt;/a&gt; from day one than to retrofit them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Frequently asked questions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Do I need a queue, or is a database table enough for the inbox?
&lt;/h3&gt;

&lt;p&gt;For most stores a database table with a unique event ID and a worker that polls it is enough, and it is easier to inspect and replay. Move to a dedicated queue when volume or latency requirements make polling impractical; keep the unique event ID either way.&lt;/p&gt;

&lt;h3&gt;
  
  
  Can I trust the dispute object inside the webhook payload?
&lt;/h3&gt;

&lt;p&gt;It is a snapshot from when the event was created. Because delivery order is not guaranteed, fetch the current Dispute from the API before writing state, and use the payload only to know which object to fetch.&lt;/p&gt;

&lt;h3&gt;
  
  
  Should evidence submission be fully automated?
&lt;/h3&gt;

&lt;p&gt;Only where you are confident in the data and the case is low risk. Since any evidence update submits by default, keep automated code on &lt;code&gt;submit: false&lt;/code&gt; and require an explicit approval, human or rule-based, for the final submission.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally published on &lt;a href="https://disruptivestudio.com/blog/stripe-dispute-webhooks-evidence-pipeline" rel="noopener noreferrer"&gt;Disruptive Studio&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>stripe</category>
      <category>payments</category>
      <category>webhooks</category>
      <category>architecture</category>
    </item>
    <item>
      <title>SaaS Cost per Tenant: Designing a Tenant Cost Attribution Pipeline</title>
      <dc:creator>Daniel Cruz</dc:creator>
      <pubDate>Tue, 17 Nov 2026 14:00:26 +0000</pubDate>
      <link>https://dev.to/daniel_cruz_aaf7dd957e107/saas-cost-per-tenant-designing-a-tenant-cost-attribution-pipeline-1ke9</link>
      <guid>https://dev.to/daniel_cruz_aaf7dd957e107/saas-cost-per-tenant-designing-a-tenant-cost-attribution-pipeline-1ke9</guid>
      <description>&lt;p&gt;Knowing your &lt;strong&gt;SaaS cost per tenant&lt;/strong&gt; lets you price tiers on data instead of instinct. Building the software is mostly a one-time investment (we covered those price ranges in &lt;a href="https://disruptivestudio.com/blog/how-much-custom-software-cost" rel="noopener noreferrer"&gt;our guide to how much custom software costs in 2026&lt;/a&gt;). Running it is different: it comes back every month and grows with every customer you add. This article covers the system that tells you which customers drive that monthly bill. That means the data model, the pipeline, the allocation rules, the trade-offs between approaches, and the places where it usually breaks.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why SaaS cost per tenant is hard to compute
&lt;/h2&gt;

&lt;p&gt;AWS's own write-up on &lt;a href="https://aws.amazon.com/blogs/apn/calculating-tenant-costs-in-saas-environments" rel="noopener noreferrer"&gt;calculating tenant costs in SaaS environments&lt;/a&gt; puts it plainly: in single-tenant environments the calculation is straightforward, but in a pooled model, where tenants share resources, attribution gets much harder. If every tenant has its own account or its own tagged resources, you just read the bill. If one database cluster, one queue and one container fleet serve everyone, the bill has no tenant column at all.&lt;/p&gt;

&lt;p&gt;Most real products are hybrids: pooled for the long tail, siloed for a few large customers. The &lt;a href="https://wa.aws.amazon.com/saas.question.COST_2.en.html" rel="noopener noreferrer"&gt;AWS Well-Architected SaaS Lens&lt;/a&gt; describes the general solution: correlate tenant consumption data with infrastructure costs, and automate it using Cost and Usage Report data. In practice that gives you a system with three inputs and one output:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Cost data:&lt;/strong&gt; what the cloud provider charged, per resource, per day.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Consumption data:&lt;/strong&gt; what each tenant actually used, emitted by your application.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Allocation rules:&lt;/strong&gt; how to split costs you cannot measure per tenant.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; a daily per-tenant cost ledger that adds up exactly to the bill.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Classify every dollar into three cost classes
&lt;/h2&gt;

&lt;p&gt;Before writing code, decide which class each line of the bill belongs to. This decision drives everything else.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Direct cost.&lt;/strong&gt; The resource belongs to one tenant: a dedicated database, a per-tenant bucket, an enterprise silo. You attribute it through a tag or an account ID.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Shared, metered cost.&lt;/strong&gt; A pooled resource where you can measure each tenant's share through a proxy: requests, CPU-milliseconds, stored bytes, messages processed, LLM tokens. You split the cost proportionally to that meter.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Shared, unmetered cost (overhead).&lt;/strong&gt; NAT gateways, observability, CI runners, the control plane. Nobody "uses" these per request. You need an explicit policy, and the common options are an equal split, tier-weighted shares, or a hybrid where direct costs go direct and shared costs follow usage (&lt;a href="https://danguisinger.com/guides/multi-tenant-saas-architecture-aws/" rel="noopener noreferrer"&gt;this multi-tenant AWS guide&lt;/a&gt; lists those three).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;One design decision pays off later: report overhead as its own visible line per tenant instead of folding it invisibly into the metered costs. When someone asks why a small tenant looks expensive, you will want to see how much of that comes from the policy and how much from real usage.&lt;/p&gt;

&lt;h2&gt;
  
  
  Architecture of a tenant cost attribution pipeline
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Cloud billing export (daily) ───────┐
                                    ├──&amp;gt; Allocation job (daily, idempotent) ──&amp;gt; tenant_cost_daily
App usage metering (events→rollups) ┘                ^
Tenant registry + versioned allocation rules ────────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  The cost source
&lt;/h3&gt;

&lt;p&gt;On AWS, the source of truth is the Cost and Usage Report. The CUR documentation describes &lt;a href="https://docs.aws.amazon.com/cur/latest/userguide/dataexports-create-standard.html" rel="noopener noreferrer"&gt;Data Exports with CUR 2.0&lt;/a&gt; as the new and recommended way to receive detailed cost and usage data. According to the &lt;a href="https://aws.amazon.com/aws-cost-management/aws-data-exports/faqs/" rel="noopener noreferrer"&gt;Data Exports FAQ&lt;/a&gt;, you define exports with basic SQL over AWS-provided tables, and they are delivered on a recurring basis. Two settings matter for tenant attribution:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Include resource IDs&lt;/strong&gt;, which also adds the tag column you need to read &lt;code&gt;tenant_id&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Split cost allocation data&lt;/strong&gt;, which creates line items per ECS task or Kubernetes pod. The &lt;a href="https://docs.aws.amazon.com/cur/latest/userguide/table-dictionary-cur2-split-line-item.html" rel="noopener noreferrer"&gt;split line item reference&lt;/a&gt; notes this is limited to Amazon ECS (including Fargate), AWS Batch and Amazon EKS. The FAQ also notes this data is not currently available in the FOCUS 1.2 export, so choose CUR 2.0 if you need it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Other clouds have equivalent billing exports. The design below does not depend on the provider, only on getting a daily, line-level file into your warehouse.&lt;/p&gt;

&lt;h3&gt;
  
  
  Tags for direct costs
&lt;/h3&gt;

&lt;p&gt;User-defined tags do nothing for billing until you activate them. Per the &lt;a href="https://github.com/awsdocs/aws-cost-management-user-guide/blob/master/doc_source/activating-tags.md" rel="noopener noreferrer"&gt;AWS documentation on activating cost allocation tags&lt;/a&gt;, newly applied tags can take up to 24 hours to appear for activation, and up to another 24 hours to activate. The &lt;code&gt;UpdateCostAllocationTagsStatus&lt;/code&gt; API lets you &lt;a href="https://docs.amazonaws.cn/en_us/awsaccountbilling/latest/aboutv2/activating-tags.html" rel="noopener noreferrer"&gt;activate tags in bulk&lt;/a&gt;. Do not rely on humans to remember tags. Enforce them in infrastructure as code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight hcl"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Terraform: every resource in a tenant silo inherits these tags&lt;/span&gt;
&lt;span class="n"&gt;provider&lt;/span&gt; &lt;span class="s2"&gt;"aws"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;alias&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"tenant"&lt;/span&gt;
  &lt;span class="n"&gt;region&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;region&lt;/span&gt;
  &lt;span class="n"&gt;default_tags&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;tags&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;tenant_id&lt;/span&gt;   &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tenant_id&lt;/span&gt;   &lt;span class="c1"&gt;# opaque ID, never a customer name&lt;/span&gt;
      &lt;span class="n"&gt;cost_class&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"direct"&lt;/span&gt;
      &lt;span class="n"&gt;environment&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environment&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Metering for pooled resources
&lt;/h3&gt;

&lt;p&gt;Emit usage where tenant context is already known and trusted, which usually means right after authentication. Do not write one row per request into your transactional database. Aggregate in memory and flush rollups.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Express-style middleware; req.tenant is resolved from the auth token, never the body&lt;/span&gt;
&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hrtime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;bigint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"finish"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;ms&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hrtime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;bigint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mf"&gt;1e6&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;usage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tenant&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"api.requests"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;usage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tenant&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"api.ms"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ms&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The buffer flushes every few seconds with an idempotency key, so retries after a timeout do not double count:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;usage_rollup&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;instance_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;flush_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tenant_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;meter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;hour&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;qty&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$6&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="k"&gt;CONFLICT&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;instance_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;flush_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tenant_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;meter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;hour&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;DO&lt;/span&gt; &lt;span class="k"&gt;NOTHING&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Background jobs must carry &lt;code&gt;tenant_id&lt;/code&gt; in their payload and meter their own work. For LLM features, record the token counts that the provider returns with each response, keyed by tenant.&lt;/p&gt;

&lt;h2&gt;
  
  
  Data model
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;cost_line&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;usage_date&lt;/span&gt;     &lt;span class="kt"&gt;date&lt;/span&gt;        &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;service&lt;/span&gt;        &lt;span class="kt"&gt;text&lt;/span&gt;        &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;resource_id&lt;/span&gt;    &lt;span class="kt"&gt;text&lt;/span&gt;        &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;tenant_tag&lt;/span&gt;     &lt;span class="kt"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;                    &lt;span class="c1"&gt;-- null when untagged&lt;/span&gt;
  &lt;span class="n"&gt;net_cost&lt;/span&gt;       &lt;span class="kt"&gt;numeric&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="c1"&gt;-- never float&lt;/span&gt;
  &lt;span class="n"&gt;currency&lt;/span&gt;       &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;     &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;export_version&lt;/span&gt; &lt;span class="kt"&gt;text&lt;/span&gt;        &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;usage_date&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;service&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;resource_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;export_version&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;allocation_rule&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;service&lt;/span&gt;    &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;cost_class&lt;/span&gt; &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;CHECK&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cost_class&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'direct'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'metered'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'overhead'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;meter&lt;/span&gt;      &lt;span class="kt"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;              &lt;span class="c1"&gt;-- for metered: which usage meter splits it&lt;/span&gt;
  &lt;span class="n"&gt;policy&lt;/span&gt;     &lt;span class="kt"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;              &lt;span class="c1"&gt;-- for overhead: equal | proportional | tier_weight&lt;/span&gt;
  &lt;span class="n"&gt;valid_from&lt;/span&gt; &lt;span class="kt"&gt;date&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;valid_to&lt;/span&gt;   &lt;span class="kt"&gt;date&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;tenant_cost_daily&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;usage_date&lt;/span&gt; &lt;span class="kt"&gt;date&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;tenant_id&lt;/span&gt;  &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;     &lt;span class="c1"&gt;-- includes the pseudo-tenant 'unallocated'&lt;/span&gt;
  &lt;span class="n"&gt;service&lt;/span&gt;    &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;cost_class&lt;/span&gt; &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;amount&lt;/span&gt;     &lt;span class="kt"&gt;numeric&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;currency&lt;/span&gt;   &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;run_id&lt;/span&gt;     &lt;span class="kt"&gt;uuid&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;usage_date&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tenant_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;service&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cost_class&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;unallocated&lt;/code&gt; pseudo-tenant matters. Untagged resources and services with no usage data go there, so the ledger always sums to the bill and the gap stays visible.&lt;/p&gt;

&lt;h2&gt;
  
  
  The allocation job
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for each day D in the recompute window:
  begin transaction
  delete from tenant_cost_daily where usage_date = D
  for each service S with cost on D:
    rule = active_rule(S, D)
    if rule.class == direct:
      for each line L: add(L.tenant_tag if valid_tenant(L.tenant_tag) else 'unallocated', L.net_cost)
    elif rule.class == metered:
      shares = usage(rule.meter, D) / sum(usage(rule.meter, D))
      if no usage: add('unallocated', cost(S, D))
      else: for each tenant t: add(t, cost(S, D) * shares[t])
    else:  # overhead, after direct and metered are done
      distribute(cost(S, D), rule.policy)
  round with the largest-remainder method so cents add up
  assert sum(tenant_cost_daily on D) == sum(cost_line on D)
  commit
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three properties make this job safe to run repeatedly:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Delete-and-replace per day inside one transaction.&lt;/strong&gt; Re-running gives the same answer, with no duplicate rows and no partial days.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A recompute window, not just "yesterday".&lt;/strong&gt; The &lt;a href="https://docs.aws.amazon.com/cur/latest/userguide/cur-create.html" rel="noopener noreferrer"&gt;CUR documentation&lt;/a&gt; notes that AWS keeps updating reports after delivery starts. Treat past days as mutable and recompute them each run.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A balance assertion.&lt;/strong&gt; If the allocated total does not match the source total, fail loudly. A cost ledger that silently drifts is worse than none.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Kubernetes and other pooled compute
&lt;/h2&gt;

&lt;p&gt;A pod in a shared deployment serves many tenants, so pod-level cost is still a shared, metered cost. You need two layers. First, get the cost of the workload. Then split it with your application meters. For the first layer you have two options:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;CUR split cost allocation data.&lt;/strong&gt; Native to AWS and needs no agent. Pod-level splits &lt;a href="https://www.automat-it.com/blog/optimizing-amazon-eks-unit-economics-the-ghost-in-the-cluster/" rel="noopener noreferrer"&gt;show up in the CUR, not in Cost Explorer&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;OpenCost.&lt;/strong&gt; A vendor-neutral, open-source project for measuring and allocating Kubernetes and cloud costs (&lt;a href="https://opencost.io/docs/faq/" rel="noopener noreferrer"&gt;OpenCost FAQ&lt;/a&gt;). It was &lt;a href="https://www.cncf.io/projects/opencost/" rel="noopener noreferrer"&gt;accepted to the CNCF in June 2022 and moved to Incubating on October 25, 2024&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you run namespace-per-tenant for large customers, those namespaces become direct costs, and only the shared ones need meters.&lt;/p&gt;

&lt;h2&gt;
  
  
  Failure modes to design for
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Untagged or mistagged resources.&lt;/strong&gt; Validate tag values against the tenant registry. Unknown values go to &lt;code&gt;unallocated&lt;/code&gt;. Alert when the unallocated share passes a threshold you choose.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Lost usage data.&lt;/strong&gt; If a deploy drops an in-memory buffer, the tenants on that instance are under-counted and everyone else's share inflates. Flush often, and consider a durable outbox for meters you bill on.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Discounts and commitments.&lt;/strong&gt; Allocate net or effective cost, not list price. In CUR 2.0, &lt;code&gt;split_line_item_net_split_cost&lt;/code&gt; is defined as the effective cost of a task or pod after all discounts.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rule changes.&lt;/strong&gt; Version rules with &lt;code&gt;valid_from&lt;/code&gt;/&lt;code&gt;valid_to&lt;/code&gt;. Changing a policy should not silently rewrite last quarter's margins.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Multiple currencies.&lt;/strong&gt; Store the currency per line and convert only at reporting time, with a recorded rate.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Security and privacy
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Cost allocation tags are visible to anyone with billing access. Use opaque tenant IDs, never customer names or emails.&lt;/li&gt;
&lt;li&gt;Per-tenant cost data is commercially sensitive. Restrict who can read it. If you show it to tenants (showback), enforce row-level security in the database instead of filtering in the API layer.&lt;/li&gt;
&lt;li&gt;Always derive &lt;code&gt;tenant_id&lt;/code&gt; for metering from the authenticated session. If a client can set it, a client can shift costs.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Trade-offs between approaches
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Account or silo per tenant.&lt;/strong&gt; The cleanest attribution, because the bill is already split (&lt;a href="https://scalefactory.com/blog/2021/07/20/b2b-saas-on-aws-how-worried-should-you-be-about-cost/" rel="noopener noreferrer"&gt;Scale Factory notes&lt;/a&gt; it suits products with a few large tenants). Operational overhead grows with tenant count.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pooled with metering.&lt;/strong&gt; Efficient infrastructure, but attribution is only as good as your meters and your choice of proxy.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Buy vs. build.&lt;/strong&gt; OpenCost or a commercial cost tool gives you visibility quickly. Build your own pipeline when cost per tenant feeds pricing, quotas or billing logic, because then you need full control of the rules.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Daily vs. hourly.&lt;/strong&gt; Daily is enough for margin analysis. Hourly helps you catch noisy neighbors, but it multiplies storage and recompute time.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Common mistakes
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Splitting the whole bill by request count, so a tenant running heavy reports looks as cheap as one making health checks.&lt;/li&gt;
&lt;li&gt;Using &lt;code&gt;float&lt;/code&gt; for money and then chasing rounding mismatches against the invoice.&lt;/li&gt;
&lt;li&gt;Dropping untagged costs instead of keeping them in an &lt;code&gt;unallocated&lt;/code&gt; bucket.&lt;/li&gt;
&lt;li&gt;Running the job only for yesterday and never picking up restated bills.&lt;/li&gt;
&lt;li&gt;Hiding overhead inside metered costs, which makes small tenants look unprofitable for the wrong reason.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Checklist
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Classify every billed service as direct, metered or overhead.&lt;/li&gt;
&lt;li&gt;Enable a line-level billing export (CUR 2.0 on AWS) with resource IDs, and split cost data if you run containers.&lt;/li&gt;
&lt;li&gt;Enforce &lt;code&gt;tenant_id&lt;/code&gt; tags in IaC and activate them in billing.&lt;/li&gt;
&lt;li&gt;Meter pooled resources from authenticated context, with idempotent flushes.&lt;/li&gt;
&lt;li&gt;Store rules with validity dates and money as &lt;code&gt;numeric&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Run an idempotent daily job over a recompute window, with a balance assertion.&lt;/li&gt;
&lt;li&gt;Track &lt;code&gt;unallocated&lt;/code&gt; as a metric and alert on it.&lt;/li&gt;
&lt;li&gt;Lock down access to per-tenant cost data.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you are planning a multi-tenant product and want cost attribution built in from the first sprint rather than added later, that is the kind of architecture our &lt;a href="https://disruptivestudio.com/custom-software" rel="noopener noreferrer"&gt;custom software development team&lt;/a&gt; designs and builds.&lt;/p&gt;

&lt;h2&gt;
  
  
  Frequently asked questions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Do I need cost per tenant before I have many customers?
&lt;/h3&gt;

&lt;p&gt;You do not need the full pipeline on day one. You do need &lt;code&gt;tenant_id&lt;/code&gt; in your tags, logs and usage events from the start, because adding tenant context later is much harder than computing costs later.&lt;/p&gt;

&lt;h3&gt;
  
  
  Is AWS Cost Explorer enough to see cost per tenant?
&lt;/h3&gt;

&lt;p&gt;For siloed resources with activated tags, it shows per-tenant spend directly. For pooled resources it cannot, because the bill has no tenant dimension. You need the CUR joined with your own usage data, and pod-level splits are only available in the CUR.&lt;/p&gt;

&lt;h3&gt;
  
  
  How should I attribute LLM API costs to tenants?
&lt;/h3&gt;

&lt;p&gt;Treat them as a shared, metered cost. Record the token usage that the provider returns with each call, keyed by the tenant that triggered it, and split the provider's invoice in proportion to those tokens.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally published on &lt;a href="https://disruptivestudio.com/blog/saas-cost-per-tenant-attribution-pipeline" rel="noopener noreferrer"&gt;Disruptive Studio&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>saas</category>
      <category>architecture</category>
      <category>aws</category>
      <category>finops</category>
    </item>
    <item>
      <title>RTO and RPO for Web Applications: A Disaster Recovery Design Guide</title>
      <dc:creator>Daniel Cruz</dc:creator>
      <pubDate>Sun, 15 Nov 2026 17:00:39 +0000</pubDate>
      <link>https://dev.to/daniel_cruz_aaf7dd957e107/rto-and-rpo-for-web-applications-a-disaster-recovery-design-guide-35ab</link>
      <guid>https://dev.to/daniel_cruz_aaf7dd957e107/rto-and-rpo-for-web-applications-a-disaster-recovery-design-guide-35ab</guid>
      <description>&lt;p&gt;Most teams can quote an uptime number, but far fewer can say how much data they would lose or how long they would be down if their primary database disappeared this afternoon. Those two numbers, recovery point objective (RPO) and recovery time objective (RTO), are the engineering core of any continuity plan. This guide covers how to set &lt;strong&gt;RTO and RPO for web applications&lt;/strong&gt; component by component, then design the backups, replication, failover and degradation paths that actually meet them. If you need the business side first (critical processes, people, communication), start with our &lt;a href="https://disruptivestudio.com/blog/business-continuity-plan-small-business" rel="noopener noreferrer"&gt;business continuity plan for small business in 8 steps&lt;/a&gt;. This article is about the system underneath.&lt;/p&gt;

&lt;h2&gt;
  
  
  What RTO and RPO actually measure
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;RPO&lt;/strong&gt; is the maximum amount of data, measured in time, you accept losing. An RPO of 15 minutes means that after recovery, at most the last 15 minutes of writes may be gone.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;RTO&lt;/strong&gt; is the maximum time from the start of an incident until the service is usable again. It includes detection, decision, restore and verification, not just the time the restore command runs.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Two consequences engineers often miss:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;RPO is a property of each data store, not of the application.&lt;/strong&gt; Your Postgres cluster, your S3 bucket of uploads, your Redis queue and your third-party CRM each have their own RPO. The application's real RPO is the worst of them for any given user workflow.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;RTO is dominated by humans and dependencies.&lt;/strong&gt; A restore that takes 20 minutes is irrelevant if nobody notices the outage for an hour, or if the restored app cannot start because its secrets lived in the account that was compromised.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Setting RTO and RPO for web applications component by component
&lt;/h2&gt;

&lt;p&gt;Start with an inventory, but make it a data model, not a spreadsheet nobody updates. Keep a recovery catalog in the repository, next to your infrastructure code, so it gets reviewed in pull requests:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="c1"&gt;# recovery-catalog.yaml
&lt;/span&gt;&lt;span class="na"&gt;workflows&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;checkout&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;rto&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;1h&lt;/span&gt;
    &lt;span class="na"&gt;rpo&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;5m&lt;/span&gt;
    &lt;span class="na"&gt;depends_on&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;postgres-main&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;payments-provider&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;object-store-invoices&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="na"&gt;reporting&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;rto&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;24h&lt;/span&gt;
    &lt;span class="na"&gt;rpo&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;24h&lt;/span&gt;
    &lt;span class="na"&gt;depends_on&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;postgres-replica&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;warehouse&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="na"&gt;components&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;postgres-main&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;kind&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;database&lt;/span&gt;
    &lt;span class="na"&gt;backup&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;pitr&lt;/span&gt;            &lt;span class="c1"&gt;# base backup + WAL archive
&lt;/span&gt;    &lt;span class="na"&gt;copy_location&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;separate-account&lt;/span&gt;
    &lt;span class="na"&gt;restore_runbook&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;runbooks/postgres-pitr.md&lt;/span&gt;
    &lt;span class="na"&gt;last_restore_test&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="c1"&gt;# filled by the restore drill job
&lt;/span&gt;  &lt;span class="na"&gt;object-store-invoices&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;kind&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;blob&lt;/span&gt;
    &lt;span class="na"&gt;backup&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;versioning + cross-account replication&lt;/span&gt;
  &lt;span class="na"&gt;payments-provider&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;kind&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;external&lt;/span&gt;
    &lt;span class="na"&gt;degraded_mode&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;queue-and-retry&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This structure lets you answer concrete questions automatically: which workflows depend on a component that has never been restore-tested, or which component's backup method cannot meet the strictest RPO of the workflows that use it. A small CI script that fails when &lt;code&gt;rpo&lt;/code&gt; targets are tighter than what the &lt;code&gt;backup&lt;/code&gt; method can deliver is cheap and catches drift.&lt;/p&gt;

&lt;p&gt;Set targets per workflow with the business owner, not per server. Checkout and login usually deserve tight targets; internal reporting can often wait a day. Tightening a target always costs money and complexity, so make every tight number justify itself.&lt;/p&gt;

&lt;h2&gt;
  
  
  Choosing a recovery pattern: the trade-offs
&lt;/h2&gt;

&lt;p&gt;AWS's disaster recovery whitepaper describes four patterns that are useful vocabulary on any cloud: backup and restore, pilot light, warm standby and multi-site active/active. It notes that if your definition of disaster includes losing a whole region, or regulation requires it, you should consider &lt;a href="https://docs.aws.amazon.com/whitepapers/latest/disaster-recovery-workloads-on-aws/disaster-recovery-options-in-the-cloud.html" rel="noopener noreferrer"&gt;pilot light, warm standby or multi-site active/active&lt;/a&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Backup and restore.&lt;/strong&gt; Cheapest, slowest. You rebuild infrastructure from code and restore data. RTO depends on how fast you can provision and how big your data is. Fine for most internal tools and many small SaaS products, provided the infrastructure is fully codified.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pilot light.&lt;/strong&gt; Data is continuously replicated to the recovery location, but compute is off or minimal. Failover means starting and scaling application servers. Good middle ground when RPO must be tight but RTO can be tens of minutes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Warm standby.&lt;/strong&gt; A scaled-down but fully working copy runs all the time. You can send test traffic to it, which makes it far easier to trust. Failover is mostly scaling and traffic routing.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Multi-site active/active.&lt;/strong&gt; Both sites serve traffic. Lowest RTO, but it forces you to solve write conflicts, data residency and cross-region latency. It changes your data model, not just your infrastructure.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The honest trade-off: every step up reduces RTO, but also increases the number of moving parts that can fail during normal operations. For many small teams, a well-tested backup and restore with infrastructure as code beats a poorly tested warm standby.&lt;/p&gt;

&lt;h2&gt;
  
  
  The database is where RPO is decided
&lt;/h2&gt;

&lt;p&gt;For PostgreSQL, nightly &lt;code&gt;pg_dump&lt;/code&gt; gives you an RPO of up to a day. For anything tighter, use continuous archiving and point-in-time recovery (PITR): a base backup plus the stream of write-ahead log (WAL) files. The &lt;a href="https://www.postgresql.org/docs/16/continuous-archiving.html" rel="noopener noreferrer"&gt;PostgreSQL documentation on PITR&lt;/a&gt; is the reference, and one detail matters for RPO: the archive command is only invoked on completed WAL segments. On a quiet database a segment can take a long time to fill, so set &lt;code&gt;archive_timeout&lt;/code&gt; to force regular switches:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ini"&gt;&lt;code&gt;&lt;span class="c1"&gt;# postgresql.conf
&lt;/span&gt;&lt;span class="na"&gt;wal_level&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;replica&lt;/span&gt;
&lt;span class="na"&gt;archive_mode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;on&lt;/span&gt;
&lt;span class="na"&gt;archive_command&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'test ! -f /mnt/wal_archive/%f &amp;amp;&amp;amp; cp %p /mnt/wal_archive/%f'&lt;/span&gt;
&lt;span class="na"&gt;archive_timeout&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;300&lt;/span&gt;   &lt;span class="c1"&gt;# force a segment switch at least every 5 minutes&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In production, replace the &lt;code&gt;cp&lt;/code&gt; with a tool that ships WAL to object storage in another account (pgBackRest and WAL-G are common choices) and alert when archiving fails. Each forced switch still produces a full-size segment file, so very short timeouts increase storage use.&lt;/p&gt;

&lt;p&gt;To recover to a moment just before a destructive mistake, you restore the base backup, set a target and, per the docs, create a &lt;code&gt;recovery.signal&lt;/code&gt; file in the data directory before starting the server:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ini"&gt;&lt;code&gt;&lt;span class="na"&gt;restore_command&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'cp /mnt/wal_archive/%f %p'&lt;/span&gt;
&lt;span class="na"&gt;recovery_target_time&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'2025-01-15 14:02:00+00'&lt;/span&gt;
&lt;span class="na"&gt;recovery_target_action&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'pause'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pausing lets you inspect the data before promoting. The same docs suggest temporarily restricting &lt;code&gt;pg_hba.conf&lt;/code&gt; so ordinary users cannot connect until you are sure the recovery succeeded.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Replication is not backup.&lt;/strong&gt; A streaming replica reduces RTO for hardware or zone failure, but it faithfully replays &lt;code&gt;DROP TABLE&lt;/code&gt;, a buggy migration or ransomware encryption. You need both: replicas for availability, PITR for recoverability. Managed databases (RDS, Cloud SQL, Azure Database) offer PITR as a setting, but check the retention window and whether snapshots can be copied out of the account.&lt;/p&gt;

&lt;h2&gt;
  
  
  Everything that is not the database
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Object storage.&lt;/strong&gt; Enable versioning so overwrites and deletes are recoverable, and replicate to a bucket in a separate account with its own credentials.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Queues and caches.&lt;/strong&gt; Decide explicitly whether they are state or not. If a Redis queue holds jobs that exist nowhere else, it has an RPO and needs persistence or a durable source of truth. Prefer designs where the queue can be rebuilt from the database.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Secrets and configuration.&lt;/strong&gt; Your recovery environment needs DNS credentials, TLS certificates, API keys and the backup decryption keys. Store a sealed copy outside the primary account, or you will discover during the incident that the keys to your backups were inside the blast radius.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Infrastructure.&lt;/strong&gt; If production was created by hand in a console, your RTO is however long it takes someone to remember every click. Terraform, Pulumi or CloudFormation turn rebuilds into a reviewed, repeatable step.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Third-party SaaS.&lt;/strong&gt; Email providers, payment gateways and CRMs will have outages you cannot fix. Export what you can on a schedule, and design the application to degrade instead of failing.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Designing for degraded mode
&lt;/h2&gt;

&lt;p&gt;When a dependency fails, the goal is to keep the critical workflow alive with reduced function. Two patterns do most of the work.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Circuit breaker.&lt;/strong&gt; Stop calling a failing dependency for a while so requests fail fast instead of piling up on timeouts:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CircuitBreaker&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;failures&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;openedAt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;threshold&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;cooldownMs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;30_000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="n"&gt;call&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fallback&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;open&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;failures&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;threshold&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
                 &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;openedAt&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cooldownMs&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;open&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;fallback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;failures&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;failures&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;failures&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;threshold&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;openedAt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;fallback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Transactional outbox.&lt;/strong&gt; Instead of calling the email or webhook provider inside the request, write the intent to your own database in the same transaction as the business change, and let a worker deliver it with retries:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;BEGIN&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;customer_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;outbox&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;topic&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;gen_random_uuid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'order.confirmation_email'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nb"&gt;jsonb_build_object&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'order_id'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'pending'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;COMMIT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the email provider is down for two hours, orders still succeed and confirmations go out when it returns. Make the worker idempotent, because retries will deliver some messages more than once.&lt;/p&gt;

&lt;h2&gt;
  
  
  Treat restores as a scheduled job, not a hope
&lt;/h2&gt;

&lt;p&gt;A backup you have never restored is a hypothesis. Automate a restore drill that runs on a schedule, restores the latest backup into an isolated environment, runs sanity checks and records how long it took:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/usr/bin/env bash&lt;/span&gt;
&lt;span class="nb"&gt;set&lt;/span&gt; -euo pipefail
&lt;span class="nv"&gt;start&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$(&lt;/span&gt;date +%s&lt;span class="o"&gt;)&lt;/span&gt;
./scripts/provision-restore-env.sh            &lt;span class="c"&gt;# infra as code, isolated network&lt;/span&gt;
./scripts/restore-latest.sh --target restore-env
psql &lt;span class="s2"&gt;"$RESTORE_URL"&lt;/span&gt; -c &lt;span class="s2"&gt;"SELECT count(*) FROM orders WHERE created_at &amp;gt; now() - interval '1 day';"&lt;/span&gt;
./scripts/smoke-tests.sh &lt;span class="s2"&gt;"$RESTORE_APP_URL"&lt;/span&gt;
&lt;span class="nv"&gt;end&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$(&lt;/span&gt;date +%s&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"restore_seconds=$((end-start))"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; ./scripts/report-metric.sh
./scripts/destroy-restore-env.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Compare the measured restore time and the age of the newest restored row against the RTO and RPO in your catalog, and update &lt;code&gt;last_restore_test&lt;/code&gt;. Then, at least occasionally, run a full game day where someone who did not write the runbook follows it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Common mistakes
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Backups stored in the same account, with the same credentials, as production.&lt;/li&gt;
&lt;li&gt;Only nightly dumps while the business assumes no data loss.&lt;/li&gt;
&lt;li&gt;A replica treated as the backup strategy.&lt;/li&gt;
&lt;li&gt;WAL archiving failing silently for weeks because nobody alerts on it.&lt;/li&gt;
&lt;li&gt;Restore runbooks that depend on the system being restored (a wiki hosted on the same cluster, for example).&lt;/li&gt;
&lt;li&gt;Measuring RTO as restore duration and ignoring detection and decision time.&lt;/li&gt;
&lt;li&gt;Synchronous calls to email, SMS or CRM providers inside critical requests.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Checklist
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Recovery catalog in the repo with RTO and RPO per workflow and dependencies per component.&lt;/li&gt;
&lt;li&gt;Every stateful component has a backup method that can meet its strictest RPO.&lt;/li&gt;
&lt;li&gt;Database PITR enabled, with &lt;code&gt;archive_timeout&lt;/code&gt; or equivalent sized to your RPO, and alerts on archive failures.&lt;/li&gt;
&lt;li&gt;Backup copies in a separate account with separate credentials; object storage versioned.&lt;/li&gt;
&lt;li&gt;Secrets, DNS access and decryption keys recoverable outside the primary account.&lt;/li&gt;
&lt;li&gt;All infrastructure defined as code and rebuildable from scratch.&lt;/li&gt;
&lt;li&gt;Circuit breakers and an outbox around external providers in critical paths.&lt;/li&gt;
&lt;li&gt;Automated restore drills with measured times, plus periodic human game days.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Getting this right is mostly careful design rather than expensive tooling, and it is much easier to build in from the start than to retrofit. If you are planning a new system or hardening an existing one, our team builds &lt;a href="https://disruptivestudio.com/custom-software" rel="noopener noreferrer"&gt;custom software designed for recovery from day one&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Frequently asked questions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What is the difference between RTO and RPO?
&lt;/h3&gt;

&lt;p&gt;RPO is how much recent data you can afford to lose, expressed as time. RTO is how long the service can be unavailable. A system can have a tight RPO (continuous WAL archiving) and still a long RTO (manual rebuild), so you need to design for both separately.&lt;/p&gt;

&lt;h3&gt;
  
  
  Is a read replica enough for disaster recovery?
&lt;/h3&gt;

&lt;p&gt;No. A replica helps you recover quickly from a server or zone failure, but it copies destructive changes immediately. You still need point-in-time backups stored outside the primary account.&lt;/p&gt;

&lt;h3&gt;
  
  
  How often should we test restores?
&lt;/h3&gt;

&lt;p&gt;Often enough that a broken backup is found before you need it. Automating the drill as a scheduled job makes frequent testing cheap; tie the frequency to how often your schema, data volume and infrastructure change.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally published on &lt;a href="https://disruptivestudio.com/blog/rto-rpo-web-applications-disaster-recovery" rel="noopener noreferrer"&gt;Disruptive Studio&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>devops</category>
      <category>architecture</category>
      <category>postgres</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Managing Redirects at Scale: Architecture for Site Migrations</title>
      <dc:creator>Daniel Cruz</dc:creator>
      <pubDate>Fri, 13 Nov 2026 17:00:56 +0000</pubDate>
      <link>https://dev.to/daniel_cruz_aaf7dd957e107/managing-redirects-at-scale-architecture-for-site-migrations-36o6</link>
      <guid>https://dev.to/daniel_cruz_aaf7dd957e107/managing-redirects-at-scale-architecture-for-site-migrations-36o6</guid>
      <description>&lt;p&gt;Managing redirects at scale is the part of a site migration that usually gets handed to whoever has time, and then breaks quietly: a config file grows past a platform limit, an editor adds a rule that creates a loop, or a chain of three hops appears after the second redesign. This article treats redirects as a small production system with its own data model, validation, runtime, failure modes and tests. If you need the SEO process side (URL inventory, mapping, launch-day checks), start with our &lt;a href="https://disruptivestudio.com/blog/website-migration-checklist" rel="noopener noreferrer"&gt;website migration checklist for keeping your SEO&lt;/a&gt;; here we focus on the engineering behind it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why a list in a config file stops working
&lt;/h2&gt;

&lt;p&gt;Most frameworks let you declare redirects in configuration, and for a few dozen rules that is the right answer. The problems show up as the list grows.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Platform limits.&lt;/strong&gt; The Next.js docs warn that &lt;a href="https://nextjs.org/docs/app/guides/redirecting" rel="noopener noreferrer"&gt;redirects may have a limit on platforms, and on Vercel there is a limit of 1,024 redirects&lt;/a&gt;; for 1,000+ they recommend a custom solution. That limit applies to routes in general: Vercel's community answer on the &lt;a href="https://community.vercel.com/t/maximum-number-of-routes-exceeded-max-is-1024/4347" rel="noopener noreferrer"&gt;"Maximum number of routes exceeded" error&lt;/a&gt; points out that other routes count too, and one &lt;a href="https://github.com/vercel/next.js/discussions/63753" rel="noopener noreferrer"&gt;GitHub discussion&lt;/a&gt; reports roughly 700 redirects being counted as more than 1,024 when i18n is configured without &lt;code&gt;locale: false&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Build-time evaluation.&lt;/strong&gt; As &lt;a href="https://agilitycms.com/docs/nextjs/implementing-url-redirects-with-next-js" rel="noopener noreferrer"&gt;Agility CMS's Next.js guide explains&lt;/a&gt;, &lt;code&gt;redirects()&lt;/code&gt; is evaluated once at build time, so a redirect an editor publishes afterwards does nothing until the next deploy.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No validation.&lt;/strong&gt; A plain array accepts duplicates, loops and redirects to pages that no longer exist. &lt;a href="https://www.sanity.io/learn/course/seo-optimization/implementing-redirects" rel="noopener noreferrer"&gt;Sanity's redirects course&lt;/a&gt; notes that without validation, authors can publish a redirect that breaks the deploy or creates circular redirects.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The fix is not a bigger file. It is to separate where redirects are &lt;strong&gt;stored and validated&lt;/strong&gt; from where they are &lt;strong&gt;resolved&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Data model: redirects are data, not code
&lt;/h2&gt;

&lt;p&gt;Store redirects in a table with enough metadata to answer "why does this exist and is it still used?" A workable starting schema:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;redirects&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt;             &lt;span class="kt"&gt;BIGSERIAL&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;source_path&lt;/span&gt;    &lt;span class="kt"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;          &lt;span class="c1"&gt;-- normalized, e.g. /blog/old-post&lt;/span&gt;
  &lt;span class="n"&gt;match_type&lt;/span&gt;     &lt;span class="kt"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="s1"&gt;'exact'&lt;/span&gt;
                 &lt;span class="k"&gt;CHECK&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;match_type&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'exact'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'prefix'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'pattern'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;destination&lt;/span&gt;    &lt;span class="kt"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;          &lt;span class="c1"&gt;-- path or absolute URL on an allowed host&lt;/span&gt;
  &lt;span class="n"&gt;status_code&lt;/span&gt;    &lt;span class="kt"&gt;SMALLINT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="mi"&gt;301&lt;/span&gt;
                 &lt;span class="k"&gt;CHECK&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status_code&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;301&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;302&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;307&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;308&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;410&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;preserve_query&lt;/span&gt; &lt;span class="kt"&gt;BOOLEAN&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="kc"&gt;TRUE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;origin&lt;/span&gt;         &lt;span class="kt"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;          &lt;span class="c1"&gt;-- 'migration-2025', 'editor', 'import'&lt;/span&gt;
  &lt;span class="n"&gt;created_by&lt;/span&gt;     &lt;span class="kt"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;created_at&lt;/span&gt;     &lt;span class="kt"&gt;TIMESTAMPTZ&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;hit_count&lt;/span&gt;      &lt;span class="kt"&gt;BIGINT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;last_hit_at&lt;/span&gt;    &lt;span class="kt"&gt;TIMESTAMPTZ&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="k"&gt;UNIQUE&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;source_path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;match_type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Design notes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;match_type&lt;/code&gt; keeps exact rules separate from patterns.&lt;/strong&gt; Exact rules are a hash lookup. Prefix and regex rules are a different, slower problem, and you want to know how many you have.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;410&lt;/code&gt; lives in the same table.&lt;/strong&gt; Some old URLs have no equivalent; recording that decision explicitly is better than letting them fall into a generic 404.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;origin&lt;/code&gt; and &lt;code&gt;created_by&lt;/code&gt;&lt;/strong&gt; make audits possible. When a migration batch turns out to be wrong, you can roll back that batch without touching editor rules.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;hit_count&lt;/code&gt; and &lt;code&gt;last_hit_at&lt;/code&gt;&lt;/strong&gt; tell you which rules still receive traffic. Update them asynchronously from logs, never in the request path.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Normalize once, at write time
&lt;/h2&gt;

&lt;p&gt;Most redirect bugs are matching bugs: &lt;code&gt;/Blog/Post/&lt;/code&gt;, &lt;code&gt;/blog/post&lt;/code&gt;, and &lt;code&gt;/blog/post?utm_source=x&lt;/code&gt; are different strings. Pick one canonical form and apply the same function when writing rules and when looking them up.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;normalizePath&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="no"&gt;URL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'https://placeholder.local'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;decodeURIComponent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pathname&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;\&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'/'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;          &lt;span class="c1"&gt;// collapse double slashes&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;endsWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'/'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                    &lt;span class="c1"&gt;// drop trailing slash&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toLowerCase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                     &lt;span class="c1"&gt;// only if your old site was case-insensitive&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two decisions here deserve a conscious choice rather than a default. Lowercasing is safe only if the old platform treated paths case-insensitively; otherwise you can merge URLs that used to be distinct. Query strings are excluded from the key on purpose: match on the path, then carry the query string over to the destination when &lt;code&gt;preserve_query&lt;/code&gt; is true, so tracking parameters survive.&lt;/p&gt;

&lt;h2&gt;
  
  
  Validate the graph before it ships
&lt;/h2&gt;

&lt;p&gt;A redirect set is a directed graph. Treat every write (single rule or bulk import) as a graph operation and reject anything that would leave it in a bad state.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;Rule&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;destination&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;flatten&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rules&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Map&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Rule&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;maxHops&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;errors&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rule&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="n"&gt;rules&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;seen&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;rule&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;destination&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;hops&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rules&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;has&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;seen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;has&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;errors&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`loop: ${[...seen].join(' -&amp;gt; ')}`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;hops&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;maxHops&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;errors&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`chain too long from ${source}`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="n"&gt;seen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="n"&gt;target&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;rules&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;destination&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;rule&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;destination&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// every rule now points at its final target&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;errors&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On top of loop and chain detection, run these checks in CI or in the admin API:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Destination exists.&lt;/strong&gt; Request each unique destination and require a 200 from the new site. A redirect to a 404 is worse than no redirect, because it hides the problem.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Source is not a live page.&lt;/strong&gt; If a source path also exists as a real route, one of them is wrong.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No self-redirects&lt;/strong&gt; after normalization (&lt;code&gt;/about/&lt;/code&gt; to &lt;code&gt;/about&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Allowed hosts only&lt;/strong&gt; for absolute destinations (more on this under security).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Flattening at write time means the runtime never follows chains: every request gets one hop to the final URL.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where to resolve redirects: the trade-offs
&lt;/h2&gt;

&lt;p&gt;There are four common places to run the lookup, and large sites usually combine them.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;CDN or reverse proxy (edge rules, Nginx map files).&lt;/strong&gt; Fastest, and the request never reaches your app. The cost is a separate deploy pipeline and, on managed CDNs, plan-dependent limits you should check in your provider's docs before committing.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Framework config (&lt;code&gt;redirects()&lt;/code&gt; in Next.js, equivalents elsewhere).&lt;/strong&gt; Zero runtime code and matched before the app renders. Good for a small, stable set of exact rules; limited by platform route caps and build-time evaluation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Middleware with a key-value store.&lt;/strong&gt; Rules are read at request time, so editors' changes apply without a deploy. The Next.js docs suggest reading from a store such as &lt;a href="https://nextjs.org/docs/app/guides/redirecting" rel="noopener noreferrer"&gt;Vercel's Edge Config or Redis&lt;/a&gt;. The cost is a lookup on every request unless you short-circuit it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Application fallback.&lt;/strong&gt; Only check the redirect table when the app is about to return a 404. Cheap, because it runs on misses only, but slower for redirected users and easy to bypass by accident with catch-all routes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A pattern that holds up well: exact migration rules at the edge or in a compiled map, editor-managed rules in middleware with a KV store, and the 404 handler as a last-chance lookup that also logs misses.&lt;/p&gt;

&lt;h2&gt;
  
  
  Making middleware lookups cheap with a Bloom filter
&lt;/h2&gt;

&lt;p&gt;The cost of middleware lookups is that they run on every request, including the vast majority that are not redirects. The &lt;a href="https://nextjs.org/docs/pages/guides/redirecting" rel="noopener noreferrer"&gt;Next.js guide on managing redirects at scale&lt;/a&gt; recommends using a Bloom filter to check whether a redirect might exist before reading the larger redirects file or database, and avoiding importing a large redirects file into middleware because it slows every request.&lt;/p&gt;

&lt;p&gt;A Bloom filter can return false positives but never false negatives, so a "no" lets you skip the lookup entirely and a "maybe" triggers the real one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// middleware.ts (proxy.ts in recent Next.js versions)&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;NextResponse&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;NextRequest&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'next/server'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;ScalableBloomFilter&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'bloom-filters'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;filterJson&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'./redirects/bloom-filter.json'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;normalizePath&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'./lib/normalize'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;filter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ScalableBloomFilter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fromJSON&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;filterJson&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="kt"&gt;any&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;middleware&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;NextRequest&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;normalizePath&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;nextUrl&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pathname&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;has&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;NextResponse&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;       &lt;span class="c1"&gt;// definite miss&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nb"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="no"&gt;URL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`/api/redirects?pathname=${encodeURIComponent(key)}`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;nextUrl&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;origin&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;AbortSignal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;300&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;res&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;NextResponse&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;        &lt;span class="c1"&gt;// false positive or failure&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;destination&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="no"&gt;URL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;destination&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;nextUrl&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;origin&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;search&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;nextUrl&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;search&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                     &lt;span class="c1"&gt;// preserve query string&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;NextResponse&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;redirect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The filter file is generated at build time from the redirects table. The route handler must handle false positives, and the same docs say you should validate requests to it to prevent malicious use. Note that in the current Next.js documentation, middleware is called Proxy (&lt;code&gt;proxy.ts&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Bloom filters only help with exact matches. A &lt;a href="https://github.com/vercel/next.js/discussions/84951" rel="noopener noreferrer"&gt;Next.js GitHub discussion on 1,000+ redirects&lt;/a&gt; makes the point that they do not work for wildcards or regex, and suggests a precompiled, prefix-indexed matcher so each request only tests a small subset of pattern rules. In practice: index pattern rules by their first static path segment (&lt;code&gt;/blog&lt;/code&gt;, &lt;code&gt;/shop&lt;/code&gt;) and compile the regexes once at startup.&lt;/p&gt;

&lt;h2&gt;
  
  
  Failure handling
&lt;/h2&gt;

&lt;p&gt;Decide in advance what happens when the redirect store is slow or down.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Fail open.&lt;/strong&gt; If the lookup times out, pass the request through. Users see a 404 on old URLs for a while, which is recoverable; a 500 on every request is not.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Short timeouts and a local cache.&lt;/strong&gt; Cache hits and misses in memory for a short time per instance; misses are the bulk of traffic.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Snapshot fallback.&lt;/strong&gt; Keep the last exported JSON of exact rules in the deploy artifact so the edge or app can resolve the critical migration set even if the live store is unavailable.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Idempotent bulk imports.&lt;/strong&gt; Imports should upsert on &lt;code&gt;(source_path, match_type)&lt;/code&gt; inside a transaction, so rerunning a failed import never creates duplicates.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Security: avoid building an open redirect
&lt;/h2&gt;

&lt;p&gt;A redirect service that accepts arbitrary destinations is an open redirect waiting to be abused for phishing.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Store destinations as paths where possible. For absolute URLs, check the host against an allowlist at write time and again at read time.&lt;/li&gt;
&lt;li&gt;Never build the destination from request input (query parameters, headers). The only input is the normalized path used as a lookup key.&lt;/li&gt;
&lt;li&gt;Protect the admin API and the bulk importer with authentication and an audit log; &lt;code&gt;created_by&lt;/code&gt; in the schema exists for this.&lt;/li&gt;
&lt;li&gt;Rate-limit and validate the internal lookup endpoint, since it is reachable from the public internet.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Observability and testing
&lt;/h2&gt;

&lt;p&gt;Redirects fail silently, so make them visible:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Log every redirect served (source, destination, status) and aggregate into &lt;code&gt;hit_count&lt;/code&gt; and &lt;code&gt;last_hit_at&lt;/code&gt; in a background job.&lt;/li&gt;
&lt;li&gt;Log 404s with their path and referrer, and review the top entries weekly after a launch. They are the rules you forgot.&lt;/li&gt;
&lt;li&gt;Add a CI test that replays the full URL inventory against a preview deployment and asserts one hop, the expected status, and a final 200:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="nv"&gt;IFS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;, &lt;span class="nb"&gt;read&lt;/span&gt; -r src expected&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="nv"&gt;out&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$(&lt;/span&gt;curl -s -o /dev/null -w &lt;span class="s1"&gt;'%{http_code} %{redirect_url}'&lt;/span&gt; &lt;span class="s2"&gt;"$PREVIEW$src"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
  &lt;span class="nv"&gt;code&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;${out%% *}&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="nv"&gt;loc&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;${out#* }&lt;/span&gt;
  &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="s2"&gt;"$code"&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"301"&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="s2"&gt;"$loc"&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"$PREVIEW$expected"&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"FAIL $src -&amp;gt; $out"&lt;/span&gt;
  &lt;span class="nv"&gt;final&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$(&lt;/span&gt;curl -s -o /dev/null -w &lt;span class="s1"&gt;'%{http_code}'&lt;/span&gt; &lt;span class="s2"&gt;"$loc"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
  &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="s2"&gt;"$final"&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"200"&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"FAIL final $loc -&amp;gt; $final"&lt;/span&gt;
&lt;span class="k"&gt;done&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; redirects.csv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Checklist
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Redirects stored in a database or versioned file with metadata, not hand-edited config.&lt;/li&gt;
&lt;li&gt;One normalization function shared by writes and lookups.&lt;/li&gt;
&lt;li&gt;Loops, chains, self-redirects and dead destinations rejected before deploy.&lt;/li&gt;
&lt;li&gt;Chains flattened so every request is a single hop.&lt;/li&gt;
&lt;li&gt;Exact rules resolved at the edge or from a compiled map; patterns indexed by prefix.&lt;/li&gt;
&lt;li&gt;Middleware lookups short-circuited with a Bloom filter or cache.&lt;/li&gt;
&lt;li&gt;Fail-open behavior and a snapshot fallback defined.&lt;/li&gt;
&lt;li&gt;Destination host allowlist and authenticated admin API.&lt;/li&gt;
&lt;li&gt;Hit and 404 logging in place from launch day.&lt;/li&gt;
&lt;li&gt;Replay test of the full URL inventory in CI.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Common mistakes
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Putting thousands of rules in framework config and discovering the platform limit during the launch build.&lt;/li&gt;
&lt;li&gt;Using temporary status codes (302/307) for permanent moves because they were the default in a helper function.&lt;/li&gt;
&lt;li&gt;Dropping query strings, which breaks campaign tracking on old links.&lt;/li&gt;
&lt;li&gt;Updating hit counters synchronously in the request path.&lt;/li&gt;
&lt;li&gt;Deleting old rules after the migration "settles" without checking whether they still get traffic.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you are planning a replatform and want the redirect layer designed alongside the new site, that is the kind of work we do in our &lt;a href="https://disruptivestudio.com/websites" rel="noopener noreferrer"&gt;website design and development projects&lt;/a&gt;, and for larger systems with their own admin tools, through &lt;a href="https://disruptivestudio.com/custom-software" rel="noopener noreferrer"&gt;custom software development&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Frequently asked questions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  How many redirects can I put in next.config.js?
&lt;/h3&gt;

&lt;p&gt;It depends on the hosting platform. On Vercel, the Next.js docs cite a limit of 1,024 redirects, and other route types count toward the same cap. Beyond roughly a thousand, move to middleware with a data store.&lt;/p&gt;

&lt;h3&gt;
  
  
  Should redirects run at the CDN or in the application?
&lt;/h3&gt;

&lt;p&gt;Exact, stable migration rules are best resolved as early as possible, at the edge or from a compiled map. Rules that editors change often fit better in middleware backed by a key-value store, so changes apply without a deploy.&lt;/p&gt;

&lt;h3&gt;
  
  
  Does a Bloom filter replace the redirects database?
&lt;/h3&gt;

&lt;p&gt;No. It only answers "definitely not here" or "maybe here". You still need the real lookup for positives, and it does not help with wildcard or regex rules.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally published on &lt;a href="https://disruptivestudio.com/blog/managing-redirects-at-scale" rel="noopener noreferrer"&gt;Disruptive Studio&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>seo</category>
      <category>nextjs</category>
      <category>architecture</category>
    </item>
    <item>
      <title>Software Project Handover Checklist: Engineering a Clean Exit</title>
      <dc:creator>Daniel Cruz</dc:creator>
      <pubDate>Wed, 11 Nov 2026 17:01:43 +0000</pubDate>
      <link>https://dev.to/daniel_cruz_aaf7dd957e107/software-project-handover-checklist-engineering-a-clean-exit-2pj4</link>
      <guid>https://dev.to/daniel_cruz_aaf7dd957e107/software-project-handover-checklist-engineering-a-clean-exit-2pj4</guid>
      <description>&lt;p&gt;Most teams think about the software project handover checklist at the worst moment: the week a vendor relationship ends, a contractor leaves, or an acquisition asks who owns what. By then, the expensive problems are already baked in: production running in a cloud account nobody on your side can log into, a deploy that only works from one person's laptop, secrets committed three years ago. This article treats handover as an engineering problem you design for from the first commit, so that any competent team could take over the system tomorrow. If you are still at the stage of picking a vendor, start with our guide on &lt;a href="https://disruptivestudio.com/blog/choose-software-development-company" rel="noopener noreferrer"&gt;how to choose a software development company&lt;/a&gt;; this piece is about the system you build once you have one.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why handover is an architecture problem, not paperwork
&lt;/h2&gt;

&lt;p&gt;A handover document written at the end of a project describes the system as someone remembers it. A handover built into the repository describes the system as it actually runs, because CI checks it every week. The difference shows up in four places:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Ownership&lt;/strong&gt;: who holds the root credentials for code, cloud, DNS, app stores and payment providers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reproducibility&lt;/strong&gt;: whether a new engineer can go from clone to running tests to a staging deploy using only what is in the repo.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;State&lt;/strong&gt;: whether the infrastructure and the data can be rebuilt or restored without tribal knowledge.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reasoning&lt;/strong&gt;: whether the next team knows why things are the way they are, so they do not undo a decision that was made for a good reason.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each of these can be verified by a machine. That is the core idea: turn the checklist into checks.&lt;/p&gt;

&lt;h2&gt;
  
  
  The software project handover checklist, layer by layer
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Ownership: organizations, not people
&lt;/h3&gt;

&lt;p&gt;Every asset should belong to an organization account the client controls, with the vendor invited as a member with scoped, revocable access. The common anti-pattern is the opposite: the vendor creates the repository, cloud account or app store listing under its own account and promises to transfer it later.&lt;/p&gt;

&lt;p&gt;Transfers are possible, but they have side effects you should know about before you rely on them. According to &lt;a href="https://docs.github.com/en/repositories/creating-and-managing-repositories/transferring-a-repository" rel="noopener noreferrer"&gt;GitHub's documentation on transferring a repository&lt;/a&gt;, moving a private repository to a GitHub Free user or organization means it loses features like protected branches and GitHub Pages, and in some cases GitHub permanently retires the old owner/name combination. Cloud accounts, mobile app listings and payment processor accounts each have their own, usually slower, transfer procedures. Creating them under the right owner from day one is cheaper than any migration.&lt;/p&gt;

&lt;p&gt;Make ownership explicit and reviewable by keeping a manifest at the repository root:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="c1"&gt;# ownership.yaml - reviewed at every release
&lt;/span&gt;&lt;span class="na"&gt;assets&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;source code&lt;/span&gt;
    &lt;span class="na"&gt;system&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;GitHub organization acme-co&lt;/span&gt;
    &lt;span class="na"&gt;owner&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;client&lt;/span&gt;
    &lt;span class="na"&gt;admins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;cto@acme.example&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;ops@acme.example&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;production cloud&lt;/span&gt;
    &lt;span class="na"&gt;system&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;cloud account (prod)&lt;/span&gt;
    &lt;span class="na"&gt;owner&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;client&lt;/span&gt;
    &lt;span class="na"&gt;billing&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;client&lt;/span&gt;
    &lt;span class="na"&gt;vendor_access&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;scoped role, time-boxed&lt;/span&gt;
  &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;domain and DNS&lt;/span&gt;
    &lt;span class="na"&gt;system&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;registrar + DNS provider&lt;/span&gt;
    &lt;span class="na"&gt;owner&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;client&lt;/span&gt;
    &lt;span class="na"&gt;renewal&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;auto, client billing&lt;/span&gt;
  &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;payments&lt;/span&gt;
    &lt;span class="na"&gt;system&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;payment processor account&lt;/span&gt;
    &lt;span class="na"&gt;owner&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;client&lt;/span&gt;
    &lt;span class="na"&gt;vendor_access&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;restricted API key&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The rule of thumb: at least two client-side people have admin on every line, and no line lists a personal email from the vendor as the only admin.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Reproducible builds: one entry point per task
&lt;/h3&gt;

&lt;p&gt;If setup requires a wiki page with fifteen manual steps, the steps will drift away from reality. A simple, durable pattern is a fixed set of scripts that every project exposes, popularized by GitHub's &lt;a href="https://github.com/github/scripts-to-rule-them-all" rel="noopener noreferrer"&gt;Scripts to Rule Them All&lt;/a&gt; convention:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;script/bootstrap   # install toolchain and dependencies from lockfiles
script/setup       # create local database, load anonymized seed data
script/test        # run the full test suite
script/server      # start the app locally
script/deploy      # deploy to an environment: script/deploy staging
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Combine it with committed lockfiles for every package manager, pinned runtime versions (a version file or a &lt;a href="https://containers.dev/" rel="noopener noreferrer"&gt;dev container&lt;/a&gt; definition), and an &lt;code&gt;.env.example&lt;/code&gt; that lists every variable the app reads, with a comment explaining where the real value lives. The test is blunt: CI must be able to run &lt;code&gt;script/bootstrap&lt;/code&gt; and &lt;code&gt;script/test&lt;/code&gt; on a clean machine. If CI can do it, a new team can.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Infrastructure as code, and the drift problem
&lt;/h3&gt;

&lt;p&gt;Infrastructure created by clicking in a console is the most common thing that fails to survive a handover. There are three realistic options, each with trade-offs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Infrastructure as code (Terraform, OpenTofu, Pulumi, CloudFormation)&lt;/strong&gt;: everything is reviewable and rebuildable, but it requires discipline and someone who can read it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Managed platforms (PaaS, serverless hosting)&lt;/strong&gt;: far less infrastructure to hand over, but configuration still lives in dashboards and you accept the platform's limits and pricing.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Documented click-ops&lt;/strong&gt;: fastest to start, and the hardest to verify. Acceptable only for tiny systems, with screenshots and an export of settings.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Whichever you choose, the failure mode is the same: someone changes production by hand and the code stops describing reality. Detect drift on a schedule. With Terraform, &lt;code&gt;plan -detailed-exitcode&lt;/code&gt; returns 0 when there are no changes, 1 on error and 2 when the live infrastructure differs from the code (see the &lt;a href="https://developer.hashicorp.com/terraform/cli/commands/plan" rel="noopener noreferrer"&gt;terraform plan docs&lt;/a&gt;):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/usr/bin/env bash&lt;/span&gt;
&lt;span class="c"&gt;# scripts/check-drift.sh&lt;/span&gt;
&lt;span class="nb"&gt;set&lt;/span&gt; +e
terraform plan -detailed-exitcode -input=false -lock=false
&lt;span class="nv"&gt;code&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$?&lt;/span&gt;
&lt;span class="nb"&gt;set&lt;/span&gt; -e
&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nv"&gt;$code&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt;
  0&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s1"&gt;'No drift'&lt;/span&gt; &lt;span class="o"&gt;;&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  2&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s1"&gt;'Drift: live infrastructure differs from code'&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="nb"&gt;exit&lt;/span&gt; 1 &lt;span class="o"&gt;;&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  *&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s1"&gt;'Plan failed'&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="nb"&gt;exit&lt;/span&gt; &lt;span class="nv"&gt;$code&lt;/span&gt; &lt;span class="o"&gt;;&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;esac&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you inherit console-built resources, bring them under management with Terraform's &lt;a href="https://developer.hashicorp.com/terraform/language/import" rel="noopener noreferrer"&gt;import blocks&lt;/a&gt; before the handover, not after, while the people who created them can still answer questions.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Secrets: inventory, scan, rotate
&lt;/h3&gt;

&lt;p&gt;Assume every secret the outgoing team has seen is compromised in the sense that it is no longer exclusively yours. That leads to three steps.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Inventory.&lt;/strong&gt; Keep a list of every secret, where it is stored and how to rotate it. The rotation procedure is the part that is always missing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;PAYMENTS_SECRET_KEY&lt;/span&gt;
  &lt;span class="na"&gt;stored_in&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;secrets manager, path prod/payments&lt;/span&gt;
  &lt;span class="na"&gt;used_by&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;api&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;worker&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="na"&gt;rotate&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;roll key in provider dashboard, update secret, redeploy api and worker&lt;/span&gt;
  &lt;span class="na"&gt;last_rotated&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;filled in at handover&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Scan the full history.&lt;/strong&gt; Deleting a secret in a later commit does not remove it from Git history. Tools like &lt;a href="https://github.com/gitleaks/gitleaks" rel="noopener noreferrer"&gt;gitleaks&lt;/a&gt; scan every commit on every branch; check the README for the current CLI syntax, since subcommands have changed between versions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# scan all commits on all branches, not just the working tree&lt;/span&gt;
gitleaks detect --source . --log-opts=&lt;span class="s1"&gt;'--all'&lt;/span&gt; --report-path gitleaks.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Rotate.&lt;/strong&gt; On handover day, rotate everything on the inventory, revoke vendor access tokens, SSH keys and personal access tokens, and remove vendor members from the organizations. Do it in a single planned window with the new team on call, because rotation is when hidden dependencies break.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Data: migrations in the repo, restores you have tested
&lt;/h3&gt;

&lt;p&gt;Schema changes belong in versioned migrations committed with the code, never applied by hand. Backups only count if a restore has been tested, so make the drill a script:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# restore the latest snapshot into a throwaway database and smoke-test it&lt;/span&gt;
./scripts/restore-backup.sh --snapshot latest --target handover-drill
&lt;span class="nv"&gt;DATABASE_URL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$DRILL_URL&lt;/span&gt; ./script/test --smoke
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add a short data inventory: which tables hold personal data, which third parties receive copies of it, and how long it is retained. The receiving team needs that to answer customer and regulatory requests on their first week.&lt;/p&gt;

&lt;h3&gt;
  
  
  6. Knowledge: ADRs and runbooks next to the code
&lt;/h3&gt;

&lt;p&gt;Architecture Decision Records are short Markdown files that capture one decision each; &lt;a href="https://adr.github.io/" rel="noopener noreferrer"&gt;adr.github.io&lt;/a&gt; collects templates and tooling. They answer the question new teams ask most: why is it built like this?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;&lt;span class="gu"&gt;# ADR-007: Generate invoices in a background job queue
&lt;/span&gt;Status: accepted
Context: invoice PDFs took long enough to block web requests at month end.
Decision: enqueue generation; email the PDF when ready.
Consequences: needs a worker process and retry policy; invoices are not instant.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pair them with a runbook covering the operations someone will need at 2 a.m.: how to deploy, roll back, restart workers, rotate a key, restore a backup, and where logs and alerts live. Each runbook entry should point at a script, not describe a manual procedure.&lt;/p&gt;

&lt;h2&gt;
  
  
  Make the checklist executable
&lt;/h2&gt;

&lt;p&gt;The handover checks above are worth running on a schedule for the whole life of the project, not once at the end. A weekly CI job that behaves like a stranger arriving at the repo catches erosion early:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;handover-check&lt;/span&gt;
&lt;span class="na"&gt;on&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;schedule&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;cron&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'0 6 * * 1'&lt;/span&gt;
  &lt;span class="na"&gt;workflow_dispatch&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="na"&gt;jobs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;stranger-test&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;runs-on&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ubuntu-latest&lt;/span&gt;
    &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/checkout@v4&lt;/span&gt;
        &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;fetch-depth&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
      &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;script/bootstrap&lt;/span&gt;
      &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;script/test&lt;/span&gt;
      &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;scripts/check-drift.sh&lt;/span&gt;
      &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;scripts/check-docs.sh&lt;/span&gt;
      &lt;span class="c1"&gt;# install gitleaks first, then:
&lt;/span&gt;      &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;gitleaks detect --source . --log-opts='--all'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The docs check can be as simple as failing when required files disappear:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/usr/bin/env bash&lt;/span&gt;
&lt;span class="c"&gt;# scripts/check-docs.sh&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; f &lt;span class="k"&gt;in&lt;/span&gt; README.md ownership.yaml .env.example docs/runbook.md docs/adr&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="o"&gt;[&lt;/span&gt; -e &lt;span class="nv"&gt;$f&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; missing: &lt;span class="nv"&gt;$f&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="nb"&gt;exit&lt;/span&gt; 1&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;done&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The final verification is human: before the outgoing team leaves, an engineer from the receiving team deploys a small change to production using only the repository and the runbook, while the outgoing team watches but does not touch the keyboard. Every question they ask is a gap to fix in the repo.&lt;/p&gt;

&lt;h2&gt;
  
  
  Trade-offs worth deciding early
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Vendor libraries and licenses.&lt;/strong&gt; If the code depends on the vendor's private packages or internal frameworks, the handover is incomplete. Either they are delivered with a license that lets you keep using and modifying them, or they are replaced. Generate a dependency list with licenses (an SBOM in SPDX or CycloneDX format works) and review it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Monorepo versus many repositories.&lt;/strong&gt; A monorepo is easier to hand over as a single unit; many repositories need an index listing each one, its purpose and its deploy target.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Observability vendor accounts.&lt;/strong&gt; Logging, error tracking and uptime tools are often signed up under the vendor. Treat them like any other asset in the ownership manifest, or you lose your history on handover day.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When we build &lt;a href="https://disruptivestudio.com/custom-software" rel="noopener noreferrer"&gt;custom software for clients&lt;/a&gt;, these are decisions we prefer to settle in the first week, because each one is cheap at the start and costly at the end. The same applies to marketing sites: domains, DNS and CMS accounts are the assets most often left in the wrong hands, which is why our &lt;a href="https://disruptivestudio.com/websites" rel="noopener noreferrer"&gt;website projects&lt;/a&gt; start with the client owning the registrar and hosting accounts.&lt;/p&gt;

&lt;h2&gt;
  
  
  Common mistakes
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Accepting a ZIP file or a fresh repository without history. You lose the context of every change and any chance to scan old commits.&lt;/li&gt;
&lt;li&gt;Rotating secrets before mapping them, then discovering a cron job on a forgotten server that used the old key.&lt;/li&gt;
&lt;li&gt;Treating backups as done because the provider says they are enabled, without ever restoring one.&lt;/li&gt;
&lt;li&gt;Writing the handover document in the last week, from memory.&lt;/li&gt;
&lt;li&gt;Removing vendor access before the receiving team has done a production deploy on its own.&lt;/li&gt;
&lt;li&gt;Forgetting non-code assets: app store accounts, push notification certificates, email sending domains, analytics properties.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The checklist
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Every asset is listed in &lt;code&gt;ownership.yaml&lt;/code&gt; with a client-side owner and two admins.&lt;/li&gt;
&lt;li&gt;Repositories live in a client organization on a plan that keeps branch protection.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;script/bootstrap&lt;/code&gt; and &lt;code&gt;script/test&lt;/code&gt; pass on a clean CI runner.&lt;/li&gt;
&lt;li&gt;Runtime versions and dependencies are pinned and lockfiles are committed.&lt;/li&gt;
&lt;li&gt;Infrastructure is in code and a scheduled drift check passes.&lt;/li&gt;
&lt;li&gt;The full Git history has been scanned for secrets and findings resolved.&lt;/li&gt;
&lt;li&gt;A secrets inventory with rotation steps exists, and every secret is rotated at handover.&lt;/li&gt;
&lt;li&gt;Schema migrations are versioned, and a backup restore drill has passed.&lt;/li&gt;
&lt;li&gt;ADRs cover the major decisions; a runbook covers deploy, rollback and restore.&lt;/li&gt;
&lt;li&gt;A dependency and license list has been reviewed, with no unlicensed private vendor code.&lt;/li&gt;
&lt;li&gt;The receiving team has deployed to production unassisted.&lt;/li&gt;
&lt;li&gt;Vendor access is revoked last, after the steps above.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Frequently asked questions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  When should a software project handover checklist be started?
&lt;/h3&gt;

&lt;p&gt;On day one. Most items, such as account ownership, lockfiles and infrastructure as code, cost almost nothing at the start and become migrations later. Running the checks in CI throughout the project means the final handover is a verification, not a rescue.&lt;/p&gt;

&lt;h3&gt;
  
  
  Is infrastructure as code required for a small project?
&lt;/h3&gt;

&lt;p&gt;Not always. A small app on a managed platform may have little infrastructure to describe. What is required is that every setting can be found, exported and reproduced, and that the accounts belong to the client.&lt;/p&gt;

&lt;h3&gt;
  
  
  What should we do if the vendor already owns the accounts?
&lt;/h3&gt;

&lt;p&gt;Inventory everything first, then plan transfers one asset at a time, reading each provider's transfer documentation for side effects. Rotate secrets and revoke access only after the receiving team has confirmed it can deploy and operate the system.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally published on &lt;a href="https://disruptivestudio.com/blog/software-project-handover-checklist" rel="noopener noreferrer"&gt;Disruptive Studio&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>devops</category>
      <category>security</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Stopping Black Friday Oversells: A Stock Reservation Data Model</title>
      <dc:creator>Daniel Cruz</dc:creator>
      <pubDate>Tue, 10 Nov 2026 03:45:39 +0000</pubDate>
      <link>https://dev.to/daniel_cruz_aaf7dd957e107/stopping-black-friday-oversells-a-stock-reservation-data-model-5do7</link>
      <guid>https://dev.to/daniel_cruz_aaf7dd957e107/stopping-black-friday-oversells-a-stock-reservation-data-model-5do7</guid>
      <description>&lt;h2&gt;
  
  
  The bug that only shows up during the sale
&lt;/h2&gt;

&lt;p&gt;Most checkout flows work fine on a normal Tuesday. You read the stock count, check that it is above zero, take payment, and decrement it. The flaw stays hidden until many buyers go after the same discounted item in the same few seconds. Then two requests both read &lt;code&gt;stock = 1&lt;/code&gt;, both pass the check, and both charge a card for an item that only exists once.&lt;/p&gt;

&lt;p&gt;This post walks through a data model that closes that gap. It uses short-lived reservations, atomic updates, and idempotent payment handling. The examples use PostgreSQL syntax, but the ideas carry over to any store that supports transactions or atomic conditional writes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why check-then-write fails
&lt;/h2&gt;

&lt;p&gt;The naive version looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- Request A and Request B run this at the same time&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;stock&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;products&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;-- both see 1&lt;/span&gt;
&lt;span class="c1"&gt;-- application code: if stock &amp;gt; 0 then...&lt;/span&gt;
&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;products&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;stock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stock&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;-- both succeed, stock = -1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The read and the write are separate steps, and nothing ties them together. Wrapping them in a transaction does not fix this by itself under the default isolation level. The fix is to make the condition part of the write.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Make the decrement conditional and atomic
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;products&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;available&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;available&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;qty&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;product_id&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;available&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;qty&lt;/span&gt;
&lt;span class="k"&gt;RETURNING&lt;/span&gt; &lt;span class="n"&gt;available&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If no row comes back, the item is sold out and the request fails cleanly. The database serializes the row update, so two buyers cannot both take the last unit. Add a &lt;code&gt;CHECK (available &amp;gt;= 0)&lt;/code&gt; constraint as a backstop, so a bug elsewhere fails loudly instead of quietly producing negative stock.&lt;/p&gt;

&lt;p&gt;That solves the race. It does not solve a second problem: stock taken by people who never finish paying.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: Separate "available" from "sold" with reservations
&lt;/h2&gt;

&lt;p&gt;If you decrement at add-to-cart, abandoned carts lock up inventory for the whole sale. If you decrement only at payment capture, you are back to overselling. A reservation table sits between those two options:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;stock_reservations&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt;           &lt;span class="kt"&gt;uuid&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;product_id&lt;/span&gt;   &lt;span class="kt"&gt;bigint&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;REFERENCES&lt;/span&gt; &lt;span class="nf"&gt;products&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;cart_id&lt;/span&gt;      &lt;span class="kt"&gt;uuid&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;qty&lt;/span&gt;          &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;CHECK&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;qty&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;status&lt;/span&gt;       &lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;CHECK&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'held'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'committed'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'released'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;expires_at&lt;/span&gt;   &lt;span class="kt"&gt;timestamptz&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;created_at&lt;/span&gt;   &lt;span class="kt"&gt;timestamptz&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;stock_reservations&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;expires_at&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The lifecycle:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Hold&lt;/strong&gt; when the buyer starts checkout (not at add-to-cart). Run the conditional decrement on &lt;code&gt;products.available&lt;/code&gt; and insert a &lt;code&gt;held&lt;/code&gt; row with an expiry, in one transaction.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Commit&lt;/strong&gt; when payment succeeds. Flip the status to &lt;code&gt;committed&lt;/code&gt;. Stock is already gone from &lt;code&gt;available&lt;/code&gt;, so nothing else changes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Release&lt;/strong&gt; when the hold expires or payment fails. Flip it to &lt;code&gt;released&lt;/code&gt; and add the quantity back to &lt;code&gt;available&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Pick the hold duration based on how long your real checkout takes, including 3-D Secure or bank redirects. Show the buyer a timer so the expiry does not surprise them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: Release expired holds safely
&lt;/h2&gt;

&lt;p&gt;A background worker reclaims expired reservations. Several workers can run it at once without fighting if each one skips rows that another has locked:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="n"&gt;expired&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;product_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;qty&lt;/span&gt;
  &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;stock_reservations&lt;/span&gt;
  &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'held'&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;expires_at&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;expires_at&lt;/span&gt;
  &lt;span class="k"&gt;LIMIT&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;
  &lt;span class="k"&gt;FOR&lt;/span&gt; &lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="no"&gt;SKIP&lt;/span&gt; &lt;span class="no"&gt;LOCKED&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;released&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;stock_reservations&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;
  &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'released'&lt;/span&gt;
  &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;expired&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;
  &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;
  &lt;span class="k"&gt;RETURNING&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;product_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;qty&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;products&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;available&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;available&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;total&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;product_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;qty&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;released&lt;/span&gt; &lt;span class="k"&gt;GROUP&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;product_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;product_id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The locking clause is documented in the &lt;a href="https://www.postgresql.org/docs/current/sql-select.html" rel="noopener noreferrer"&gt;PostgreSQL SELECT reference&lt;/a&gt;. Because the status filter is &lt;code&gt;held&lt;/code&gt;, a reservation that was committed a moment earlier is never released by mistake.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: Make payment confirmation idempotent
&lt;/h2&gt;

&lt;p&gt;The commit step is triggered by a payment event, and payment events are messy. Buyers double-click, mobile networks retry, and webhooks can arrive more than once or out of order. Two rules keep this under control:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Send an idempotency key on the payment request.&lt;/strong&gt; Use the reservation or order ID, so a retried request does not create a second charge. Stripe, for example, documents this pattern in its &lt;a href="https://docs.stripe.com/api/idempotent_requests" rel="noopener noreferrer"&gt;idempotent requests guide&lt;/a&gt;. Check your own provider's equivalent.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Make the commit a conditional state transition.&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;stock_reservations&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'committed'&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;reservation_id&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'held'&lt;/span&gt;
&lt;span class="k"&gt;RETURNING&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the row comes back, this is the first time you have processed the event. If it does not, either the event was already handled (do nothing) or the hold expired before payment landed. That last case is the one teams forget.&lt;/p&gt;

&lt;h2&gt;
  
  
  The edge case: paid after the hold expired
&lt;/h2&gt;

&lt;p&gt;A buyer who stalls on a bank redirect can pay after the worker has released their units. Decide your policy before the sale:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;on payment_succeeded(reservation):
  if commit(reservation) succeeds: fulfil
  else if reservation.status == 'released':
      try conditional decrement on products.available
      if it succeeds: create committed reservation, fulfil
      else: refund automatically, notify buyer, log for support
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Automatic refunds with a clear message are better than a support team finding the oversell by hand days later.&lt;/p&gt;

&lt;h2&gt;
  
  
  Keeping hot rows from becoming a bottleneck
&lt;/h2&gt;

&lt;p&gt;When one doorbuster SKU gets most of the traffic, every checkout queues on the same row. Options, from simplest to most complex:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Keep the transaction that touches &lt;code&gt;products&lt;/code&gt; as short as possible. No network calls inside it.&lt;/li&gt;
&lt;li&gt;Split stock for a hot SKU into several bucket rows and decrement a random bucket with stock left.&lt;/li&gt;
&lt;li&gt;Move the counter for that SKU into an atomic in-memory counter (for example, a Redis &lt;code&gt;DECRBY&lt;/code&gt; guarded by a script) and reconcile it to the database. This is fast, but now you have two sources of truth to reconcile.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Start with the first option and measure before you reach for the third.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pre-sale verification list
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;A concurrency test that fires many parallel checkouts at a SKU with stock of 1 and asserts exactly one success.&lt;/li&gt;
&lt;li&gt;A replay test that delivers the same payment webhook several times.&lt;/li&gt;
&lt;li&gt;A test for payment arriving after expiry.&lt;/li&gt;
&lt;li&gt;A dashboard for &lt;code&gt;available&lt;/code&gt;, &lt;code&gt;held&lt;/code&gt; and &lt;code&gt;committed&lt;/code&gt; per hot SKU.&lt;/li&gt;
&lt;li&gt;An alert if the expiry worker falls behind.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Inventory is only one part of getting a store ready for a sale. Traffic, speed, payments and support all need attention too. The team at &lt;a href="https://disruptivestudio.com/blog/prepare-website-black-friday-checklist" rel="noopener noreferrer"&gt;Disruptive Studio&lt;/a&gt; covers the broader checklist. If your stock counts hold up under concurrent checkouts, you avoid the most expensive kind of Black Friday bug: one that costs you money and annoys the customer.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>database</category>
      <category>ecommerce</category>
      <category>architecture</category>
    </item>
  </channel>
</rss>
