Lambda call latency

You’ve done a great job examining and logging your findings. These are quite helpful, so thank you.

I can just offer a few observations, which might be useful.

If you are trying to optimize lambda response for speed especially with API gateway involved, it’s pretty much an impossible task, due to latency between various services. I can spin up an ec2 endpoint on something like a lamp stack and get 5ms response time (end to end), with proper caching. Lambda can return a response pretty fast as well, but it could still be running, especially if you did some async operation.

Honestly I don’t think lamba is the best solution for APIs that require an immediate or near real time response time. Unless you can use caching, but then underlying solution doesn’t matter as much.

To offer some tips to speed things up…
On Dynamo increase your read/write capacity units.
If you are planning to query by something other than primary key, you’re going to need an index. Be mindful.

Use one file per function. Don’t do handler.save or handler.get in one file. This tends to grow unnecessarily big quickly.

Be mindful of cold starts, that will add as much as a second to your response time because the container needs to come “alive” again. Unless your API is hit non stop, consider using serverless plugin to keep your lambdas warm.

Remember that billing is done in 100ms increments, so you are not going to see any savings if your function runs for 5ms vs 85ms.

More memory = faster running function. You need to find a balance for each use case.

If you see strange outliers in terms of response time or timeouts, make sure you’re exiting correctly and not just leaving some process to accidentally run in the background until things timeout.