Prefetch & Consumer Distribution
Try Change Prefetch count from 1 to 50 with varying consumer processing speeds.
Observe Watch Fairness Index and messages per consumer as Prefetch count changes from 1.
Explain Explain why prefetch=1 affects fairness when consumers have varying processing speeds.
Play distribution
Reset
Technical boundaries. Messages are dispatched in rounds, with processing determined by the selected speed profile. Fairness compares minimum and maximum consumer message loads; rounds stop when all messages are dispatched.
Adjust the prefetch count and number of consumers to see how messages are distributed. Observe how different prefetch values affect fairness and throughput when consumers have varying processing speeds.
viewof pfPrefetch = Inputs. range ([1 , 50 ], {value : 1 , step : 1 , label : "Prefetch count" })
viewof pfConsumers = Inputs. range ([1 , 6 ], {value : 3 , step : 1 , label : "Number of consumers" })
viewof pfTotalMsgs = Inputs. range ([10 , 60 ], {value : 30 , step : 1 , label : "Total messages in queue" })
viewof pfSpeedVariation = Inputs. select (["Uniform (all same speed)" , "Variable (1x, 2x, 3x speeds)" , "One slow consumer (10x slower)" ], {value : "Variable (1x, 2x, 3x speeds)" , label : "Consumer speed profile" })
pfSimulation = {
const n = pfConsumers;
const prefetch = pfPrefetch;
const total = pfTotalMsgs;
const speeds = [];
for (let i = 0 ; i < n; i++ ) {
if (pfSpeedVariation === "Uniform (all same speed)" ) {
speeds. push (1 );
} else if (pfSpeedVariation === "Variable (1x, 2x, 3x speeds)" ) {
speeds. push (1 + i);
} else {
speeds. push (i === 0 ? 10 : 1 );
}
}
const consumerLoad = Array . from ({length : n}, () => 0 );
const consumerUnacked = Array . from ({length : n}, () => 0 );
const consumerProcessed = Array . from ({length : n}, () => 0 );
let dispatched = 0 ;
for (let round = 0 ; round < total * 3 && dispatched < total; round++ ) {
for (let c = 0 ; c < n && dispatched < total; c++ ) {
if (consumerUnacked[c] < prefetch) {
consumerUnacked[c]++;
consumerLoad[c]++;
dispatched++;
}
}
for (let c = 0 ; c < n; c++ ) {
if (consumerUnacked[c] > 0 && round % speeds[c] === 0 ) {
consumerUnacked[c]--;
consumerProcessed[c]++;
}
}
}
const maxLoad = Math . max (... consumerLoad);
const minLoad = Math . min (... consumerLoad);
const fairness = maxLoad > 0 ? (minLoad / maxLoad * 100 ). toFixed (0 ) : 100 ;
return {consumerLoad, speeds, fairness, n, prefetch, total, maxLoad};
}
html `
<div style="background: #f8f9fa; padding: 20px; border-radius: 8px; border-left: 4px solid #16A085; margin: 20px 0;">
<div style="font-size: 15px; font-weight: bold; color: #2C3E50; margin-bottom: 15px;">Message Distribution Across ${ pfSimulation. n } Consumers (prefetch= ${ pfSimulation. prefetch } )</div>
<div style="display: grid; grid-template-columns: repeat( ${ Math . min (pfSimulation. n , 6 )} , 1fr); gap: 10px; margin-bottom: 15px;">
${ pfSimulation. consumerLoad . map ((load, i) => `
<div style="background: white; padding: 12px; border-radius: 6px; text-align: center;">
<div style="font-size: 11px; color: #7F8C8D; margin-bottom: 4px;">Consumer ${ i + 1 } ( ${ pfSimulation. speeds [i]} x)</div>
<div style="font-size: 20px; font-weight: bold; color: #2C3E50;"> ${ load} </div>
<div style="font-size: 11px; color: #7F8C8D;">messages</div>
<div style="margin-top: 8px; background: #ecf0f1; border-radius: 4px; height: 8px; overflow: hidden;">
<div style="height: 100%; width: ${ pfSimulation. maxLoad > 0 ? (load / pfSimulation. maxLoad * 100 ) : 0 } %; background: ${ load === pfSimulation. maxLoad ? '#E67E22' : '#16A085' } ; border-radius: 4px;"></div>
</div>
</div>
` ). join ("" )}
</div>
<div style="display: grid; grid-template-columns: 1fr 1fr; gap: 12px;">
<div style="background: white; padding: 12px; border-radius: 6px;">
<div style="font-size: 12px; color: #7F8C8D;">Fairness Index</div>
<div style="font-size: 20px; font-weight: bold; color: ${ pfSimulation. fairness >= 80 ? '#16A085' : pfSimulation. fairness >= 50 ? '#E67E22' : '#E74C3C' } ;"> ${ pfSimulation. fairness } %</div>
<div style="font-size: 11px; color: #7F8C8D;"> ${ pfSimulation. fairness >= 80 ? 'Fair distribution' : pfSimulation. fairness >= 50 ? 'Moderate imbalance' : 'Unfair -- reduce prefetch' } </div>
</div>
<div style="background: white; padding: 12px; border-radius: 6px;">
<div style="font-size: 12px; color: #7F8C8D;">Recommendation</div>
<div style="font-size: 12px; color: #2C3E50; line-height: 1.6; margin-top: 4px;">
${ pfSimulation. prefetch === 1 ? "Prefetch=1 gives fairest distribution but adds ACK latency per message. Good for slow tasks." : pfSimulation. prefetch <= 10 ? "Moderate prefetch balances throughput and fairness. Good for medium-speed tasks." : "High prefetch boosts throughput but fast consumers may hoard messages, starving slower ones." }
</div>
</div>
</div>
</div>
`
Back to the chapter