Skip to content

Instantly share code, notes, and snippets.

@HelveticaScenario
Created July 25, 2020 03:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HelveticaScenario/79027fb2708748c9134dfb6b7a4cb673 to your computer and use it in GitHub Desktop.
Save HelveticaScenario/79027fb2708748c9134dfb6b7a4cb673 to your computer and use it in GitHub Desktop.
Demonstation of how sharded db can reduce load
function roundRobin(count, shardCount) {
const shards = new Array(shardCount).fill(0);
function hitShard(shard, key) {
shards[shard]++;
const mod = key % shards.length;
if (mod !== shard) {
hitShard(mod, key);
}
}
let idx = 0;
for (let i = 0; i < count; i++) {
const num = Math.round(Math.random() * 100);
hitShard(idx++ % shards.length, num);
}
const total = shards.reduce((accum, curr) => accum + curr, 0);
return {
shards,
total,
max: shards.reduce((last, curr) => (curr > last ? curr : last)),
average: total / shardCount
};
}
function test(count, shardCount, iterations) {
const results = new Array(iterations);
for (let i = 0; i < iterations; i++) {
results[i] = roundRobin(count, shardCount);
}
return {
max: results.reduce((accum, curr) => accum + curr.max, 0) / iterations,
average:
results.reduce((accum, curr) => accum + curr.average, 0) / iterations,
total: results.reduce((accum, curr) => accum + curr.total, 0) / iterations
};
}
const testCases = [[10, 1], [10, 2], [10, 5], [100, 3], [100, 5], [100, 15]];
for (const testCase of testCases) {
console.log(`count: ${testCase[0]}`, `shardCount: ${testCase[1]}`, test(...testCase, 100));
}
@HelveticaScenario
Copy link
Author

results

  • count: 10, shardCount: 1, {max: 10, average: 10, total: 10}
  • count: 10, shardCount: 2, {max: 8.12, average: 7.47, total: 14.94}
  • count: 10, shardCount: 5, {max: 5.17, average: 3.63, total: 18.13}
  • count: 100, shardCount: 3, {max: 59.39, average: 55.53, total: 166.59}
  • count: 100, shardCount: 5, {max: 40.49, average: 35.91, total: 179.53}
  • count: 100, shardCount: 15, {max: 17.74, average: 12.88, total: 193.26}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment