AWS SQS Lambda 一次处理 n 个文件
AWS SQS Lambda Processing n files at once
我已经设置了一个 SQS 队列,每当有文件上传时,S3 路径就会被推送。
我还设置了一个带有 SQS 触发器且批处理大小为 1 的 Lambda。
在我的方案中,我必须一次处理 n
个文件。让我们说 (n = 10
).
比如说,队列中有 100 条消息。在我当前的实施中,我正在执行以下步骤:
- 只要输入队列中有消息,就会触发Lambda
- 首先,我检查并发执行的活跃数量。如果我已经 运行 10 次执行,代码将简单地 return 而不做任何事情。如果小于10条,则从队列中读取一条消息,调用处理。
- 处理完成后,邮件将从队列中手动删除。
通过上述方法,我一次可以处理 n
个文件。但是,假设有 100 个文件同时登陆 S3。
它导致 100 次 lambda 调用。由于我们在 Lambda 中进行了条件检查,前 10 条消息进行处理,其余 90 条消息进入飞行模式。
现在,当我的一些处理完成后(比如 3/10 结束),主队列仍然是空的,因为消息仍在传输中。
据我了解,如果处理一个文件需要x分钟,那么队列中消息的可见超时应该小于x (
但这也导致了另一个问题。假设批处理需要更多时间才能完成,消息将返回队列。 Lambda 将被触发并再次进入飞行模式。
有什么办法可以控制在lambda中创建的触发器的数量。例如:只应处理前 10 条消息,但其余 90 条消息应在队列中保持 可见。或者有什么其他方法可以让这个设计变得简单吗?
我不想等到 10 条消息。即使只有 5 条消息,它也应该触发那些文件。而且我不想及时调用 Lambda(例如:每 5 分钟调用一次)。
您不必限制并发 Lambda 执行。 AWS 已经为您处理了。以下是本文档中每个区域的最大并发列表:https://docs.aws.amazon.com/lambda/latest/dg/invocation-scaling.html
突发并发配额
- 3000 – 美国西部(俄勒冈)、美国东部(弗吉尼亚北部)、欧洲(爱尔兰)
- 1000 – 亚太地区(东京)、欧洲(法兰克福)、美国东部(俄亥俄)
- 500 – 其他地区
在本文档中:https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html
Scaling and processing
For standard queues, Lambda uses long polling to poll a queue until it
becomes active. When messages are available, Lambda reads up to 5
batches and sends them to your function. If messages are still
available, Lambda increases the number of processes that are reading
batches by up to 60 more instances per minute. The maximum number of
batches that can be processed simultaneously by an event source
mapping is 1000.
For FIFO queues, Lambda sends messages to your function in the order
that it receives them. When you send a message to a FIFO queue, you
specify a message group ID. Amazon SQS ensures that messages in the
same group are delivered to Lambda in order. Lambda sorts the messages
into groups and sends only one batch at a time for a group. If the
function returns an error, all retries are attempted on the affected
messages before Lambda receives additional messages from the same
group.
Your function can scale in concurrency to the number of active message
groups. For more information, see SQS FIFO as an event source on the
AWS Compute Blog.
您可以看到 Lambda 正在自动处理扩展。不需要人为限制Lambda的个数运行 10.
Lambda的思想是你想运行尽可能多的任务,这样你就可以在最短的时间内实现并行执行。
Lambda 中有一个名为保留并发的设置,我将引用 docs(强调我的):
- Reserved concurrency – Reserved concurrency creates a pool of requests that can only be used by its function, and also prevents its function from using unreserved concurrency.
[...]
To ensure that a function can always reach a certain level of concurrency, configure the function with reserved concurrency. When a function has reserved concurrency, no other function can use that concurrency. Reserved concurrency also limits the maximum concurrency for the function, and applies to the function as a whole, including versions and aliases.
如需更深入的了解,请查看 this article from the documentation。
您可以使用它来限制可以并行触发的 Lambda 数量 - 如果没有可用的 Lambda 执行上下文,SQS 调用将等待。
只有当你想限制可以并行处理的文件数量时才需要这样做。如果没有实际需要限制,它不会花费你更多让 Lambda 为您扩展。
我已经设置了一个 SQS 队列,每当有文件上传时,S3 路径就会被推送。
我还设置了一个带有 SQS 触发器且批处理大小为 1 的 Lambda。
在我的方案中,我必须一次处理 n
个文件。让我们说 (n = 10
).
比如说,队列中有 100 条消息。在我当前的实施中,我正在执行以下步骤:
- 只要输入队列中有消息,就会触发Lambda
- 首先,我检查并发执行的活跃数量。如果我已经 运行 10 次执行,代码将简单地 return 而不做任何事情。如果小于10条,则从队列中读取一条消息,调用处理。
- 处理完成后,邮件将从队列中手动删除。
通过上述方法,我一次可以处理 n
个文件。但是,假设有 100 个文件同时登陆 S3。
它导致 100 次 lambda 调用。由于我们在 Lambda 中进行了条件检查,前 10 条消息进行处理,其余 90 条消息进入飞行模式。
现在,当我的一些处理完成后(比如 3/10 结束),主队列仍然是空的,因为消息仍在传输中。
据我了解,如果处理一个文件需要x分钟,那么队列中消息的可见超时应该小于x ( 但这也导致了另一个问题。假设批处理需要更多时间才能完成,消息将返回队列。 Lambda 将被触发并再次进入飞行模式。 有什么办法可以控制在lambda中创建的触发器的数量。例如:只应处理前 10 条消息,但其余 90 条消息应在队列中保持 可见。或者有什么其他方法可以让这个设计变得简单吗? 我不想等到 10 条消息。即使只有 5 条消息,它也应该触发那些文件。而且我不想及时调用 Lambda(例如:每 5 分钟调用一次)。
您不必限制并发 Lambda 执行。 AWS 已经为您处理了。以下是本文档中每个区域的最大并发列表:https://docs.aws.amazon.com/lambda/latest/dg/invocation-scaling.html
突发并发配额
- 3000 – 美国西部(俄勒冈)、美国东部(弗吉尼亚北部)、欧洲(爱尔兰)
- 1000 – 亚太地区(东京)、欧洲(法兰克福)、美国东部(俄亥俄)
- 500 – 其他地区
在本文档中:https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html
Scaling and processing
For standard queues, Lambda uses long polling to poll a queue until it becomes active. When messages are available, Lambda reads up to 5 batches and sends them to your function. If messages are still available, Lambda increases the number of processes that are reading batches by up to 60 more instances per minute. The maximum number of batches that can be processed simultaneously by an event source mapping is 1000.
For FIFO queues, Lambda sends messages to your function in the order that it receives them. When you send a message to a FIFO queue, you specify a message group ID. Amazon SQS ensures that messages in the same group are delivered to Lambda in order. Lambda sorts the messages into groups and sends only one batch at a time for a group. If the function returns an error, all retries are attempted on the affected messages before Lambda receives additional messages from the same group.
Your function can scale in concurrency to the number of active message groups. For more information, see SQS FIFO as an event source on the AWS Compute Blog.
您可以看到 Lambda 正在自动处理扩展。不需要人为限制Lambda的个数运行 10.
Lambda的思想是你想运行尽可能多的任务,这样你就可以在最短的时间内实现并行执行。
Lambda 中有一个名为保留并发的设置,我将引用 docs(强调我的):
- Reserved concurrency – Reserved concurrency creates a pool of requests that can only be used by its function, and also prevents its function from using unreserved concurrency.
[...]
To ensure that a function can always reach a certain level of concurrency, configure the function with reserved concurrency. When a function has reserved concurrency, no other function can use that concurrency. Reserved concurrency also limits the maximum concurrency for the function, and applies to the function as a whole, including versions and aliases.
如需更深入的了解,请查看 this article from the documentation。
您可以使用它来限制可以并行触发的 Lambda 数量 - 如果没有可用的 Lambda 执行上下文,SQS 调用将等待。
只有当你想限制可以并行处理的文件数量时才需要这样做。如果没有实际需要限制,它不会花费你更多让 Lambda 为您扩展。