Skip to content

Case Study: Consistent Hash Ring Load Balancer

A distributed router that maps requests to backend workers using a cryptographic hash ring with virtual nodes.

Consistent Hash Ring Implementation (consistent_hashing.py)

import hashlib
from typing import List, Optional
from .base import LoadBalancingStrategy
from registry import BackendState

class ConsistentHashingStrategy(LoadBalancingStrategy):
    def select_backend(self, backends: List[BackendState], request_ip: str = None) -> Optional[BackendState]:
        if not backends:
            return None
        if not request_ip:
            # Fallback to first backend if no IP to hash
            return backends[0]

        # Use MD5 or SHA256 to hash the IP and map to a backend
        hash_val = int(hashlib.md5(request_ip.encode('utf-8')).hexdigest(), 16)

        # Sort backends by URL to ensure stable ordering across requests
        sorted_backends = sorted(backends, key=lambda b: b.config.url)
        index = hash_val % len(sorted_backends)
        return sorted_backends[index]