Serverless 框架对比

你的 CTO 决定评估 Serverless 作为新项目的计算平台。团队里有人用 Lambda,有人推荐 Azure Functions,隔壁组刚上了 Knative。每个人都说自己的方案最好。

「没有最好的框架,只有最适合的框架。」 本文的目的是帮你建立完整的评估框架,而不是给你一个标准答案。

评估维度

评估 Serverless 框架时,需要从多个维度综合考虑:

评估维度关键问题
功能能力支持哪些触发器、语言、集成?
性能特征冷启动时间、执行时间限制、并发能力?
成本模型计费方式、免费额度、规模经济?
运维复杂度需要管理什么?学习曲线如何?
生态集成与云服务的集成度如何?
可移植性能否跨云部署?
企业支持SLA 保障、技术支持、社区活跃度?

功能对比

触发器与事件源

触发器AWS LambdaAzure FunctionsCloud FunctionsKnative
HTTPAPI Gateway/VPC内置 HTTP内置 HTTPIstio/Contour
定时任务EventBridgeTimer TriggerCloud SchedulerCronJob Source
对象存储S3Blob TriggerStorage TriggerSource 适配
消息队列SQSQueue TriggerPub/SubKafka/MT Broker
数据库变更DynamoDB StreamsCosmos DB Change FeedFirestoreTrigger 适配
流数据KinesisEvent HubsDataflowKafka Source
消息主题SNSService BusPub/SubBroker/Channel

编程语言支持

语言AWS LambdaAzure FunctionsCloud FunctionsKnative
Node.js18.x, 16.x18, 16, 1418, 16, 14
Python3.11, 3.10, 3.93.11, 3.10, 3.93.11, 3.10, 3.9
Java17, 11, 817, 11, 817, 11
Go1.x不支持1.x
.NET不支持6, 3.1不支持
Ruby3.x不支持不支持
Custom Runtime

状态与编排

能力AWS LambdaAzure FunctionsCloud FunctionsKnative
有状态函数Step FunctionsDurable Functions外部服务KEDA + State
工作流编排Step FunctionsDurable FunctionsWorkflows APIArgo/Tekton
Saga 模式Step FunctionsDurable Functions自定义自定义
人类交互Task TokenApprovalCallback自定义

性能对比

冷启动时间

运行时LambdaAzureCloud Functions
Node.js50-200ms100-300ms100-200ms
Python100-300ms100-300ms100-300ms
Java1-5s (含 SnapStart)2-10s2-8s
Go10-50msN/A50-100ms

执行限制

限制AWS LambdaAzure FunctionsCloud Functions
最大执行时间15 分钟无限制(消耗计划)60 分钟(2nd gen)
最大内存10 GB14 GB8 GB
最大包大小50 MB(解压 250 MB)100 MB(扩展到 500 MB)100 MB
HTTP 超时30 秒(API GW)230 秒60-3600 秒

扩缩容

特性AWS LambdaAzure FunctionsCloud Functions
并发限制账户级默认 1000100-200(消耗)1000(2nd gen)
最小实例Premium: 0/12nd gen: 支持
并发/实例1多(可配置)多(2nd gen)

成本对比

计费模型

计费项AWS LambdaAzure FunctionsCloud Functions
执行时间每 GB-秒每 GB-秒每 GB-秒
请求数每 100 万次每 100 万次每 100 万次
免费额度400K GB-s + 1M 请求/月400K GB-s + 1M 请求/月400K GB-s + 2M 请求/月
1M 请求 + 1B GB-s 成本~$0.20~$0.20~$0.40

实际成本计算

假设一个函数配置:

  • 内存:512 MB
  • 执行时间:100ms
  • 月请求量:1000 万次
cost_calculator.py
def calculate_monthly_cost(
    memory_mb: int,
    duration_ms: int,
    monthly_requests: int,
    provider: str
) -> float:
    # 执行时间成本(GB-秒)
    gb_seconds = (memory_mb / 1024) * (duration_ms / 1000) * monthly_requests

    # 每月免费额度
    free_gb_seconds = 400_000  # 约 400K GB-秒

    # 计费 GB-秒
    billable_gb_seconds = max(0, gb_seconds - free_gb_seconds)

    # 成本计算(美元)
    if provider == "aws":
        # $0.0000166667 per GB-second
        execution_cost = billable_gb_seconds * 0.0000166667
        request_cost = (monthly_requests / 1_000_000) * 0.20
    elif provider == "azure":
        execution_cost = billable_gb_seconds * 0.000016
        request_cost = (monthly_requests / 1_000_000) * 0.20
    else:  # gcp
        execution_cost = billable_gb_seconds * 0.000015
        request_cost = (monthly_requests / 1_000_000) * 0.40

    return execution_cost + request_cost

# 计算示例
cost = calculate_monthly_cost(
    memory_mb=512,
    duration_ms=100,
    monthly_requests=10_000_000,
    provider="aws"
)
print(f"Monthly cost: ${cost:.2f}")  # ~$6.67

运维对比

需要管理的组件

flowchart LR
    subgraph AWS["AWS Lambda"]
        L1[函数代码]
        L2[触发器配置]
        L3[权限策略]
        L4[API Gateway]
        L5[监控配置]
    end

    subgraph Azure["Azure Functions"]
        A1[函数代码]
        A2[触发器绑定]
        A3[应用设置]
        A4[App Service Plan]
        A5[Application Insights]
    end

    subgraph GCP["Cloud Functions"]
        G1[函数代码]
        G2[触发器配置]
        G3[环境变量]
        G4[Cloud Run 底层]
        G5[Cloud Monitoring]
    end

    subgraph K8s["Knative"]
        K1[容器镜像]
        K2[KSVC/Route]
        K3[Trigger/Broker]
        K4[K8s Cluster]
        K5[网络层/Istio]
    end

部署工具

工具AWSAzureGCPKnative
CLIAWS CLI, SAM CLIAzure CLI, Functions Coregcloud CLIkubectl
IaCTerraform, SAMARM, Bicep, TerraformTerraformHelm, Kustomize
框架Serverless FrameworkDurable Functions SDKFunctions FrameworkCustom YAML
CDKAWS CDK不支持不支持CDK8s

集成生态

数据库

数据库LambdaAzureGCPKnative
SQLRDS Proxy, AuroraSQL Database, Cosmos DBCloud SQL标准驱动
NoSQLDynamoDBCosmos DBFirestore, Bigtable标准驱动
CacheElastiCacheAzure CacheMemorystore标准驱动
SearchCloudSearch, OpenSearchAzure SearchCloud Search标准驱动

监控

监控LambdaAzureGCPKnative
日志CloudWatch LogsApplication InsightsCloud LoggingLoki/EFK
指标CloudWatch MetricsMonitorCloud MonitoringPrometheus
追踪X-RayApplication InsightsCloud TraceJaeger
APM第三方Application InsightsOperationsJaeger/Grafana

可移植性

跨云策略

flowchart TB
    subgraph "抽象层"
        TF[Terraform\n统一 IaC]
        SA[Serverless Framework\n统一部署]
        CD[Cloud Development Kit\n统一编码]
    end

    subgraph "运行时"
        K8s[Knative\n容器化运行]
        OS[OpenFaaS\n容器化运行]
    end

    TF --> K8s
    SA --> K8s
    CD --> K8s

    subgraph AWS
        L[Lambda]
    end

    subgraph Azure
        F[Functions]
    end

    subgraph GCP
        CF[Cloud Functions]
    end

    K8s -.-> L
    K8s -.-> F
    K8s -.-> CF

迁移复杂度

迁移路径复杂度主要工作
Lambda → Azure Functions触发器适配、SDK 替换
Lambda → Cloud Functions触发器适配、SDK 替换
Lambda → Knative容器化、网络配置
Functions → Knative容器化、集成配置

选择建议

按场景推荐

场景推荐原因
AWS 深度集成Lambda原生集成、成本优化
Azure 深度集成Azure FunctionsMicrosoft 生态无缝
GCP 深度集成Cloud FunctionsGCP 服务原生
跨云部署Knative无厂商锁定
有状态工作流Durable Functions / Step Functions内置编排
简单事件处理Cloud Functions最快上手
长期运行任务Knative / Cloud Run无时间限制
现有 K8s 架构Knative复用基础设施

按团队推荐

团队情况推荐原因
DevOps 全栈团队Knative完全可控
快速创业公司云函数最小运维
大型企业云函数 + 治理云厂商企业支持
初学 ServerlessCloud Functions文档最清晰

评估清单

在选择 Serverless 平台前,回答这些问题:

  1. 厂商锁定是否可接受?

    • 如果需要多云,Knative 是唯一选择
  2. 冷启动是否关键?

    • 如果 P99 延迟敏感,考虑 Go/Node.js 或 Provisioned Concurrency
  3. 执行时间有多长?

    • 如果超过 15 分钟,Lambda 不适用
  4. 团队技术栈是什么?

    • .NET 团队优先 Azure Functions
    • Java 团队考虑 Knative + Spring Boot
  5. 运维能力如何?

    • 无 K8s 经验,优先云函数
    • 有 K8s 经验,Knative 更灵活
  6. 成本模型是否清晰?

    • 运行频率高时,专用实例/Reserved 可能更划算

延伸思考

Serverless 选型不是一次性的决定,而是可以渐进演进的:

  1. 起步:选择一个云厂商的 Serverless,原型验证
  2. 成长:识别核心业务,考虑跨云抽象
  3. 成熟:如果需要多云,迁移到 Knative 或 Serverless Framework

记住:Serverless 的价值不在于「不用管理服务器」,而在于「只为你使用的计算付费」。 如果你的应用负载稳定,Serverless 可能比 EC2 更贵。

评估的核心问题是:Serverless 是否能降低我的总拥有成本(TCO)?