[英]DynamoDB Poor/Slow Performance
I have a very simple webservice in AWS Lambda using Python and Flask (Service A).我在 AWS Lambda 中有一个非常简单的网络服务,使用 Python 和 Flask(服务 A)。 The service receive a request and perform a DynamoDB query and returns the results.
该服务接收请求并执行 DynamoDB 查询并返回结果。 DynamoDB has on-demand capacity and almost in all cases return 1 result.
DynamoDB 具有按需容量,几乎在所有情况下都返回 1 个结果。
I perform the query with the following function.我使用以下 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响应大小 ~ 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.我遇到了一些性能问题,例如单个请求需要 1040 毫秒,AWS Lambda Memory 到 128MB,最大 Memory 用于 95-100 MB。 All the time except of 4ms consumed in the DynamoDB query.
除了 DynamoDB 查询中消耗的 4 毫秒外,所有时间都是如此。
Below are the response times when I increase the memory.下面是我增加 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.现在我在 AWS Lambda(服务 B)中有另一个网络服务,它使用 Python、Flask、Pandas 和 PyODBC。 The service receive a request and perform 2 simple queries to MSSQL server which is not hosted in AWS and return the results.
该服务接收请求并对未托管在 AWS 中的 MSSQL 服务器执行 2 个简单查询并返回结果。 This service has 128MB of Memory and Max Memory Used: 128 MB (consume all the memory).
此服务有 128MB 的 Memory 和 Max Memory Used:128 MB(消耗所有内存)。 The performance for a single request to this service is 500ms.
对该服务的单个请求的性能为 500 毫秒。
Can someone explain me how is that possible?有人可以向我解释这怎么可能吗?
Is there any solution in order to make the query in Service A faster?是否有任何解决方案可以使服务 A 中的查询更快?
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.150ms
客户端或资源大约需要 150 毫秒,因为在第一次实例化时它读取并解析一些 JSON 描述并构建整个 object 层次结构,这需要一段时间。Boy does memory size matter when instantiating boto3 for the first time, I'm putting a blog post together about the methodology.男孩在第一次实例化 boto3 时确实 memory 大小很重要,我正在写一篇关于该方法的博客文章。 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.
但如果 memory 参数非常小,在 lambda 冷启动后初始化第一个 boto3 客户端/资源似乎需要很长时间。
I moved the ddb = DynamoDB()
outside of the handler and I increased the memory of the lambda function to 256MB.我将
ddb = DynamoDB()
移到处理程序之外,并将 lambda function 的 memory 增加到 256MB。 As a result I reduce the response to 67ms - 75ms.因此,我将响应时间减少到 67 毫秒 - 75 毫秒。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.