Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Very slow requests to dynamodb from lambda function

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):

  • 256mb => ~2000ms
  • 512mb => ~1000ms
  • 1024mb => ~500ms
  • 1536mb => ~300ms

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:

  • 8.32$ per 1 milion 4000ms requests using 128mb memory
  • 10.004$ per 1 milion 300ms requests using 1536mb memory

So it's not so bad I guess...

like image 721
user606521 Avatar asked Aug 28 '15 15:08

user606521


People also ask

Why is my Lambda function so slow?

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.

How can I speed up DynamoDB query?

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.

How do I fix DynamoDB throttling?

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.


1 Answers

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.

like image 74
adamkonrad Avatar answered Sep 27 '22 22:09

adamkonrad