Skip to main content

URL-Based Load Balancing

Imagine that in an application, there are several Backend servers, one of which must be selected to send the response, based on the request path. In this case, we need to send requests for the same route to the same server. Also, load distribution between servers should be balanced.

For this, you can use the Consistent Hash method. In this method, the Hash of the input request URL is calculated and the backend server is selected based on its value.

addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})

const BACKEND_SERVERS = [
'https://backend-server-1.example.com',
'https://backend-server-2.example.com',
'https://backend-server-3.example.com',
'https://backend-server-4.example.com',
'https://backend-server-5.example.com',
];

async function handleRequest(request) {
try {
const serverIndex = getDeterministicServerIndex(request.url);
const targetServer = BACKEND_SERVERS[serverIndex];

return new Response(JSON.stringify({ targetServer , serverIndex}), {
headers: { 'content-type': 'text/plain' },
})
}
catch (error) {
return new Response(error.message, { status: 500 });
}
}

function getDeterministicServerIndex(url) {
// Use a hash function to get a consistent server index
const hash = getUrlHash(url);
return hash % BACKEND_SERVERS.length;
}

// Simple hash function for strings
function getUrlHash(str) {
let hash = 0;
if (str.length === 0) return hash;
for (let i = 0; i < str.length; i++) {
const chr = str.charCodeAt(i);
hash = (hash << 5) - hash + chr;
hash |= 0; // Convert to 32-bit integer
}
return Math.abs(hash);
}