What is horizontal scaling for a Node.js application, and what specific architectural requirement does it impose?
Short answer
Horizontal scaling adds more instances behind a load balancer rather than making one instance more powerful -- this requires the application to be stateless: any instance must handle any request without depending on data that only exists on one instance, which is why session…
Advertisement
AModel answer
Horizontal scaling adds more instances behind a load balancer rather than making one instance more powerful -- this requires the application to be stateless: any instance must handle any request without depending on data that only exists on one instance, which is why session state needs a shared store like Redis and uploads need shared storage like S3.
// Stateful (breaks horizontal scaling)
app.use(session({ secret: 'x' }));
// Stateless: session in shared Redis
app.use(session({ store: new RedisStore({ client: redisClient }), secret: process.env.SESSION_SECRET }));Real-world example
An application that worked with a single instance started randomly logging users out after being scaled to three instances, traced to in-memory sessions, fixed by moving to Redis-backed sessions.
Likely follow-ups
- How does sticky sessions at the load balancer offer a partial alternative to making the app fully stateless?
- What's the difference between horizontal scaling and Node's built-in cluster module?
Related topics