aws-lambdaamazon-dynamodbvpc-endpoint

200ms latency to DynamoDB from cold lambda vs <10ms when hot. Why?


I'm developing an AWS Serverless architecture.

I have a lambda attached to a subnet within a VPC. I have setup a VPC endpoint to reach my DynamoDB table.

When my lambda is cold, it takes up to 200 - 300ms make a simple GetItem command to my dynamodb table. This is just the GetItem command, I have already substracted lambda initialization, DynamoDB client instanciation etc. This is unacceptable for my application.

However, when my lambda is hot, I only take ~8-9msm for the GetItem command, which is acceptable.

Is there some ENIs latencies because my lambda is attached to a subnet ? If so, what can I do to speed it up ? Or is there another problem that I do not see ?


Solution

  • The latencies you're experiencing is due to the metadata caching that DynamoDB utilizes to lower latency.

    When requests are made frequently to DynamoDB it makes use of caching certain metadata locally, such as authentication and data locality etc....

    When requests are infrequent this cache is stale and results in extra hope by the DynamoDB service in order to serve your requests. This is one of the fundamental reasons that DynamoDB latency decreases as throughput increases.

    Some thing's that you can do to avoid the latency hit:

    1. Ensure you reuse TCP connections by setting keep-alice to true
    2. Ensure client is created outside of the Lambda handler
    3. Send dummy traffic to your DynamoDB table to ensure it keeps the metadata cache warm.