Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DynamoDB slow response

So my problem is that DynamoDB is taking quite some time to return single object. I'm using node.js and AWS docclient. The weird thing is that it takes from 100ms to 200ms to "select" single item from DB. Is there anyway to make it faster?

Exampel code:

var AWS = require("aws-sdk");
var docClient = new AWS.DynamoDB.DocumentClient();
console.time("user get");
var params = {
      TableName : 'User',
      Key: {
        "id": "2f34rf23-4523452-345234"
      }
    };

    docClient.get(params, function(err, data) {
        if (err) {
            callback(err);
        }
        else {              
            console.timeEnd("user get");
        }
    });

And average for this simple piece of code in lambda is 130ms. Any idea what could I do to make it faster? User table has only Primary partition key "id" and global secondary index with primary key email. When I try this from my console it takes even more time.

Any help will be much appreciated!

like image 616
gabrjan Avatar asked Feb 01 '17 11:02

gabrjan


3 Answers

I faced exactly the same issue using Lambda@Edge. Responses from DynamoDB took 130-140ms on average while the DynamoDB latency graph shown 10-20ms latency.

I managed to improve response times to ~30ms on average by disabling ssl, parameter validations, and convertResponseTypes:

const docClient = new AWS.DynamoDB.DocumentClient({ 
  apiVersion: '2012-08-10',
  sslEnabled: false,
  paramValidation: false,
  convertResponseTypes: false
});

Most likely the cause of the issue was CPU/Network throttling in the lambda itself. Lambda@Edge for viewer request can have maximum 128MB which is a pretty slow lambda. So disabling extra-checks and SSL validation made things lots faster.

If you are running just a regular Lambda, increasing memory should fix the issue.

like image 71
RomanHotsiy Avatar answered Oct 28 '22 07:10

RomanHotsiy


Have you warmed up your Lambda function? If you are only running it ad-hoc, and not running a continuous load, the function might not be available yet on the container running it, so additional time might be taken there. One way to support or refute this theory would be to look at latency metrics for the GetItem API. Finally, you could try using AWS X-Ray to find other spots of latency in your stack.

The DynamoDB SDK could also be retrying, adding to your perceived latency in the Lambda function. Given that your items are around 10 KB, it is possible you are getting throttled. Have you provisioned enough read capacity? You can verify both your read latency and read throttling metrics in the DynamoDB console for your table.

like image 38
Alexander Patrikalakis Avatar answered Oct 28 '22 07:10

Alexander Patrikalakis


I know this is a little old, but for anyone finding this question now: the instantiation of the client can be extremely slow. This was despite fast local testing, yet accessing Dynamo DB from the same region and Elastic Beanstalk instance was extremely slow!

Accessing Dynamo from a single client instance improved the speeds significantly.

like image 1
WhatIsHeDoing Avatar answered Oct 28 '22 08:10

WhatIsHeDoing