I have lambda function and dynamo db table in the same region (us-east-1). In lambda function I perform very simple query:
params = TableName: 'users' Item: email: S: event.body.email ConditionExpression: 'attribute_not_exists (email)' dynamodb.putItem(params, context.done)
There are only few rows in DynamoDB table, there is Hash Key on email and Read/Write throughtputs are set to 5/5.
Lambda function exeutes in ~4 seconds... This is very slow. Am I doing something wrong?
I've tested my function with different memory settings for lambda function (it was set to 128mb previously):
So it seems that response time depends 1-1 on memory (well in fact on compute capacity as AWS scales it along with memory). Still this is crazy because to make very simple REST API I have to set 1536mb memory to make it "responsive" while my program uses 17mb!
Hmm on the other hand I've calculated that it will cost:
So it's not so bad I guess...
Without the CPU contention that often affects serverful applications, the primary cause for slow Lambda response time is elevated latency from services that your functions integrate with. In my previous post, we discussed different ways you can track the latency to these external services.
You can increase your DynamoDB throughput by several times, by parallelizing reads/writes over multiple partitions. Use DynamoDB as an attribute store rather than as a document store. This will not only reduce the read/write costs but also improve the performance of your operations considerably.
If the traffic to a table exceeds the per-table account quotas for throughput, then the table might be throttled. To resolve this issue, use the Service Quotas console to increase the table-level read throughput and write throughput quotas for your account.
Well, the problem might also be related to un-pausing the container the Lambda
function is running at. You also may want to optimize how you initialize your objects so they don't get re-initialized every time the function is called.
See the article Container reuse in Lambda.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With