<mxfile host="thecodeclouds.com" agent="hand-built" version="24.0.0" type="device">
<diagram id="idempotency" name="3. Idempotent payment on retry"><mxGraphModel dx="1400" dy="900" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="1500" pageHeight="900" math="0" shadow="0"><root><mxCell id="0"/><mxCell id="1" parent="0"/><mxCell id="c1" value="Scenario: the shopper taps Pay, the network drops the response, the app retries" style="text;html=1;whiteSpace=wrap;align=left;verticalAlign=top;fontSize=18;fontColor=#232F3E;fontStyle=1;spacing=0;" vertex="1" parent="1"><mxGeometry x="20" y="16" width="1300" height="24" as="geometry"/></mxCell>
<mxCell id="c2" value="Without a key this is two charges. With one, the retry gets the first answer back and the PSP is never called twice." style="text;html=1;whiteSpace=wrap;align=left;verticalAlign=top;fontSize=12;fontColor=#5F6368;fontStyle=0;spacing=0;" vertex="1" parent="1"><mxGeometry x="20" y="44" width="1300" height="18" as="geometry"/></mxCell>
<mxCell id="app" value="&lt;b&gt;Shopper app&lt;/b&gt;" style="rounded=1;arcSize=10;html=1;whiteSpace=wrap;strokeColor=#5F6368;fillColor=#F1F3F4;strokeWidth=1.5;dashed=0;fontSize=12;fontColor=#202124;align=center;verticalAlign=middle;" vertex="1" parent="1"><mxGeometry x="15" y="80" width="190" height="50" as="geometry"/></mxCell>
<mxCell id="c3" style="edgeStyle=none;html=1;rounded=0;strokeColor=#BDC1C6;strokeWidth=1.5;dashed=1;startArrow=none;startFill=1;endArrow=none;endFill=1;endSize=4;startSize=4;" edge="1" parent="1"><mxGeometry relative="1" as="geometry"><mxPoint x="110" y="130" as="sourcePoint"/><mxPoint x="110" y="860" as="targetPoint"/></mxGeometry></mxCell>
<mxCell id="pay" value="&lt;b&gt;Payment service&lt;/b&gt;" style="rounded=1;arcSize=10;html=1;whiteSpace=wrap;strokeColor=#1A73E8;fillColor=#E8F0FE;strokeWidth=1.5;dashed=0;fontSize=12;fontColor=#202124;align=center;verticalAlign=middle;" vertex="1" parent="1"><mxGeometry x="325" y="80" width="190" height="50" as="geometry"/></mxCell>
<mxCell id="c4" style="edgeStyle=none;html=1;rounded=0;strokeColor=#BDC1C6;strokeWidth=1.5;dashed=1;startArrow=none;startFill=1;endArrow=none;endFill=1;endSize=4;startSize=4;" edge="1" parent="1"><mxGeometry relative="1" as="geometry"><mxPoint x="420" y="130" as="sourcePoint"/><mxPoint x="420" y="860" as="targetPoint"/></mxGeometry></mxCell>
<mxCell id="db" value='&lt;b&gt;Payments DB&lt;/b&gt;&lt;br&gt;&lt;font style="font-size:10px" color="#5F6368"&gt;idempotency_keys&lt;/font&gt;' style="rounded=1;arcSize=10;html=1;whiteSpace=wrap;strokeColor=#1E8E3E;fillColor=#E6F4EA;strokeWidth=1.5;dashed=0;fontSize=12;fontColor=#202124;align=center;verticalAlign=middle;" vertex="1" parent="1"><mxGeometry x="635" y="80" width="190" height="50" as="geometry"/></mxCell>
<mxCell id="c5" style="edgeStyle=none;html=1;rounded=0;strokeColor=#BDC1C6;strokeWidth=1.5;dashed=1;startArrow=none;startFill=1;endArrow=none;endFill=1;endSize=4;startSize=4;" edge="1" parent="1"><mxGeometry relative="1" as="geometry"><mxPoint x="730" y="130" as="sourcePoint"/><mxPoint x="730" y="860" as="targetPoint"/></mxGeometry></mxCell>
<mxCell id="psp" value="&lt;b&gt;Payment gateway (PSP)&lt;/b&gt;" style="rounded=1;arcSize=10;html=1;whiteSpace=wrap;strokeColor=#8430CE;fillColor=#F3E8FD;strokeWidth=1.5;dashed=0;fontSize=12;fontColor=#202124;align=center;verticalAlign=middle;" vertex="1" parent="1"><mxGeometry x="945" y="80" width="190" height="50" as="geometry"/></mxCell>
<mxCell id="c6" style="edgeStyle=none;html=1;rounded=0;strokeColor=#BDC1C6;strokeWidth=1.5;dashed=1;startArrow=none;startFill=1;endArrow=none;endFill=1;endSize=4;startSize=4;" edge="1" parent="1"><mxGeometry relative="1" as="geometry"><mxPoint x="1040" y="130" as="sourcePoint"/><mxPoint x="1040" y="860" as="targetPoint"/></mxGeometry></mxCell>
<mxCell id="c7" value="Attempt 1" style="text;html=1;whiteSpace=wrap;align=left;verticalAlign=top;fontSize=12;fontColor=#202124;fontStyle=1;spacing=0;" vertex="1" parent="1"><mxGeometry x="20" y="150" width="80" height="18" as="geometry"/></mxCell>
<mxCell id="c8" style="edgeStyle=none;html=1;rounded=0;strokeColor=#1A73E8;strokeWidth=2;dashed=0;startArrow=none;startFill=1;endArrow=block;endFill=1;endSize=4;startSize=4;" edge="1" parent="1"><mxGeometry relative="1" as="geometry"><mxPoint x="110" y="190" as="sourcePoint"/><mxPoint x="420" y="190" as="targetPoint"/></mxGeometry></mxCell>
<mxCell id="c9" value="POST /payments  Idempotency-Key: 7f3c-…  £79.99" style="text;html=1;whiteSpace=wrap;align=center;verticalAlign=top;fontSize=11;fontColor=#1A73E8;fontStyle=0;spacing=0;labelBackgroundColor=#FFFFFF;" vertex="1" parent="1"><mxGeometry x="120" y="158" width="290" height="17" as="geometry"/></mxCell>
<mxCell id="c10" style="edgeStyle=none;html=1;rounded=0;strokeColor=#1E8E3E;strokeWidth=2;dashed=0;startArrow=none;startFill=1;endArrow=block;endFill=1;endSize=4;startSize=4;" edge="1" parent="1"><mxGeometry relative="1" as="geometry"><mxPoint x="420" y="250" as="sourcePoint"/><mxPoint x="730" y="250" as="targetPoint"/></mxGeometry></mxCell>
<mxCell id="c11" value="INSERT key, request_hash, status=STARTED" style="text;html=1;whiteSpace=wrap;align=center;verticalAlign=top;fontSize=11;fontColor=#1E8E3E;fontStyle=0;spacing=0;labelBackgroundColor=#FFFFFF;" vertex="1" parent="1"><mxGeometry x="430" y="218" width="290" height="17" as="geometry"/></mxCell>
<mxCell id="c12" style="edgeStyle=none;html=1;rounded=0;strokeColor=#1E8E3E;strokeWidth=2;dashed=1;startArrow=none;startFill=1;endArrow=block;endFill=1;endSize=4;startSize=4;" edge="1" parent="1"><mxGeometry relative="1" as="geometry"><mxPoint x="730" y="300" as="sourcePoint"/><mxPoint x="420" y="300" as="targetPoint"/></mxGeometry></mxCell>
<mxCell id="c13" value="inserted (we own this key)" style="text;html=1;whiteSpace=wrap;align=center;verticalAlign=top;fontSize=11;fontColor=#1E8E3E;fontStyle=0;spacing=0;labelBackgroundColor=#FFFFFF;" vertex="1" parent="1"><mxGeometry x="430" y="268" width="290" height="17" as="geometry"/></mxCell>
<mxCell id="c14" style="edgeStyle=none;html=1;rounded=0;strokeColor=#8430CE;strokeWidth=2;dashed=0;startArrow=none;startFill=1;endArrow=block;endFill=1;endSize=4;startSize=4;" edge="1" parent="1"><mxGeometry relative="1" as="geometry"><mxPoint x="420" y="360" as="sourcePoint"/><mxPoint x="1040" y="360" as="targetPoint"/></mxGeometry></mxCell>
<mxCell id="c15" value="authorize £79.99, idempotency key 7f3c-…" style="text;html=1;whiteSpace=wrap;align=center;verticalAlign=top;fontSize=11;fontColor=#8430CE;fontStyle=0;spacing=0;labelBackgroundColor=#FFFFFF;" vertex="1" parent="1"><mxGeometry x="430" y="328" width="600" height="17" as="geometry"/></mxCell>
<mxCell id="c16" style="edgeStyle=none;html=1;rounded=0;strokeColor=#8430CE;strokeWidth=2;dashed=1;startArrow=none;startFill=1;endArrow=block;endFill=1;endSize=4;startSize=4;" edge="1" parent="1"><mxGeometry relative="1" as="geometry"><mxPoint x="1040" y="410" as="sourcePoint"/><mxPoint x="420" y="410" as="targetPoint"/></mxGeometry></mxCell>
<mxCell id="c17" value="AUTHORIZED, auth_id pi_91X" style="text;html=1;whiteSpace=wrap;align=center;verticalAlign=top;fontSize=11;fontColor=#8430CE;fontStyle=0;spacing=0;labelBackgroundColor=#FFFFFF;" vertex="1" parent="1"><mxGeometry x="430" y="378" width="600" height="17" as="geometry"/></mxCell>
<mxCell id="c18" style="edgeStyle=none;html=1;rounded=0;strokeColor=#1E8E3E;strokeWidth=2;dashed=0;startArrow=none;startFill=1;endArrow=block;endFill=1;endSize=4;startSize=4;" edge="1" parent="1"><mxGeometry relative="1" as="geometry"><mxPoint x="420" y="470" as="sourcePoint"/><mxPoint x="730" y="470" as="targetPoint"/></mxGeometry></mxCell>
<mxCell id="c19" value="UPDATE status=SUCCEEDED, response=…" style="text;html=1;whiteSpace=wrap;align=center;verticalAlign=top;fontSize=11;fontColor=#1E8E3E;fontStyle=0;spacing=0;labelBackgroundColor=#FFFFFF;" vertex="1" parent="1"><mxGeometry x="430" y="438" width="290" height="17" as="geometry"/></mxCell>
<mxCell id="c20" style="edgeStyle=none;html=1;rounded=0;strokeColor=#D93025;strokeWidth=2;dashed=1;startArrow=none;startFill=1;endArrow=none;endFill=1;endSize=4;startSize=4;" edge="1" parent="1"><mxGeometry relative="1" as="geometry"><mxPoint x="420" y="520" as="sourcePoint"/><mxPoint x="265" y="520" as="targetPoint"/></mxGeometry></mxCell>
<mxCell id="c21" value="✕" style="text;html=1;whiteSpace=wrap;align=left;verticalAlign=top;fontSize=18;fontColor=#D93025;fontStyle=1;spacing=0;" vertex="1" parent="1"><mxGeometry x="257" y="509" width="20" height="24" as="geometry"/></mxCell>
<mxCell id="c22" value="201 Created (lost: tunnel, app killed, LB timeout)" style="text;html=1;whiteSpace=wrap;align=center;verticalAlign=top;fontSize=11;fontColor=#D93025;fontStyle=0;spacing=0;labelBackgroundColor=#FFFFFF;" vertex="1" parent="1"><mxGeometry x="120" y="526" width="290" height="17" as="geometry"/></mxCell>
<mxCell id="c23" style="edgeStyle=none;html=1;rounded=0;strokeColor=#5F6368;strokeWidth=1;dashed=0;startArrow=none;startFill=1;endArrow=none;endFill=1;endSize=4;startSize=4;" edge="1" parent="1"><mxGeometry relative="1" as="geometry"><mxPoint x="20" y="570" as="sourcePoint"/><mxPoint x="1180" y="570" as="targetPoint"/></mxGeometry></mxCell>
<mxCell id="c24" value="Attempt 2 (app retries 3 s later with the SAME key)" style="text;html=1;whiteSpace=wrap;align=left;verticalAlign=top;fontSize=12;fontColor=#202124;fontStyle=1;spacing=0;" vertex="1" parent="1"><mxGeometry x="20" y="580" width="600" height="18" as="geometry"/></mxCell>
<mxCell id="c25" style="edgeStyle=none;html=1;rounded=0;strokeColor=#1A73E8;strokeWidth=2;dashed=0;startArrow=none;startFill=1;endArrow=block;endFill=1;endSize=4;startSize=4;" edge="1" parent="1"><mxGeometry relative="1" as="geometry"><mxPoint x="110" y="640" as="sourcePoint"/><mxPoint x="420" y="640" as="targetPoint"/></mxGeometry></mxCell>
<mxCell id="c26" value="POST /payments  Idempotency-Key: 7f3c-…  £79.99" style="text;html=1;whiteSpace=wrap;align=center;verticalAlign=top;fontSize=11;fontColor=#1A73E8;fontStyle=0;spacing=0;labelBackgroundColor=#FFFFFF;" vertex="1" parent="1"><mxGeometry x="120" y="608" width="290" height="17" as="geometry"/></mxCell>
<mxCell id="c27" style="edgeStyle=none;html=1;rounded=0;strokeColor=#1E8E3E;strokeWidth=2;dashed=0;startArrow=none;startFill=1;endArrow=block;endFill=1;endSize=4;startSize=4;" edge="1" parent="1"><mxGeometry relative="1" as="geometry"><mxPoint x="420" y="700" as="sourcePoint"/><mxPoint x="730" y="700" as="targetPoint"/></mxGeometry></mxCell>
<mxCell id="c28" value="INSERT … ON CONFLICT DO NOTHING → 0 rows" style="text;html=1;whiteSpace=wrap;align=center;verticalAlign=top;fontSize=11;fontColor=#1E8E3E;fontStyle=0;spacing=0;labelBackgroundColor=#FFFFFF;" vertex="1" parent="1"><mxGeometry x="430" y="668" width="290" height="17" as="geometry"/></mxCell>
<mxCell id="c29" style="edgeStyle=none;html=1;rounded=0;strokeColor=#1E8E3E;strokeWidth=2;dashed=1;startArrow=none;startFill=1;endArrow=block;endFill=1;endSize=4;startSize=4;" edge="1" parent="1"><mxGeometry relative="1" as="geometry"><mxPoint x="730" y="750" as="sourcePoint"/><mxPoint x="420" y="750" as="targetPoint"/></mxGeometry></mxCell>
<mxCell id="c30" value="existing row: SUCCEEDED, same request_hash" style="text;html=1;whiteSpace=wrap;align=center;verticalAlign=top;fontSize=11;fontColor=#1E8E3E;fontStyle=0;spacing=0;labelBackgroundColor=#FFFFFF;" vertex="1" parent="1"><mxGeometry x="430" y="718" width="290" height="17" as="geometry"/></mxCell>
<mxCell id="c31" style="edgeStyle=none;html=1;rounded=0;strokeColor=#1A73E8;strokeWidth=2;dashed=1;startArrow=none;startFill=1;endArrow=block;endFill=1;endSize=4;startSize=4;" edge="1" parent="1"><mxGeometry relative="1" as="geometry"><mxPoint x="420" y="810" as="sourcePoint"/><mxPoint x="110" y="810" as="targetPoint"/></mxGeometry></mxCell>
<mxCell id="c32" value="201 Created (stored response replayed)" style="text;html=1;whiteSpace=wrap;align=center;verticalAlign=top;fontSize=11;fontColor=#1A73E8;fontStyle=0;spacing=0;labelBackgroundColor=#FFFFFF;" vertex="1" parent="1"><mxGeometry x="120" y="778" width="290" height="17" as="geometry"/></mxCell>
<mxCell id="c33" value="PSP also dedupes on the key,&lt;br&gt;in case WE retry" style="text;html=1;whiteSpace=wrap;align=right;verticalAlign=top;fontSize=11;fontColor=#8430CE;fontStyle=0;spacing=0;labelBackgroundColor=#FFFFFF;" vertex="1" parent="1"><mxGeometry x="865" y="440" width="165" height="32" as="geometry"/></mxCell>
<mxCell id="nocall" value='&lt;b&gt;PSP not called.&lt;/b&gt;&lt;br&gt;&lt;font style="font-size:10px" color="#5F6368"&gt;One charge.&lt;/font&gt;' style="rounded=1;arcSize=10;html=1;whiteSpace=wrap;strokeColor=#1E8E3E;fillColor=#E6F4EA;strokeWidth=1.5;dashed=0;fontSize=12;fontColor=#202124;align=center;verticalAlign=middle;" vertex="1" parent="1"><mxGeometry x="950" y="690" width="180" height="56" as="geometry"/></mxCell>
<mxCell id="c34" value="&lt;b&gt;What the key row decides&lt;/b&gt;" style="rounded=1;arcSize=8;html=1;whiteSpace=wrap;strokeColor=#202124;fillColor=#FFFFFF;dashed=0;fontSize=12;fontStyle=0;fontColor=#202124;align=left;verticalAlign=top;spacingLeft=8;" vertex="1" parent="1"><mxGeometry x="1200" y="80" width="280" height="330" as="geometry"/></mxCell>
<mxCell id="c35" value="STARTED, under 30 s old" style="text;html=1;whiteSpace=wrap;align=left;verticalAlign=top;fontSize=12;fontColor=#E8710A;fontStyle=1;spacing=0;" vertex="1" parent="1"><mxGeometry x="1215" y="116" width="255" height="18" as="geometry"/></mxCell>
<mxCell id="c36" value='409, "still processing,&lt;br&gt;retry in 1 s"' style="text;html=1;whiteSpace=wrap;align=left;verticalAlign=top;fontSize=11;fontColor=#5F6368;fontStyle=0;spacing=0;" vertex="1" parent="1"><mxGeometry x="1215" y="136" width="255" height="32" as="geometry"/></mxCell>
<mxCell id="c37" value="STARTED, stale" style="text;html=1;whiteSpace=wrap;align=left;verticalAlign=top;fontSize=12;fontColor=#E8710A;fontStyle=1;spacing=0;" vertex="1" parent="1"><mxGeometry x="1215" y="188" width="255" height="18" as="geometry"/></mxCell>
<mxCell id="c38" value="take over: ask PSP&lt;br&gt;for key 7f3c-… first" style="text;html=1;whiteSpace=wrap;align=left;verticalAlign=top;fontSize=11;fontColor=#5F6368;fontStyle=0;spacing=0;" vertex="1" parent="1"><mxGeometry x="1215" y="208" width="255" height="32" as="geometry"/></mxCell>
<mxCell id="c39" value="SUCCEEDED / FAILED" style="text;html=1;whiteSpace=wrap;align=left;verticalAlign=top;fontSize=12;fontColor=#1E8E3E;fontStyle=1;spacing=0;" vertex="1" parent="1"><mxGeometry x="1215" y="260" width="255" height="18" as="geometry"/></mxCell>
<mxCell id="c40" value="replay stored&lt;br&gt;response as is" style="text;html=1;whiteSpace=wrap;align=left;verticalAlign=top;fontSize=11;fontColor=#5F6368;fontStyle=0;spacing=0;" vertex="1" parent="1"><mxGeometry x="1215" y="280" width="255" height="32" as="geometry"/></mxCell>
<mxCell id="c41" value="same key, different body" style="text;html=1;whiteSpace=wrap;align=left;verticalAlign=top;fontSize=12;fontColor=#D93025;fontStyle=1;spacing=0;" vertex="1" parent="1"><mxGeometry x="1215" y="332" width="255" height="18" as="geometry"/></mxCell>
<mxCell id="c42" value="422, key reuse&lt;br&gt;is a client bug" style="text;html=1;whiteSpace=wrap;align=left;verticalAlign=top;fontSize=11;fontColor=#5F6368;fontStyle=0;spacing=0;" vertex="1" parent="1"><mxGeometry x="1215" y="352" width="255" height="32" as="geometry"/></mxCell>
<mxCell id="c43" value="&lt;b&gt;Table&lt;/b&gt;" style="rounded=1;arcSize=8;html=1;whiteSpace=wrap;strokeColor=#1E8E3E;fillColor=#FFFFFF;dashed=0;fontSize=12;fontStyle=0;fontColor=#1E8E3E;align=left;verticalAlign=top;spacingLeft=8;" vertex="1" parent="1"><mxGeometry x="1200" y="430" width="280" height="210" as="geometry"/></mxCell>
<mxCell id="c44" value="CREATE TABLE idempotency_keys (" style="text;html=1;whiteSpace=wrap;align=left;verticalAlign=top;fontSize=10;fontColor=#202124;fontStyle=0;spacing=0;" vertex="1" parent="1"><mxGeometry x="1212" y="458" width="265" height="16" as="geometry"/></mxCell>
<mxCell id="c45" value="   key text PRIMARY KEY," style="text;html=1;whiteSpace=wrap;align=left;verticalAlign=top;fontSize=10;fontColor=#202124;fontStyle=0;spacing=0;" vertex="1" parent="1"><mxGeometry x="1212" y="477" width="265" height="16" as="geometry"/></mxCell>
<mxCell id="c46" value="   merchant_id bigint NOT NULL," style="text;html=1;whiteSpace=wrap;align=left;verticalAlign=top;fontSize=10;fontColor=#202124;fontStyle=0;spacing=0;" vertex="1" parent="1"><mxGeometry x="1212" y="496" width="265" height="16" as="geometry"/></mxCell>
<mxCell id="c47" value="   request_hash bytea NOT NULL," style="text;html=1;whiteSpace=wrap;align=left;verticalAlign=top;fontSize=10;fontColor=#202124;fontStyle=0;spacing=0;" vertex="1" parent="1"><mxGeometry x="1212" y="515" width="265" height="16" as="geometry"/></mxCell>
<mxCell id="c48" value="   status text NOT NULL," style="text;html=1;whiteSpace=wrap;align=left;verticalAlign=top;fontSize=10;fontColor=#202124;fontStyle=0;spacing=0;" vertex="1" parent="1"><mxGeometry x="1212" y="534" width="265" height="16" as="geometry"/></mxCell>
<mxCell id="c49" value="   response jsonb," style="text;html=1;whiteSpace=wrap;align=left;verticalAlign=top;fontSize=10;fontColor=#202124;fontStyle=0;spacing=0;" vertex="1" parent="1"><mxGeometry x="1212" y="553" width="265" height="16" as="geometry"/></mxCell>
<mxCell id="c50" value="   locked_at timestamptz," style="text;html=1;whiteSpace=wrap;align=left;verticalAlign=top;fontSize=10;fontColor=#202124;fontStyle=0;spacing=0;" vertex="1" parent="1"><mxGeometry x="1212" y="572" width="265" height="16" as="geometry"/></mxCell>
<mxCell id="c51" value="   created_at timestamptz DEFAULT now()" style="text;html=1;whiteSpace=wrap;align=left;verticalAlign=top;fontSize=10;fontColor=#202124;fontStyle=0;spacing=0;" vertex="1" parent="1"><mxGeometry x="1212" y="591" width="265" height="16" as="geometry"/></mxCell>
<mxCell id="c52" value="); -- keep 24 to 72 h, then purge" style="text;html=1;whiteSpace=wrap;align=left;verticalAlign=top;fontSize=10;fontColor=#202124;fontStyle=0;spacing=0;" vertex="1" parent="1"><mxGeometry x="1212" y="610" width="265" height="16" as="geometry"/></mxCell>
<mxCell id="c53" value="&lt;b&gt;Who makes the key?&lt;/b&gt;" style="rounded=1;arcSize=8;html=1;whiteSpace=wrap;strokeColor=#1A73E8;fillColor=#FFFFFF;dashed=0;fontSize=12;fontStyle=0;fontColor=#1A73E8;align=left;verticalAlign=top;spacingLeft=8;" vertex="1" parent="1"><mxGeometry x="1200" y="660" width="280" height="140" as="geometry"/></mxCell>
<mxCell id="c54" value='The client, once per checkout&lt;br&gt;attempt, before the first tap.&lt;br&gt;Stored with the cart so a reload&lt;br&gt;reuses it. A new key means&lt;br&gt;"I really want to pay again".' style="text;html=1;whiteSpace=wrap;align=left;verticalAlign=top;fontSize=12;fontColor=#202124;fontStyle=0;spacing=0;" vertex="1" parent="1"><mxGeometry x="1212" y="690" width="260" height="82" as="geometry"/></mxCell></root></mxGraphModel></diagram>
</mxfile>
