System design assembles six core building blocks. Each solves a specific problem.
Load balancers distribute requests across backend instances. L4 balancers route by
TCP/IP; L7 balancers route by HTTP path, headers, and cookies. Use L7 for HTTP
services. Algorithms: round-robin (default), least-connections (when request latency
varies), consistent hashing (when you need sticky routing, e.g., cache affinity).
Caches reduce read latency and database load. Sit in front of the database.
Patterns: cache-aside (default), write-through (strong consistency), write-behind
(high write throughput, tolerate loss). Key concerns: TTL, invalidation strategy,
and stampede prevention. Redis is the default; Memcached only when pure key-value at
massive scale.
Databases are the source of truth. SQL for structured data with ACID transactions;
NoSQL for scale, flexible schemas, or specific access patterns. Read replicas for
read-heavy workloads. Sharding for write-heavy workloads that exceed one node.
Message queues decouple producers from consumers and absorb traffic spikes. Use
for async work, fan-out events, and unreliable downstream dependencies. Always
configure a dead-letter queue. SQS for AWS-native work; Kafka for high-throughput
event streaming or replay.
CDNs cache static assets and edge-terminate TLS close to users. Reduces origin
load and cuts latency for geographically distributed users. Use for images, JS/CSS,
and any content with high read-to-write ratio.
API gateways enforce cross-cutting concerns - auth, rate limiting, request logging,
TLS termination - at a single entry point. Never build a custom gateway; use Kong,
Envoy, or a managed provider.
系统设计由六个核心构建模块组成,每个模块解决特定问题。
负载均衡器将请求分发到后端实例。L4负载均衡器基于TCP/IP路由;L7负载均衡器基于HTTP路径、标头和Cookie路由。HTTP服务使用L7负载均衡器。算法:轮询(默认)、最少连接数(当请求延迟变化时)、一致性哈希(当需要粘性路由时,如缓存亲和性)。
缓存降低读取延迟和数据库负载,部署在数据库前端。模式:旁路缓存(默认)、写穿(强一致性)、写回(高写入吞吐量,可容忍数据丢失)。关键关注点:TTL、失效策略和缓存击穿预防。默认使用Redis;仅在超大规模纯键值场景下使用Memcached。
数据库是可信数据源。SQL适用于具有ACID事务的结构化数据;NoSQL适用于需要扩展、灵活架构或特定访问模式的场景。只读副本适用于读密集型工作负载;当写入负载超过单节点能力时使用分片。
消息队列解耦生产者和消费者,吸收流量峰值。用于异步工作、扇出事件和不可靠的下游依赖。始终配置死信队列。AWS原生场景使用SQS;高吞吐量事件流或重放场景使用Kafka。
CDN缓存静态资源并在靠近用户的边缘节点终止TLS连接。减少源站负载,降低地理分布用户的延迟。用于图片、JS/CSS以及任何读远多于写的内容。
API网关在单一入口点实施横切关注点——认证、限流、请求日志、TLS终止。切勿自行构建网关;使用Kong、Envoy或托管服务提供商的网关。