aws lambda使用
by Yan Cui
崔燕
AWS offers a wealth of options for implementing messaging patterns such as Publish/Subscribe
(often shortened to pub/sub) with AWS Lambda. In this article, we’ll compare and contrast some of these options.
AWS提供了许多选项来实现消息传递模式,例如使用AWS Lambda实现Publish/Subscribe
(通常简称为Publish/Subscribe
)。 在本文中,我们将比较和对比其中一些选项。
Pub/Sub is a messaging pattern where publishers and subscribers are decoupled through an intermediary message broker (ZeroMQ, RabbitMQ, SNS, and so on).
发布/订阅是一种消息传递模式,其中发布者和订阅者通过中间消息代理(ZeroMQ,RabbitMQ,SNS等)解耦。
In the AWS ecosystem, the obvious candidate for the broker role is Simple Notification Service (SNS).
在AWS生态系统中,代理角色的明显候选者是简单通知服务(SNS)。
SNS will make three attempts for your Lambda function to process a message before sending it to a Dead Letter Queue (DLQ) if a DLQ is specified for the function. However, according to an analysis by the folks at OpsGenie, the number of retries can be as many as six.
如果为该函数指定了DLQ,SNS将在您的Lambda函数将消息发送到死信队列(DLQ)之前进行三次尝试。 但是,根据OpsGenie员工的分析 ,重试次数可以多达6次。
Another thing to consider is the degree of parallelism this setup offers. For each message, SNS will create a new invocation of your function. So if you publish 100 messages to SNS, then you can have 100 concurrent executions of the subscribed Lambda function.
要考虑的另一件事是此设置提供的并行度。 对于每条消息,SNS都会创建一个新的函数调用。 因此,如果将100条消息发布到SNS,则可以同时执行100个并发执行的所预订Lambda函数。
This is great if you’re optimising for throughput.
如果您要优化吞吐量,那就太好了。
However, we’re often constrained by the max throughput our downstream dependencies can handle — databases, S3, internal/external services, and so on.
但是,我们经常受到下游依赖项可以处理的最大吞吐量的约束 -数据库,S3,内部/外部服务等。
If the burst in throughput is short, then there’s a good chance the retries would be sufficient (there’s a randomized, exponential back off between retries too) and you won’t miss any messages.
如果吞吐量突发很短,那么重试就很有可能就足够了(重试之间也有随机的,指数的退避),并且您不会错过任何消息。
If the burst in throughput is sustained over a long period of time, then you can exhaust the max number of retries. At this point you’ll have to rely on the DLQ and possibly human intervention in order to recover the messages that couldn’t be processed the first time around.
如果吞吐量的爆发持续了很长时间,那么您可以用尽最大的重试次数。 此时,您必须依靠DLQ以及可能的人工干预来恢复第一次无法处理的消息。
Similarly, if the downstream dependency experiences an outage, then all messages received and retried during the outage are bound to fail.
同样,如果下游依赖项发生中断,则在中断期间接收和重试的所有消息都必然会失败。
You can also run into the Lambda limit on the number of concurrent executions in a region. Since this is an account-wide limit, it will also impact your other systems within the account that rely on AWS Lambda: APIs, event processing, cron jobs, and so on.
您还可以在区域中并发执行次数遇到Lambda限制 。 由于这是一个帐户范围的限制,因此也会影响帐户中依赖AWS Lambda的其他系统:API,事件处理,cron作业等。
SNS is also prone to suffer from temporal issues, like bursts in traffic, downstream outage, and so on. Kinesis, on the other hand, deals with these issues much better as described below:
SNS还容易受到时间问题的困扰,例如流量突发,下游中断等。 另一方面,Kinesis可以更好地处理这些问题,如下所述:
But Kinesis Streams is not without its own problems. In fact, from my experience using Kinesis Streams with Lambda, I have found a number of caveats that need to be understood in order to make effective use of the service.
但是Kinesis Streams并非没有自己的问题。 实际上,根据我将Kinesis Streams与Lambda结合使用的经验,我发现了一些需要注意的注意事项,以便有效地使用该服务。
You can read about these caveats in another article I wrote here.
您可以在我在这里写的另一篇文章中了解这些警告。
Interestingly, Kinesis Streams is not the only streaming option available on AWS. There is also DynamoDB Streams.
有趣的是,Kinesis Streams并不是AWS上唯一可用的流媒体选项。 还有DynamoDB流。
By and large, DynamoDB Streams + Lambda works the same way as Kinesis Streams + Lambda. Operationally, it does have some interesting twists:
总的来说,DynamoDB Streams + Lambda的工作方式与Kinesis Streams + Lambda相同。 在操作上,确实有一些有趣的转折:
The fact that DynamoDB Streams auto scales the number of shards can be a double-edged sword. On one hand, it eliminates the need for you to manage and scale the stream (or come up with home-baked auto scaling solutions). But on the other hand, it can also diminish the ability to amortize spikes in the load you pass on to downstream systems.
DynamoDB流自动扩展分片数量的事实可能是一把双刃剑。 一方面,它消除了您管理和扩展流(或提供自制的自动扩展解决方案 )的需要。 但另一方面,它也可能削弱摊销传递给下游系统的负载中的峰值的能力。
As far as I know, there is no way to limit the number of shards a DynamoDB stream can scale up to, which is something you’d surely consider when implementing your own auto scaling solution.
据我所知,没有办法限制DynamoDB流可以扩展的分片数量,这是您在实现自己的自动扩展解决方案时一定要考虑的问题。
I think the most pertinent question is, “what is your source of truth?”
我认为最相关的问题是: “what is your source of truth?”
Does a row being written in DynamoDB make it canon to the state of your system? This is certainly the case in most N-tier systems that are built around a database, regardless of whether it’s an RDBMS or NoSQL database.
在DynamoDB中写的行是否使其符合系统状态? 在围绕数据库构建的大多数N层系统中,无论是RDBMS数据库还是NoSQL数据库,都肯定是这种情况。
In an event-sourced system where state is modeled as a sequence of events (as opposed to a snapshot), the source of truth might well be the Kinesis stream. For example, as soon as an event is written to the stream, it’s considered canon to the state of the system.
在将事件建模为一系列事件(而不是快照)的事件源系统中,真理的源头很可能就是Kinesis流。 例如,一旦将事件写入流,就将其视为系统状态的标准。
Then, there’re other considerations around cost, auto-scaling, and so on.
然后,还有其他一些有关成本,自动缩放等方面的考虑。
From a development point of view, DynamoDB streams also have some limitations and shortcomings:
从开发的角度来看,DynamoDB流也有一些限制和不足:
Excluding the cost of Lambda invocations for processing the messages, here are some cost projections for using SNS vs Kinesis Streams vs DynamoDB Streams as the broker. I’m making the assumption that throughput is consistent, and that each message is 1KB in size.
不包括用于处理消息的Lambda调用的成本,以下是一些使用SNS vs Kinesis Streams vs DynamoDB Streams作为代理的成本预测。 我假设吞吐量是一致的,每个消息的大小为1KB。
Monthly cost at 1 msg/s
每月费用为1 msg / s
Monthly cost at 1,000 msg/s
每月费用为1,000 msg / s
These projections should not be taken at face value. For starters, the assumption about a perfectly consistent throughput and message size is unrealistic, and you’ll need some headroom with Kinesis and DynamoDB streams even if you’re not hitting the throttling limits.
这些预测不应以票面价值为依据。 对于初学者来说,关于吞吐量和消息大小完全一致的假设是不现实的,并且即使您未达到节流限制,您仍需要Kinesis和DynamoDB流有一定的余量。
That said, what these projections do tell me is that:
也就是说,这些预测告诉我的是:
Whilst SNS, Kinesis, and DynamoDB streams are your basic choices for the broker, Lambda functions can also act as brokers in their own right and propagate events to other services.
尽管SNS,Kinesis和DynamoDB流是代理的基本选择,但Lambda函数也可以自己充当代理并将事件传播到其他服务。
This is the approach used by the aws-lambda-fanout project from awslabs. It allows you to propagate events from Kinesis and DynamoDB streams to other services that cannot directly subscribe to the three basic choice of brokers (either because of account/region limitations or that they’re just not supported).
这是awslabs的aws-lambda-fanout项目使用的方法。 它允许您将事件从Kinesis和DynamoDB流传播到其他不能直接订阅代理的三种基本选择的服务(由于帐户/区域限制或不支持它们)。
While it’s a nice idea and definitely meets some specific needs, it’s worth bearing in mind the extra complexities it introduces, such as handling partial failures, dealing with downstream outages, misconfigurations, and so on.
尽管这是一个不错的主意,并且肯定可以满足某些特定需求,但值得记住的是它引入了额外的复杂性,例如处理部分故障,处理下游中断,配置错误等。
So what is the best event source for doing pub-sub with AWS Lambda? Like most tech decisions, it depends on the problem you’re trying to solve, and the constraints you’re working with.
那么,使用AWS Lambda进行发布订阅的最佳事件源是什么? 像大多数高科技的决定,这取决于你试图解决的问题 ,而你正在使用的限制 。
In this post, we looked at SNS, Kinesis Streams, and DynamoDB Streams as candidates for the broker role. We walked through a number of scenarios to see how the choice of event source affects scalability, parallelism, and resilience against temporal issues and cost.
在这篇文章中,我们研究了SNS,Kinesis Streams和DynamoDB Streams作为代理角色的候选人。 我们遍历了许多场景,以了解事件源的选择如何影响可伸缩性,并行性以及针对时间问题和成本的弹性。
You should now have a much better understanding of the tradeoffs between various event sources when working with Lambda.
现在,使用Lambda时,您应该对各种事件源之间的权衡有了更好的了解。
Until next time!
直到下一次!
aws lambda使用