Horizontal scaling adds more machines to handle increased load. It's the key to building truly scalable systems.
Load Balancing
Distribute traffic across multiple servers:
typescript
class LoadBalancer {
private servers: Server[] = [];
private strategy: 'round-robin' | 'least-connections' | 'ip-hash';
addServer(server: Server) {
this.servers.push(server);
}
getServer(request: Request): Server {
switch (this.strategy) {
case 'round-robin':
return this.roundRobin();
case 'least-connections':
return this.leastConnections();
case 'ip-hash':
return this.ipHash(request.ip);
}
}
private roundRobin(): Server {
const server = this.servers[0];
this.servers.push(this.servers.shift()!);
return server;
}
}Stateless Applications
Stateless apps scale easier:
typescript
// Bad: Stateful
class SessionManager {
private sessions = new Map<string, Session>();
createSession(userId: string) {
const session = new Session();
this.sessions.set(userId, session);
}
}
// Good: Stateless with external storage
class StatelessSessionManager {
createSession(userId: string) {
const session = new Session();
redis.set(`session:${userId}`, JSON.stringify(session));
}
}Database Sharding
Split data across databases:
typescript
class ShardManager {
private shards: Database[] = [];
constructor(shards: Database[]) {
this.shards = shards;
}
getShard(key: string): Database {
const hash = this.hash(key);
const index = hash % this.shards.length;
return this.shards[index];
}
private hash(key: string): number {
let hash = 0;
for (let i = 0; i < key.length; i++) {
hash = ((hash << 5) - hash) + key.charCodeAt(i);
hash |= 0;
}
return Math.abs(hash);
}
}Conclusion
Horizontal scaling requires stateless design and smart data distribution.