如何配置AWS API Gateway提升TPS?网关致TPS大幅下降问题排查
提升AWS API Gateway TPS的配置优化方案
问题背景
我们在集成环境中完成多轮性能测试,架构为:AWS API Gateway -> AWS应用负载均衡器(ALB)-> 3台AWS ECS(Docker)实例(已启用自动扩缩容)。
测试数据对比:
- 经过API Gateway的5分钟压测:TPS=198,95%响应时间=442ms,总请求数=61790
- 直接向ALB发起压测:TPS=901,95%响应时间=433ms,总请求数=280399
已关闭API Gateway的全局限流选项,但性能无明显提升;改用Lambda替代ECS后,同样存在TPS偏低的问题。
优化配置建议
1. 针对ECS架构:切换为VPC链路集成
当前API Gateway为REGIONAL类型,通过公网访问后端ALB存在额外网络开销。改为VPC链路集成可让API Gateway直接在VPC内部与ALB通信:
- 创建AWS API Gateway VPC Link,关联目标ALB
- 修改API Gateway集成配置,使用
VPC_LINK类型并指向创建的VPC链路 - 确保VPC链路的安全组允许API Gateway的IP段访问ALB端口
2. 调整API Gateway阶段节流与缓存设置
即使关闭全局限流,阶段级别的默认节流限制仍可能存在:
- 进入API Gateway控制台,找到对应阶段(如dev),进入「设置」->「节流」
- 调高速率限制(Requests/Second)和并发限制(Concurrent Requests),可参考ALB的TPS能力设置为1000+
- 若后端响应允许缓存,开启阶段缓存,减少重复请求到后端的次数
3. 针对Lambda架构:优化集成与Lambda配置
在提供的CFN模板基础上,可做以下调整:
- 提升Lambda内存:当前Lambda内存为128MB,建议改为256MB或更高(Lambda CPU资源与内存成正比,更高内存可提升执行速度)
- 开启Lambda预配置并发:提前初始化Lambda实例,避免冷启动导致的响应延迟和TPS瓶颈
- 匹配超时时间:确保API Gateway阶段超时与Lambda超时(当前为3秒)一致,避免不必要的请求中断
4. 启用API Gateway性能优化特性
- 开启内容压缩:在API Gateway阶段设置中启用Gzip压缩,减少传输数据量
- 使用HTTP/2:API Gateway默认支持HTTP/2,确保客户端用该协议发起请求,提升多路复用能力
- 分析CloudWatch指标:查看
Count、Latency、IntegrationLatency等指标,定位是API Gateway自身延迟还是后端集成延迟
5. 修改CloudFormation模板配置(Lambda测试环境)
在service.yaml中添加或调整以下配置:
- 提升Lambda内存:将
LambdaFunction的MemorySize从128改为256或512 - 添加预配置并发资源:
LambdaProvisionedConcurrencyConfig: Type: AWS::Lambda::ProvisionedConcurrencyConfig Properties: FunctionName: !Ref LambdaFunction ProvisionedConcurrentExecutions: 100 Qualifier: !Ref APIStage.StageName
- 配置阶段节流:在
APIStage的MethodSettings中添加:
ThrottlingBurstLimit: 1000 ThrottlingRateLimit: 1000
附:测试用CloudFormation代码(中文注释版)
deploy.sh
#!/bin/bash set -ex AWS_REGION="us-east-1" CLOUD_FORMATION_STACK_NAME="APILowTPSTest" CLOUD_FORMATION_TEMPLATE_FILE="service.yaml" CLOUD_FORMATION_CAPABILITIES="CAPABILITY_NAMED_IAM" aws --region "${AWS_REGION}" cloudformation deploy \ --template-file "${CLOUD_FORMATION_TEMPLATE_FILE}" \ --stack-name "${CLOUD_FORMATION_STACK_NAME}" \ --capabilities "${CLOUD_FORMATION_CAPABILITIES}" \ --no-fail-on-empty-changeset \ --tags \ Name="${CLOUD_FORMATION_STACK_NAME}-Stack" \ --parameter-overrides \ EnvironmentName="${CLOUD_FORMATION_STACK_NAME}" \ LambdaFunctionName="API-Tps-Mock-Test"
service.yaml
AWSTemplateFormatVersion: 2010-09-09 Description: > 该模板部署一个包含API Gateway和Lambda函数的测试环境,用于性能测试 Parameters: EnvironmentName: Description: 环境名称(用于命名部分资源) Type: String LambdaFunctionName: Description: 作为模拟服务的Lambda函数名称 Type: String Resources: LambdaIAMRole: Type: AWS::IAM::Role Properties: AssumeRolePolicyDocument: Version: 2012-10-17 Statement: - Action: - sts:AssumeRole Effect: Allow Principal: Service: - lambda.amazonaws.com Policies: - PolicyDocument: Version: 2012-10-17 Statement: - Action: - logs:CreateLogGroup - logs:CreateLogStream - logs:PutLogEvents Effect: Allow Resource: - !Sub arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/${LambdaFunctionName}:* PolicyName: lambda LambdaFunction: Type: AWS::Lambda::Function Properties: Description: 用于性能测试的模拟Lambda函数 FunctionName: !Ref LambdaFunctionName Handler: index.handler MemorySize: 128 # 建议修改为256或更高 Timeout: 3 Role: !GetAtt LambdaIAMRole.Arn Runtime: nodejs14.x Code: ZipFile: | exports.handler = (event, context) => { return new Promise((resolve) => resolve({ isBase64Encoded: false, body: JSON.stringify({ Result: "测试通过!" }), statusCode: 200, })); } ApiGateway: Type: AWS::ApiGateway::RestApi Properties: Name: !Join [ "-", [ !Ref EnvironmentName, "API" ] ] Description: !Join [ "-", [ !Ref EnvironmentName, "用于测试TPS的API网关" ] ] EndpointConfiguration: Types: - REGIONAL # 针对ECS架构建议改为VPC链路集成 Tags: - Key: Name Value: !Join [ "-", [ "API", !Ref EnvironmentName ] ] ApiLambdaPermission: Type: AWS::Lambda::Permission Properties: FunctionName: !Ref LambdaFunction Action: lambda:InvokeFunction Principal: apigateway.amazonaws.com SourceArn: !Join - '' - - 'arn:aws:execute-api:' - !Ref AWS::Region - ':' - !Ref AWS::AccountId - ':' - !Ref ApiGateway - '/*/*/*' SubRootResource: Type: 'AWS::ApiGateway::Resource' Properties: RestApiId: !Ref ApiGateway ParentId: !GetAtt ApiGateway.RootResourceId PathPart: "v1" SingleRequestResource: Type: 'AWS::ApiGateway::Resource' Properties: RestApiId: !Ref ApiGateway ParentId: !Ref SubRootResource PathPart: "test" ApiGatewaySingleRequestMethod: Type: AWS::ApiGateway::Method Properties: ResourceId: !Ref SingleRequestResource RestApiId: !Ref ApiGateway AuthorizationType: "NONE" HttpMethod: "POST" Integration: Type: AWS_PROXY IntegrationHttpMethod: "POST" Uri: !Sub - 'arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${lambdaArn}/invocations' - lambdaArn: !GetAtt "LambdaFunction.Arn" ApiGatewayDeployment: Type: AWS::ApiGateway::Deployment DependsOn: - ApiGatewaySingleRequestMethod Properties: RestApiId: !Ref ApiGateway APIStage: Type: AWS::ApiGateway::Stage Properties: DeploymentId: !Ref ApiGatewayDeployment MethodSettings: - HttpMethod: '*' LoggingLevel: INFO ResourcePath: /* DataTraceEnabled: true # 建议添加节流配置 # ThrottlingBurstLimit: 1000 # ThrottlingRateLimit: 1000 RestApiId: !Ref ApiGateway StageName: 'dev'
内容的提问来源于stack exchange,提问作者Edenshaw
相关产品推荐
相关产品推荐

