serverless

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Serverless

Serverless

Serverless is a cloud-native development model for building and running applications without managing servers. The cloud provider handles the routine work of provisioning, maintaining, and scaling the server infrastructure.
Serverless是一种云原生开发模式,无需管理服务器即可构建和运行应用程序。云服务提供商负责处理服务器基础设施的配置、维护和扩容等日常工作。

When to Use

适用场景

  • Event-driven background tasks (Image processing, Cron jobs).
  • APIs with spiky or unpredictable traffic (Auto-scales instantly).
  • Startup/MVP where "Scale to Zero" (Zero cost when idle) is critical.
  • Glue code between cloud services (e.g., S3 trigger -> Lambda -> DynamoDB).
  • 事件驱动型后台任务(如图片处理、定时任务)。
  • 流量波动大或不可预测的API(可即时自动扩容)。
  • 对“闲置时自动缩容至零(闲置时零成本)”有要求的初创项目/最小可行产品(MVP)。
  • 云服务之间的粘合代码(例如:S3触发器 -> Lambda -> DynamoDB)。

Quick Start

快速开始

javascript
// AWS Lambda Handler (Node.js)
export const handler = async (event) => {
  console.log("Event received:", JSON.stringify(event));

  const name = event.queryStringParameters?.name || "World";

  return {
    statusCode: 200,
    body: JSON.stringify({ message: `Hello, ${name}!` }),
  };
};
yaml
undefined
javascript
// AWS Lambda Handler (Node.js)
export const handler = async (event) => {
  console.log("Event received:", JSON.stringify(event));

  const name = event.queryStringParameters?.name || "World";

  return {
    statusCode: 200,
    body: JSON.stringify({ message: `Hello, ${name}!` }),
  };
};
yaml
undefined

serverless.yml (Serverless Framework)

serverless.yml (Serverless Framework)

service: my-api provider: name: aws runtime: nodejs20.x
functions: hello: handler: handler.hello events: - httpApi: path: /hello method: get
undefined
service: my-api provider: name: aws runtime: nodejs20.x
functions: hello: handler: handler.hello events: - httpApi: path: /hello method: get
undefined

Core Concepts

核心概念

FaaS (Function as a Service)

FaaS (Function as a Service)

Upload code (Function), define triggers (HTTP, Queue, DB, Timer). Run only when triggered.
上传代码(Function),定义触发器(HTTP、消息队列、数据库、定时器)。仅在触发时运行。

Cold Start

冷启动

The latency incurred when a function is invoked after being idle (container spinning up).
函数在闲置后被调用时产生的延迟(容器启动耗时)。

Statelessness

无状态

Functions are ephemeral. Store state in external managed services (Redis, DynamoDB, S3).
函数是临时的。需将状态存储在外部托管服务中(如Redis、DynamoDB、S3)。

Common Patterns

常见模式

Fan-out

扇出模式

One event triggers multiple parallel functions (e.g., Upload -> [Resize, Analyze, Back up]).
一个事件触发多个并行函数(例如:上传文件 -> [图片缩放、内容分析、备份])。

Strangler Fig

绞杀者模式

Migrating a monolith by gradually replacing endpoints with serverless functions routed via API Gateway.
通过API网关逐步将单体应用的端点替换为Serverless函数,完成迁移。

Best Practices

最佳实践

Do:
  • Use Frameworks (SST, Serverless Framework, SAM) for IaC.
  • optimize Cold Starts (keep functions small, use provisioned concurrency if needed).
  • Use Managed Services (DynamoDB, EventBridge) instead of custom code logic.
Don't:
  • Don't use Serverless for Long-Running tasks (Gateways timeout at ~30s, Lambdas at 15m). Use Fargate/Batch for that.
  • Don't ignore vendor lock-in (though often the speed creates enough value to justify it).
建议
  • 使用框架(SST、Serverless Framework、SAM)实现基础设施即代码(IaC)。
  • 优化冷启动(保持函数体积小巧,必要时使用预置并发)。
  • 使用托管服务(DynamoDB、EventBridge)而非自定义代码逻辑。
不建议
  • 不要将Serverless用于长时间运行的任务(网关超时约30秒,Lambda超时为15分钟)。此类场景应使用Fargate/Batch。
  • 不要忽视厂商锁定问题(不过通常其带来的速度优势足以证明其合理性)。

Troubleshooting

故障排查

ErrorCauseSolution
Timeout
Function took too long.Increase timeout setting; optimize code; move to async pattern.
OOM (Out of Memory)
Processing large files.Increase RAM (allocates more CPU too); stream data instead of loading all in memory.
错误类型原因解决方案
Timeout(超时)
函数运行时间过长。增加超时设置;优化代码;改用异步模式。
OOM(内存不足)
处理大文件。增加内存分配(同时会分配更多CPU);采用流式处理数据而非一次性加载全部内容。

References

参考资料