简体   繁体   中英

DynamoDB Poor/Slow Performance

I have a very simple webservice in AWS Lambda using Python and Flask (Service A). The service receive a request and perform a DynamoDB query and returns the results. DynamoDB has on-demand capacity and almost in all cases return 1 result.

I perform the query with the following function.

class DynamoDB:

    def __init__( self ):
        session = boto3.Session( )
        self.dynamodb = session.resource( 'dynamodb' )

    def query( self, table_name, **kwargs ):

        # Selected Table
        table = self.dynamodb.Table( table_name )

        # Request to table
        response = table.query( **kwargs )

        return response

Query Expression

"#user_id = :user_id and begins_with( #sort_key, :sort_key)" 

Response size ~ 400B

I encounter some issues with the performance such as for a single request take 1040ms with AWS Lambda Memory to 128MB and Max Memory Used to 95-100 MB. All the time except of 4ms consumed in the DynamoDB query.

Below are the response times when I increase the memory.

128  MB  -> 1040 ms
512  MB  -> 520  ms
1024 MB  -> 210  ms  

Now I have an another webservice in AWS Lambda (Service B) which is using Python, Flask, Pandas and PyODBC. The service receive a request and perform 2 simple queries to MSSQL server which is not hosted in AWS and return the results. This service has 128MB of Memory and Max Memory Used: 128 MB (consume all the memory). The performance for a single request to this service is 500ms.

Can someone explain me how is that possible?

Is there any solution in order to make the query in Service A faster?

A couple of things that might help you:

  • The amount of RAM you provision not only influences the compute, but also the.network throughput of your Lambda function, so depending on your workload this may be a limit.
  • Instantiating boto3 resources and clients is typically relatively expensive in terms of compute, it's definitely worth it to cache these in order to shave of a few milliseconds from your time - on my relatively powerful notebook it takes about 150ms to instantiate the first boto3 client or resource, because on first instantiation it reads and parses some JSON descriptions and builds the whole object hierarchy, which takes a while.
  • You could consider adding the X-Ray SDK to your function and enable X-Ray on it. This will give you more detailed insights into which part of your application and which API call took so long.

Edit

Boy does memory size matter when instantiating boto3 for the first time, I'm putting a blog post together about the methodology. but it seems that it takes a very long time to initialize the first boto3 client/resource after a lambda cold start if the memory parameter is very small.

图形

I moved the ddb = DynamoDB() outside of the handler and I increased the memory of the lambda function to 256MB. As a result I reduce the response to 67ms - 75ms.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM