将数千条消息添加到 Azure 存储队列

add thousands of messages to an Azure Storage Queue

我正在尝试使用 [= 在 Azure 函数 中向我的 Azure 存储队列 添加大约 6000 条消息72=].

我已经尝试了多种方法来做到这一点,现在我把 QueueService method in a Promise and resolve the 6000 promises through a Promise.map with a concurrency of about 50 using Bluebird.

const addMessages = Promise.map(messages, (msg) => {
  //returns a promise wrapping the Azure QueueService method
  return myQueueService.addMessage(msg);
}, { concurrency: 50 });

//this returns a promise that resolves when all promises have resolved.
//it rejects when one of the promises have rejected.
addMessages.then((results) => {
  console.log("SUCCESS");
}, (error) => {
  console.log("ERROR");
});

我的 QueueService 是使用 ExponentialRetry 策略创建的。


我使用这个策略得到了不同的结果:


我是不是遗漏了什么,或者我的电话有时需要 2 分钟才能解决,有时需要超过 10 分钟?

将来,我可能不得不添加大约 100.000 条消息,所以我有点担心我现在无法预测的结果。

What would be the best strategy to add a large number of messages in Node (in an Azure Function)?


编辑:

不确定我是怎么错过这个的,但是将我的消息添加到我的存储队列的一种非常可靠的方法是使用我的 Azure 函数的队列输出绑定:

https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-storage-queue#storage-queue-output-binding

也让我的代码更简单!

for (var i = 0; i < messages.length; i++) {
  // add each message to queue
  context.bindings.outputQueue.push(messages[i]);
}

编辑2:

我打算将我的消息分成大约 1000 条的批次,并将这些批次存储在 Azure Blob 存储

每次添加新的 blob 时都会触发另一个 Azure 函数,这个函数将一次处理 1000 条消息的排队。

这应该使我的排队更加可靠和可扩展,因为我尝试通过我的输出绑定将 20.000 条消息添加到我的队列,并在 5 分钟后收到 Azure 函数超时,只能处理大约 15.000 条消息。

是什么触发了这个函数?我建议的做法是散开并允许这些函数扩展并通过限制它们正在执行的工作量来更好地利用并发性,而不是让一个函数添加所有这些消息。

根据我上面的建议,您将拥有处理您今天已经到位的触发器的功能排队工作,而这些工作又将由另一个功能处理,该功能执行添加(很多)的实际工作) 队列中的消息数量较少。您可能需要根据您的工作负载来比较这些数字,看看哪些功能运行良好,但这种模式可以让这些功能更好地扩展(包括跨多台机器)、更好地处理故障并提高可靠性和可预测性。

例如,您可以在消息中排队以触发工作的消息数量,如果您想要 1000 条消息作为最终输出,您可以排队 10 条消息来指示您的 "worker" 函数每条添加 100 条消息。我还建议每个函数使用更小的数字。

希望对您有所帮助!