viewof numCommands = Inputs.range([5, 50], {
value: 10,
step: 1,
label: "Number of ADD commands:",
width: 400
})
viewof glitchPercent = Inputs.range([0, 80], {
value: 30,
step: 5,
label: "ACK-loss probability (%):",
width: 400
})
viewof volumePerCmd = Inputs.range([10, 500], {
value: 100,
step: 10,
label: "Volume per command (ml):",
width: 400
})
viewof tankCapacity = Inputs.range([500, 10000], {
value: 2000,
step: 100,
label: "Tank capacity (ml):",
width: 400
})Try It: Duplicate Message Simulator
Try It: Duplicate Message Simulator
animation
amqp
protocols
messaging
Try Set ACK-loss probability to 30% and change volume per command.
Observe Compare Volume, Duplicates executed, and Duplicates blocked for 10 commands.
Explain Explain why idempotency keys keep Excess at 0 ml.
Technical boundaries. The simulation is deterministic based on inputs. Each ACK loss causes one redelivery, and idempotency keys track executed command IDs to block duplicate executions.
NoteTry It: Duplicate Message Simulator
Simulate what happens when network glitches cause message redelivery. Adjust the number of commands, glitch probability, and see how idempotency keys prevent dangerous duplicate executions in a chemical tank filling scenario.
{
// Deterministic simulation based on inputs
const seed = numCommands * 1000 + glitchPercent * 10 + volumePerCmd;
function pseudoRandom(i) {
const x = Math.sin(seed + i * 9301 + 49297) * 49297;
return x - Math.floor(x);
}
let duplicatesWithout = 0;
let duplicatesWith = 0;
let volumeWithout = 0;
let volumeWith = 0;
const executedIds = new Set();
const events = [];
for (let i = 0; i < numCommands; i++) {
const cmdId = `cmd-${String(i + 1).padStart(3, '0')}`;
const hasGlitch = pseudoRandom(i) * 100 < glitchPercent;
// Without idempotency
volumeWithout += volumePerCmd;
if (hasGlitch) {
volumeWithout += volumePerCmd; // duplicate execution
duplicatesWithout++;
}
// With idempotency
if (!executedIds.has(cmdId)) {
volumeWith += volumePerCmd;
executedIds.add(cmdId);
}
// Redelivered but skipped
if (hasGlitch) {
duplicatesWith++; // counted but not executed
}
if (hasGlitch) {
events.push({ cmd: cmdId, glitch: true, vol: volumePerCmd });
}
}
const expectedVolume = numCommands * volumePerCmd;
const overflowWithout = volumeWithout > tankCapacity;
const overflowWith = volumeWith > tankCapacity;
const maxBarVol = Math.max(volumeWithout, tankCapacity * 1.2);
const barScale = 300 / maxBarVol;
return html`<div style="margin: 20px 0; padding: 20px; background: #f8f9fa; border-left: 4px solid #2C3E50; border-radius: 4px;">
<h4 style="margin-top: 0; color: #2C3E50;">Tank Filling: Duplicate Command Impact</h4>
<div style="display: grid; grid-template-columns: 1fr 1fr; gap: 20px; margin: 15px 0;">
<div style="background: white; padding: 15px; border-radius: 8px; border: 2px solid ${overflowWithout ? '#E74C3C' : '#E67E22'};">
<div style="font-size: 12px; color: #7F8C8D; text-transform: uppercase; margin-bottom: 8px;">Without Idempotency</div>
<div style="position: relative; background: #ecf0f1; border-radius: 4px; height: 120px; width: 100%; overflow: hidden; border: 1px solid #bdc3c7;">
<div style="position: absolute; bottom: 0; width: 100%; background: ${overflowWithout ? '#E74C3C' : '#E67E22'}; height: ${Math.min(100, (volumeWithout / tankCapacity) * 100)}%; transition: height 0.5s; opacity: 0.7;"></div>
<div style="position: absolute; bottom: ${(tankCapacity / maxBarVol) * 100}%; width: 100%; border-top: 2px dashed #E74C3C; z-index: 1;"></div>
<div style="position: absolute; bottom: ${(tankCapacity / maxBarVol) * 100 + 1}%; right: 4px; font-size: 10px; color: #E74C3C;">MAX</div>
</div>
<div style="margin-top: 10px; font-size: 14px;">
<div>Volume: <strong style="color: ${overflowWithout ? '#E74C3C' : '#E67E22'};">${volumeWithout.toLocaleString()} ml</strong></div>
<div>Duplicates executed: <strong style="color: #E74C3C;">${duplicatesWithout}</strong></div>
<div>Excess: <strong style="color: #E74C3C;">${(volumeWithout - expectedVolume).toLocaleString()} ml</strong></div>
${overflowWithout ? html`<div style="color: #E74C3C; font-weight: bold; margin-top: 4px;">OVERFLOW - DANGEROUS!</div>` : ''}
</div>
</div>
<div style="background: white; padding: 15px; border-radius: 8px; border: 2px solid #16A085;">
<div style="font-size: 12px; color: #7F8C8D; text-transform: uppercase; margin-bottom: 8px;">With Idempotency Keys</div>
<div style="position: relative; background: #ecf0f1; border-radius: 4px; height: 120px; width: 100%; overflow: hidden; border: 1px solid #bdc3c7;">
<div style="position: absolute; bottom: 0; width: 100%; background: #16A085; height: ${Math.min(100, (volumeWith / tankCapacity) * 100)}%; transition: height 0.5s; opacity: 0.7;"></div>
<div style="position: absolute; bottom: ${(tankCapacity / maxBarVol) * 100}%; width: 100%; border-top: 2px dashed #E74C3C; z-index: 1;"></div>
<div style="position: absolute; bottom: ${(tankCapacity / maxBarVol) * 100 + 1}%; right: 4px; font-size: 10px; color: #E74C3C;">MAX</div>
</div>
<div style="margin-top: 10px; font-size: 14px;">
<div>Volume: <strong style="color: #16A085;">${volumeWith.toLocaleString()} ml</strong></div>
<div>Duplicates blocked: <strong style="color: #16A085;">${duplicatesWith}</strong></div>
<div>Excess: <strong style="color: #16A085;">0 ml</strong></div>
<div style="color: #16A085; font-weight: bold; margin-top: 4px;">CORRECT - Exact target volume</div>
</div>
</div>
</div>
${events.length > 0 ? html`<div style="background: white; padding: 12px; border-radius: 8px; margin-top: 15px; max-height: 150px; overflow-y: auto;">
<div style="font-size: 12px; color: #7F8C8D; margin-bottom: 8px;">Network Glitch Events (${events.length} of ${numCommands} commands):</div>
${events.map(e => html`<div style="font-size: 12px; padding: 3px 8px; margin: 2px 0; background: #fff3cd; border-radius: 3px;">
<code>${e.cmd}</code>: ACK lost, broker redelivered +${e.vol}ml
<span style="color: #E74C3C;">executed twice without idempotency</span> /
<span style="color: #16A085;">blocked with idempotency</span>
</div>`)}
</div>` : ''}
<div style="margin-top: 15px; padding: 12px; background: #e8f4f8; border-radius: 6px; border-left: 3px solid #3498DB;">
<strong style="color: #3498DB;">Key Insight:</strong>
<span style="color: #2C3E50;">
With ${glitchPercent}% ACK-loss rate, ${duplicatesWithout} of ${numCommands} commands were duplicated,
adding ${(volumeWithout - expectedVolume).toLocaleString()} ml of excess volume.
${overflowWithout ? " This caused a dangerous tank overflow!" : ""}
Idempotency keys prevented all ${duplicatesWith} duplicate executions.
</span>
</div>
</div>`;
}