AMQP Prefetch Optimizer
Try Change Prefetch count: from 1 to 100.
Observe Watch Throughput, Avg Latency, Batch Time, and Efficiency as prefetch increases from 1.
Explain Explain why increasing prefetch from 1 changes network overhead per message.
Calculate throughput
Reset
Technical boundaries. Batch time is prefetch count times message processing time plus one network RTT per batch. Throughput and latency use this batch calculation.
AMQP Prefetch Optimizer
Explore the tradeoff between throughput (higher prefetch) and latency (lower prefetch) for optimal consumer performance.
colors = ({navy : "#2C3E50" , teal : "#16A085" , orange : "#E67E22" , blue : "#3498DB" , gray : "#7F8C8D" , purple : "#9B59B6" , red : "#E74C3C" })
viewof targetPrefetch = Inputs. range ([1 , 200 ], {
value : 50 ,
step : 1 ,
label : "Prefetch count:" ,
width : 400
})
viewof msgProcessTime = Inputs. range ([1 , 100 ], {
value : 10 ,
step : 1 ,
label : "Message processing time (ms):" ,
width : 400
})
viewof networkLatency = Inputs. range ([1 , 50 ], {
value : 5 ,
step : 1 ,
label : "Network RTT (ms):" ,
width : 400
})
// Calculations
batchProcessTime = targetPrefetch * msgProcessTime
networkOverhead = networkLatency // One RTT per prefetch batch
totalBatchTime = batchProcessTime + networkOverhead
effectiveThroughput = (targetPrefetch / totalBatchTime) * 1000 // msg/sec
avgLatencyPerMessage = (batchProcessTime / targetPrefetch) + (networkLatency / targetPrefetch)
efficiencyPercent = (batchProcessTime / totalBatchTime) * 100
// Generate data for throughput curve
prefetchRange = d3. range (1 , 201 , 5 )
throughputData = prefetchRange. map (p => ({
prefetch : p,
throughput : (p / (p * msgProcessTime + networkLatency)) * 1000
}))
Plot. plot ({
width : 700 ,
height : 300 ,
marginLeft : 60 ,
marginBottom : 50 ,
style : {
background : "transparent" ,
color : colors. navy ,
fontSize : "12px"
},
x : {
label : "Prefetch Count →" ,
grid : true ,
labelAnchor : "center" ,
labelOffset : 40
},
y : {
label : "↑ Throughput (msg/sec)" ,
grid : true ,
labelAnchor : "center"
},
marks : [
Plot. line (throughputData, {
x : "prefetch" ,
y : "throughput" ,
stroke : colors. teal ,
strokeWidth : 3
}),
Plot. dot ([{prefetch : targetPrefetch, throughput : effectiveThroughput}], {
x : "prefetch" ,
y : "throughput" ,
fill : colors. orange ,
r : 6 ,
stroke : colors. navy ,
strokeWidth : 2
}),
Plot. ruleY ([0 ])
]
})
html `
<div style="background: linear-gradient(135deg, ${ colors. teal } 0%, ${ colors. blue } 100%); padding: 25px; border-radius: 12px; color: white; margin: 20px 0; box-shadow: 0 4px 6px rgba(0,0,0,0.1);">
<h3 style="margin-top: 0; color: white; border-bottom: 2px solid rgba(255,255,255,0.3); padding-bottom: 10px;">Prefetch Performance at ${ targetPrefetch} </h3>
<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 20px; margin-top: 20px;">
<div style="background: rgba(255,255,255,0.1); padding: 15px; border-radius: 8px; border-left: 4px solid ${ colors. orange } ;">
<div style="font-size: 0.9em; opacity: 0.9; margin-bottom: 5px;">Throughput</div>
<div style="font-size: 2em; font-weight: bold;"> ${ effectiveThroughput. toFixed (0 )} </div>
<div style="font-size: 0.85em; opacity: 0.8; margin-top: 5px;">msg/sec</div>
</div>
<div style="background: rgba(255,255,255,0.1); padding: 15px; border-radius: 8px; border-left: 4px solid ${ colors. purple } ;">
<div style="font-size: 0.9em; opacity: 0.9; margin-bottom: 5px;">Avg Latency</div>
<div style="font-size: 2em; font-weight: bold;"> ${ avgLatencyPerMessage. toFixed (1 )} </div>
<div style="font-size: 0.85em; opacity: 0.8; margin-top: 5px;">ms/msg</div>
</div>
<div style="background: rgba(255,255,255,0.1); padding: 15px; border-radius: 8px; border-left: 4px solid ${ colors. blue } ;">
<div style="font-size: 0.9em; opacity: 0.9; margin-bottom: 5px;">Batch Time</div>
<div style="font-size: 2em; font-weight: bold;"> ${ totalBatchTime. toFixed (0 )} </div>
<div style="font-size: 0.85em; opacity: 0.8; margin-top: 5px;">ms total</div>
</div>
<div style="background: rgba(255,255,255,0.1); padding: 15px; border-radius: 8px; border-left: 4px solid ${ colors. teal } ;">
<div style="font-size: 0.9em; opacity: 0.9; margin-bottom: 5px;">Efficiency</div>
<div style="font-size: 2em; font-weight: bold;"> ${ efficiencyPercent. toFixed (1 )} %</div>
<div style="font-size: 0.85em; opacity: 0.8; margin-top: 5px;">processing/total</div>
</div>
</div>
<div style="margin-top: 20px; padding: 15px; background: rgba(255,255,255,0.05); border-radius: 8px; border: 1px solid rgba(255,255,255,0.1);">
<strong>Recommendation:</strong>
${ targetPrefetch < 10
? 'Very low prefetch causes frequent network round-trips. Increase to 50-100 for batch workloads.'
: targetPrefetch > 100
? '✓ High prefetch maximizes throughput but delays message redistribution after consumer failures.'
: '✓ Balanced prefetch for most workloads.' }
${ efficiencyPercent < 90
? ` Network overhead is ${ (100 - efficiencyPercent). toFixed (1 )} % - consider increasing prefetch.`
: '' }
</div>
</div>
`
Key Insights:
First: Prefetch count controls the tradeoff between throughput and latency. Low prefetch (1-10) minimizes per-message latency but adds network overhead. High prefetch (100+) maximizes throughput but delays fair work distribution across consumers.
Next: Backlog payload scales with message count and size. A 10-second backlog at 500 msg/sec is 5,000 messages; at 1 kB of payload each, that is 5 MB of payload before queue metadata, disk/index overhead, and replication. It is not a 5 MB RAM requirement.
Then: Consumer service capacity is approximately consumers × (1000 / processing_time_ms) messages/sec for sequential, fully busy workers with no other bottleneck. At 10 ms per message, one worker could process about 100 msg/sec. Little’s Law separately relates average work in progress to arrival rate and time in system.
Back to the chapter