Load balancing (round-robin, least connections, hashing)

A load balancer distributes incoming requests across multiple servers (replicas) so no single node is overloaded. The algorithm decides which backend gets each request: round-robin, least connections, or hash-based (e.g. by client IP or session ID) for stickiness.

Where the load balancer sits

flowchart TB C[Clients] --> LB[Load Balancer] LB --> S1[Server 1] LB --> S2[Server 2] LB --> S3[Server 3]

Algorithms

AlgorithmHow it worksUse case
Round-robinRotate: S1, S2, S3, S1, S2, …Stateless; even distribution
Least connectionsSend to server with fewest active connectionsLong-lived or variable cost requests
Hash (e.g. IP or cookie)Same key → same serverSticky session, local cache
WeightedAssign more traffic to stronger nodesMixed hardware or capacity

Visual: round-robin vs hash

flowchart LR subgraph RR["Round-robin"] R1[Req1→S1] --> R2[Req2→S2] --> R3[Req3→S3] --> R4[Req4→S1] end subgraph Hash["Hash(user_id)"] H1[user_A→S1] H2[user_B→S2] H3[user_A→S1] end

Use round-robin when backends are stateless and equal. Use least connections when request cost or duration varies. Use hashing when you need the same client/session to hit the same server (and consider consistent hashing for adding/removing nodes).