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