简体   繁体   English

DynamoDB 性能差/慢

[英]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:一些可能对您有帮助的事情:

  • 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.您提供的 RAM 量不仅会影响计算,还会影响 Lambda function 的网络吞吐量,因此根据您的工作负载,这可能是一个限制。
  • 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.实例化 boto3 资源和客户端在计算方面通常相对昂贵,缓存这些资源绝对值得,以便节省几毫秒的时间——在我相对强大的笔记本上,实例化第一个150ms客户端或资源大约需要 150 毫秒,因为在第一次实例化时它读取并解析一些 JSON 描述并构建整个 object 层次结构,这需要一段时间。
  • You could consider adding the X-Ray SDK to your function and enable X-Ray on it.您可以考虑将 X-Ray SDK 添加到您的 function 并在其上启用 X-Ray。 This will give you more detailed insights into which part of your application and which API call took so long.这将使您更详细地了解应用程序的哪一部分以及哪个 API 调用花费了这么长时间。

Edit编辑

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.

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