水平扩展与垂直扩展
了解为接口增加容量的两种基本方式——纵向扩展与横向扩展,并根据成本、限制和架构在两者之间做出选择。
水平扩展与垂直扩展 是 CoddyKit 上的免费 API Rate Limiting & Scalability Patterns 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 API Rate Limiting & Scalability Patterns 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 API Rate Limiting & Scalability Patterns 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Two Ways to Grow
When an API runs out of capacity you have two levers:
- Vertical scaling (scale up) — make the machine bigger
- Horizontal scaling (scale out) — add more machines
Each has very different cost and reliability profiles.
Vertical Scaling Explained
Vertical scaling means upgrading a single server: more CPU cores, more RAM, faster disks.
It is simple — no code changes — but you eventually hit the largest instance available, and that one box is a single point of failure.
Horizontal Scaling Explained
Horizontal scaling adds more identical servers behind a load balancer. Traffic spreads across the fleet.
- No hard ceiling — add nodes as needed
- One node failing does not take down the service
The Cost Curve
Vertical scaling cost rises steeply — top-tier hardware carries a premium. Horizontal scaling uses many commodity nodes, which is usually cheaper per unit of capacity at large scale.
Statelessness Enables Scale-Out
To scale out, any node must handle any request. That requires stateless services — no session data stored on the instance.
Push session and state to a shared store (Redis, a database) so nodes stay interchangeable.
// session in a shared store, not local memory
await redis.set('session:' + id, data, 'EX', 3600)Auto-Scaling Groups
Cloud platforms add or remove nodes automatically based on metrics like CPU or request rate.
You define a min, max, and a target metric; the platform keeps the fleet sized to load.
min_instances: 2
max_instances: 20
target_cpu_percent: 60When Vertical Still Wins
Scaling up is the right call when:
- The workload is hard to distribute (a single large in-memory dataset)
- You need a quick fix before re-architecting
- Licensing is per-node and a bigger box is cheaper
Diminishing Returns
Adding nodes is not free scaling — shared resources (a single database, a lock) become the new bottleneck. This is why scaling the data tier often matters more than the app tier.
Combining Both
Real systems mix strategies: right-size each node (a bit of vertical) then run many of them (horizontal). The goal is the best cost per request at your reliability target.
Measuring Before Scaling
Never scale blindly. Profile first to find the real constraint — CPU, memory, I/O, or a downstream dependency. Scaling the wrong dimension wastes money and hides the true bottleneck.
Scaling and Cost Awareness
Capacity is not free. A fleet sized for peak sits idle at night, burning money. Combine auto-scaling with right-sizing and consider spot or reserved capacity to match spend to real demand.
Quick Check
Check your understanding of scaling directions.
Recap
You compared scaling strategies:
- Vertical — bigger box, simple, but capped and a single point of failure
- Horizontal — more boxes, resilient, needs statelessness
- Auto-scaling sizes the fleet to load
- Always measure the real bottleneck first
常见问题解答
「水平扩展与垂直扩展」课时是免费的吗?
是的 — 「水平扩展与垂直扩展」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 API Rate Limiting & Scalability Patterns 课程的其余内容,请升级到 CoddyKit PRO。 API Rate Limiting & Scalability Patterns 课程共包含 4 节课。
「水平扩展与垂直扩展」这节课中我会学到什么?
了解为接口增加容量的两种基本方式——纵向扩展与横向扩展,并根据成本、限制和架构在两者之间做出选择。 你通过在浏览器中直接运行的动手代码来练习 API Rate Limiting & Scalability Patterns,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 API Rate Limiting & Scalability Patterns 需要有经验吗?
无需任何先前经验。CoddyKit 上的 API Rate Limiting & Scalability Patterns 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「水平扩展与垂直扩展」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 API Rate Limiting & Scalability Patterns 课中编写并运行代码吗?
能。每节 API Rate Limiting & Scalability Patterns 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 理解 API 可扩展性
- 关键可扩展性指标
- 无状态与有状态 API 设计
- 水平扩展与垂直扩展