Does my DynamoDb connection Close every time?

I am trying to understand if my Dynamodb connection closes each time I execute a command

const dboperation1 = async(param) =>{
    const docClient = new AWS.DynamoDB.DocumentClient();
    const result = await docClient.query(params).promise();
    return result

}

Considering the above example each time I call dboperation1 would it lead to connection establishment to DynamoDb first.

If so how can I keep some connections open already so that I could readily use them to query the db avoiding the extra time taken to set up the connection ? basically trying to avoid the initial handshake that takes place.

Answers 1

  • From the docs:

    By default, the default Node.js HTTP/HTTPS agent creates a new TCP connection for every new request. To avoid the cost of establishing a new connection, you can reuse an existing connection.

    All AWS SDK calls use http(s) connections. we just need to build a keepAlive Agent and pass it to httpOptions when initializing a client. This is applicable for all other AWS SDK clients , not just DynamoDB.

    const agent = new https.Agent({
      keepAlive: true
    });
    

    Here is the documentation

    Below is an example

    import { Agent as httpsAgent } from 'https';
    import AWS from 'aws-sdk';
    var docClient = new DB.DocumentClient({
        httpOptions: {
            connectTimeout: 4000,
            agent: new httpsAgent({ keepAlive: true }),
        },
        logger: console,
        apiVersion: '2012-08-10',
        convertEmptyValues: true,
    });
    

Related Articles