Implementing Scalability Patterns

1. Horizontal Scaling Pattern

AspectDetail
MechanismAdd more instances; distribute load via LB
RequiresStateless services or externalized state
ProsNear-linear scaling; commodity hardware
ConsCoordination overhead; stateful workloads harder

2. Vertical Scaling Pattern

AspectDetail
MechanismBigger instance (more CPU/RAM)
ProsNo code change; simple
ConsHardware ceiling; downtime to resize; cost spikes
Use ForStateful services (DBs); legacy apps

3. Load Balancing Pattern

AlgorithmUse
Round RobinUniform distribution; simple
Least ConnectionsLong-lived connections
Least Response TimeHeterogeneous latencies
WeightedMixed instance sizes
Consistent HashCache affinity, session stickiness
Power of Two ChoicesSample 2; pick less loaded — near-optimal

4. Auto-Scaling Pattern

TriggerDetail
CPU UtilizationMost common; lagging indicator
Request RateDirect demand signal
Queue DepthFor async workers (KEDA)
Custom Metricsp99 latency, errors
ScheduledTime-of-day patterns
PredictiveML forecast

Example: K8s HPA

apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata: { name: order-service }
spec:
  scaleTargetRef: { apiVersion: apps/v1, kind: Deployment, name: order-service }
  minReplicas: 3
  maxReplicas: 50
  metrics:
  - type: Resource
    resource: { name: cpu, target: { type: Utilization, averageUtilization: 70 } }

5. Stateless Service Pattern

RuleDetail
No In-Memory SessionExternalize to Redis / DB / cookie
No Local FilesUse object store
IdempotentAny instance can serve any request
BenefitTrivial horizontal scaling, fast restart

6. Database Sharding Pattern

StrategyDetail
Hash-Basedhash(key) → shard
Range-BasedKey ranges per shard
Directory-BasedLookup table for shard mapping
Geo-BasedShard by region
ToolsVitess, Citus, MongoDB sharding, Cassandra

7. Read Replica Pattern

AspectDetail
Topology1 primary (writes) + N replicas (reads)
ReplicationAsync (default) or sync
Read RoutingSmart client / proxy splits reads
Trade-offReplica lag → eventual consistency

8. Queue-Based Load Leveling Pattern

AspectDetail
DefinitionUse queue between bursty producer and steady consumer
EffectSmooths traffic; consumer scales independently
Auto-ScaleScale consumers based on queue depth (KEDA)

9. Partitioning Pattern

TypeUse
Functional (Vertical)Different services own different data domains
Horizontal (Sharding)Same schema split by key
Topic Partitioning (Kafka)Parallel consumers per partition
Hash + Range HybridCassandra primary key

10. Elastic Scaling Pattern

AspectDetail
DefinitionScale up AND down automatically with demand
Cost BenefitPay for what you use
Scale-Down CareDrain in-flight requests; respect graceful shutdown
CooldownPrevent flapping