Back to results for “Communication”
Node.js › Clustering & Worker Threads

What is the difference between the 'sticky' and default load-balancing strategies when using Node's cluster module, and when does it matter?

Intermediate

Answer

By default the primary distributes connections across workers round-robin-like. 'Sticky' load balancing ensures requests from the same client always route to the same worker -- important for WebSocket connections or scenarios relying on in-memory, per-connection state on a specific worker.
const sticky = require('sticky-session');
if (!sticky.listen(server, 3000)) {
  server.once('listening', () => console.log('Server started'));
}
Real-world example A chat app clustering across four cores discovers WebSocket reconnections sometimes landed on a different worker than the one holding session state; sticky load balancing by IP resolved dropped connections.

Common follow-ups: Why is sticky load balancing generally unnecessary if session state is already externalized to Redis?;How does sticky behavior interact with a cloud load balancer in front of the whole cluster?