File size: 10,459 Bytes
58c64c6
06d7a7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="description" content="Interactive simulator for retry safety, idempotency keys, and transactional outbox patterns.">
<title>Async Task Reliability Lab</title>
<style>
:root{color-scheme:dark;--bg:#0b1220;--panel:#121d2e;--panel2:#172438;--line:#293a50;--text:#e9f0f8;--muted:#9aabc0;--teal:#69ddc3;--blue:#80b9ff;--red:#ff7f86;--amber:#f3c77d;--green:#90e0ae}*{box-sizing:border-box}body{margin:0;background:radial-gradient(ellipse at 80% -10%,#213b53 0,transparent 38%),var(--bg);color:var(--text);font:15px/1.55 Inter,ui-sans-serif,system-ui,-apple-system,"Segoe UI",sans-serif}.wrap{max-width:1100px;margin:auto;padding:38px 22px 70px}.hero{padding:28px 30px;border:1px solid #31445b;border-radius:22px;background:linear-gradient(135deg,#172b42,#172338 65%,#20374b);box-shadow:0 18px 55px #02081566}.eyebrow{text-transform:uppercase;letter-spacing:.16em;color:var(--teal);font-weight:700;font-size:11px}.hero h1{font-size:clamp(30px,5vw,48px);line-height:1.05;margin:10px 0 12px;letter-spacing:-.04em}.hero p{color:#bdcce0;max-width:760px;margin:0;font-size:16px}.hero strong{color:white}.grid{display:grid;grid-template-columns:340px 1fr;gap:18px;margin-top:18px}.panel{background:linear-gradient(160deg,#152236,#111b2b);border:1px solid var(--line);border-radius:18px;padding:20px;box-shadow:0 12px 32px #0002}.panel h2{font-size:16px;margin:0 0 16px}.field{margin:0 0 17px}.field label{display:block;font-size:12px;color:var(--muted);margin-bottom:7px}.field select{width:100%;background:#0d1726;color:var(--text);border:1px solid #344860;border-radius:10px;padding:11px 12px;font:inherit}.field select:focus,button:focus{outline:2px solid var(--teal);outline-offset:2px}.rangehead{display:flex;justify-content:space-between;align-items:center}.rangehead output{color:var(--teal);font-weight:700}.field input[type=range]{width:100%;accent-color:var(--teal);margin-top:11px}.hint{font-size:12px;color:var(--muted);margin:-6px 0 17px}.run{width:100%;border:0;border-radius:10px;background:var(--teal);color:#07151a;padding:12px 16px;font-weight:800;font-size:14px;cursor:pointer}.run:hover{filter:brightness(1.08)}.results{display:flex;flex-direction:column;gap:14px}.summary{display:flex;align-items:center;gap:14px;min-height:94px}.badge{width:48px;height:48px;border-radius:50%;display:grid;place-items:center;background:#203748;font-size:23px;flex:none}.summary h2{margin:0 0 2px;font-size:19px}.summary p{margin:0;color:var(--muted)}.metrics{display:flex;gap:9px;flex-wrap:wrap;margin-top:9px}.metric{border:1px solid var(--line);background:#0e1928;padding:5px 9px;border-radius:8px;color:#cbd8e8;font-size:12px}.metric b{color:white}.explanation{color:#bfccdc;margin:0;border-left:2px solid var(--blue);padding-left:12px}.tablewrap{overflow-x:auto}.tablewrap h2{margin-bottom:10px}.table{width:100%;border-collapse:collapse;font-size:13px}.table th{text-align:left;color:var(--muted);font-weight:600;font-size:11px;text-transform:uppercase;letter-spacing:.08em;padding:8px;border-bottom:1px solid var(--line)}.table td{padding:9px 8px;border-bottom:1px solid #26364a;vertical-align:top}.table tr:last-child td{border-bottom:0}.time{font-variant-numeric:tabular-nums;color:#b4c5d9;white-space:nowrap}.kind{font-weight:700;white-space:nowrap}.tone-good .kind{color:var(--green)}.tone-bad .kind{color:var(--red)}.tone-info .kind{color:var(--blue)}.ledger-empty{color:var(--muted);font-size:13px}.foot{margin-top:18px;color:#8fa1b7;font-size:12px}.foot strong{color:#c5d2e1}.method{margin-top:18px;display:grid;grid-template-columns:repeat(3,1fr);gap:12px}.method article{border:1px solid var(--line);border-radius:14px;padding:14px;background:#101a29}.method h3{font-size:13px;margin:0 0 5px;color:var(--teal)}.method p{font-size:12px;color:var(--muted);margin:0}.sr{position:absolute;width:1px;height:1px;padding:0;margin:-1px;overflow:hidden;clip:rect(0,0,0,0);white-space:nowrap;border:0}@media(max-width:800px){.grid{grid-template-columns:1fr}.wrap{padding:20px 14px 45px}.hero{padding:23px 20px}.method{grid-template-columns:1fr}.panel{padding:17px}}
</style>
</head>
<body><main class="wrap">
<header class="hero"><div class="eyebrow">Backend reliability 路 interactive lab</div><h1>Async Task Reliability Lab</h1><p>See why retries can duplicate work鈥攁nd how <strong>idempotency keys</strong> and a <strong>transactional outbox</strong> change the failure story.</p></header>
<div class="grid">
<section class="panel" aria-labelledby="controls-title"><h2 id="controls-title">Configure a failure</h2>
<div class="field"><label for="strategy">Reliability strategy</label><select id="strategy"><option>Naive retry</option><option>Idempotency key</option><option>Transactional outbox</option></select></div>
<div class="field"><label for="scenario">Failure scenario</label><select id="scenario"><option value="commit_lost">Crash after commit (response lost)</option><option value="before_commit">Worker crashes before commit</option><option value="transient">Transient delivery failure</option></select></div>
<div class="field"><div class="rangehead"><label for="retries">Retries after first attempt</label><output id="retry-value" for="retries">3</output></div><input id="retries" type="range" min="0" max="5" step="1" value="3"></div>
<p class="hint">One synthetic request 路 deterministic outcomes 路 no real services</p><button class="run" id="run">Run failure simulation</button></section>
<div class="results" aria-live="polite">
<section class="panel summary"><div id="badge" class="badge" aria-hidden="true">馃煝</div><div><h2 id="verdict">Completed once</h2><p id="stats">Attempts: 2 路 Effects committed: 1</p><div class="metrics"><span class="metric">Strategy: <b id="chosen-strategy">Naive retry</b></span><span class="metric">Scenario: <b id="chosen-scenario">Commit lost</b></span></div></div></section>
<section class="panel"><h2>What happened</h2><p id="explanation" class="explanation"></p></section>
<section class="panel tablewrap"><h2>Event timeline</h2><table class="table"><thead><tr><th>Time</th><th>Event</th><th>Detail</th></tr></thead><tbody id="events"></tbody></table></section>
<section class="panel tablewrap"><h2>Idempotency / outbox ledger</h2><div id="ledger" class="ledger-empty"></div></section>
</div></div>
<section class="method" aria-label="Reliability patterns"><article><h3>Naive retries</h3><p>Repeat a side effect after an ambiguous response, risking duplicate work.</p></article><article><h3>Idempotency key</h3><p>Store a result by request key so a retry can replay the first outcome.</p></article><article><h3>Transactional outbox</h3><p>Commit domain state and a durable event together; delivery and consumer deduplication still need real infrastructure.</p></article></section>
<p class="foot"><strong>Scope:</strong> teaching simulation only. It uses in-memory synthetic records, not a database or payment service; its deterministic counts are examples, not production benchmarks. A real outbox needs transactional storage, a poller, observability, and recovery procedures.</p>
</main>
<script>
const $=id=>document.getElementById(id), retries=$('retries');
retries.addEventListener('input',()=> $('retry-value').textContent=retries.value);
function simulate(){
 const mode=$('strategy').value, scenario=$('scenario').value, max=Number(retries.value), key='order-7f2a';
 let events=[], ledger=[], effects=0, attempts=0;
 const ev=(t,k,d,tone='info')=>events.push({t,k,d,tone});
 ev('T+00ms','request',`POST /charge 路 idempotency-key: ${key}`);
 if(mode==='Transactional outbox')ev('T+02ms','database','Order + outbox event committed atomically.','good');
 for(let attempt=1;attempt<=max+1;attempt++){
  attempts=attempt; const t=attempt*20; ev(`T+${String(t).padStart(3,'0')}ms`,'worker',`Attempt ${attempt}: handling ${key}.`);
  if(scenario==='before_commit'&&attempt===1){ev(`T+${t+5}ms`,'failure','Worker stopped before the side effect was committed.','bad');continue;}
  if(scenario==='transient'&&attempt===1){ev(`T+${t+5}ms`,'failure','Downstream returned a retryable 503; no effect was committed.','bad');continue;}
  if(mode==='Naive retry'){
   effects++; ev(`T+${t+8}ms`,'side effect',`Charge applied. Total applied: ${effects}.`,'good');
  }else{
   const prior=ledger.find(x=>x.key===key);
   if(prior)ev(`T+${t+8}ms`,'deduplicated',`Key already completed; replay cached result ${prior.result}.`,'good');
   else{effects++;const receipt=mode==='Idempotency key'?'ch_042':'evt_042';ledger.push({key,result:receipt,status:'completed'});ev(`T+${t+8}ms`,'side effect',`Effect committed once; receipt ${receipt}.`,'good');}
  }
  if(scenario==='commit_lost'&&attempt===1){ev(`T+${t+12}ms`,'failure','Worker lost the response after commit; caller cannot tell whether it succeeded.','bad');continue;}
  ev(`T+${t+15}ms`,'response','200 OK 路 operation complete','good');break;
 }
 let explanation=scenario==='commit_lost'?'A lost response creates an ambiguous outcome. The naive retry applies the charge twice; an idempotency ledger replays the original result, while an outbox makes work durable for delivery.':scenario==='before_commit'?'No effect happened before the crash. Retrying is safe in this example; production systems still need bounded retries and durable state.':'A transient 503 with no committed effect is retryable. Backoff and jitter would spread load in a real worker.';
 const status=effects>1?'Duplicate side effect':effects===1?'Completed once':'No side effect';
 $('verdict').textContent=status;$('stats').textContent=`Attempts: ${attempts} 路 Effects committed: ${effects}`;$('chosen-strategy').textContent=mode;$('chosen-scenario').textContent=$('scenario').selectedOptions[0].textContent;$('explanation').textContent=explanation;
 $('badge').textContent=effects>1?'馃敶':effects===1?'馃煝':'馃煛';$('events').innerHTML=events.map(e=>`<tr class="tone-${e.tone}"><td class="time">${e.t}</td><td class="kind">${e.k}</td><td>${e.d}</td></tr>`).join('');
 $('ledger').innerHTML=ledger.length?`<table class="table"><thead><tr><th>Key</th><th>Result</th><th>Status</th></tr></thead><tbody>${ledger.map(r=>`<tr><td>${r.key}</td><td>${r.result}</td><td>${r.status}</td></tr>`).join('')}</tbody></table>`:'No deduplication record in this strategy.';
}
$('run').addEventListener('click',simulate);simulate();
</script></body></html>