Designing for Scalability

1. Implementing Horizontal Scaling

RequirementSolution
StatelessnessMove session to Redis/JWT
Service discoveryConsul, K8s DNS, Eureka
Load balancingL4/L7 LB, round-robin, least-conn
Shared storageS3, EFS, distributed DB
ConfigEnv vars, central config store

2. Implementing Vertical Scaling

ResourceLimitWhen to Use
CPU/RAMSingle-node ceilingLow-traffic, simple ops
SSD/NVMe IOPSDisk bandwidthDB write workloads
NIC throughput10/25/100GbENetwork-bound services
Cost curveSuper-linear at top tierSwitch to horizontal

3. Designing Auto-Scaling Strategies

StrategyTriggerUse Case
Reactive (target tracking)CPU/RPS over thresholdGeneral workloads
Step scalingDifferent sizes per bandBursty traffic
ScheduledTime-of-dayPredictable patterns
PredictiveML forecastHoliday spikes
Queue-basedQueue depth per workerAsync workers
Note: Always set min/max bounds; scale-out faster than scale-in (cooldowns) to avoid flapping.

4. Designing Stateless vs Stateful Services

AspectStatelessStateful
ScalingTrivially horizontalRequires data partitioning
FailoverReplace instanceReplicate state, leader election
ExamplesAPI servers, web frontendsDatabases, game servers, caches
K8s primitiveDeploymentStatefulSet, PVC

5. Implementing Service Decomposition

StrategySplit By
Business capabilityOrder, Inventory, Billing
Subdomain (DDD)Bounded contexts
Verb/use caseCheckout service, Search service
Data ownershipOne service owns each entity
VolatilityIsolate frequently-changing parts

6. Implementing Connection Pooling at Scale

SettingGuideline
Pool size≈ (CPU × 2) + effective spindles (Postgres rule)
Max DB connectionsApp pool × instances ≤ DB max
Connection proxyPgBouncer, RDS Proxy for high fan-out
Acquire timeout200–500ms; fail fast
Idle eviction5–10 min

7. Managing Distributed State

ApproachDescription
ExternalizeMove state to Redis/DB
Sticky sessionsLB routes user to same node
Consistent hashingStable mapping under node changes
Leader electionSingle owner per resource (etcd, ZK)
CRDTsConflict-free merging across replicas

8. Designing Scalability Testing Strategy

TestGoal
LoadVerify SLO at expected peak
StressFind breaking point
SpikeSudden 10× traffic
SoakDetect leaks over hours/days
Toolsk6, Gatling, JMeter, Locust

9. Identifying Scalability Bottlenecks

LayerSymptomTool
CPUHigh %util, runqueue depthtop, perf, async-profiler
MemoryGC pauses, OOMjstat, pprof
Disk I/Oiowait, queue depthiostat, fio
NetworkRetransmits, full TX queuess, tcpdump
DBSlow queries, lock waitspg_stat_activity, EXPLAIN
ApplicationLock contention, thread poolflame graphs, APM

10. Designing Capacity Planning Strategy

Capacity Planning Process

  1. Forecast demand (RPS, GB/day) over horizon
  2. Benchmark single-instance throughput
  3. Compute required instances + headroom (≥30%)
  4. Validate via load test at projected scale
  5. Re-evaluate quarterly with actuals

11. Designing Sharding Strategy

StrategyProsCons
RangeRange queries efficientHot spots on sequential keys
HashEven distributionRange queries scatter
Consistent hashMin reshuffling on resizeSlightly uneven; needs vnodes
GeoLocality, complianceCross-region joins
DirectoryFlexible mappingLookup is SPOF

12. Designing Partitioning Strategy

TypeDescription
HorizontalRows split across partitions (sharding)
VerticalColumns split (rare; column stores)
FunctionalTables split by feature
HybridComposite key hashing + range
Warning: Choose partition key carefully — changing it later requires full data migration. Avoid keys with hot partitions (e.g., timestamp as primary).