MongoDB作为队列服务? [英] MongoDB as a queue service?

查看:170
本文介绍了MongoDB作为队列服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很想听到更多有关MongoDB作为队列服务的真实应用程序体验的信息,如果您为此目的使用MongoDB,您能否分享您的想法以及使用它的环境?

I would love to hear more about real application experience witn MongoDB as a queue service, if you used MongoDB for this purpose could you share your thoughts, as well as the environment in which it was used?

推荐答案

我正在将mongodb用作发送电子邮件的队列服务. 很快它将以以下方式工作:

I am using mongodb as a queue service for email sending. Soon it will work in following way:

  1. 收到新消息时,我会将其存储在mongodb中.
  2. 然后,后台作业通过原子操作 findAndModify 从mongodb加载消息并将标志Processing设置为true,因此它不会处理两次相同的消息(因为我的后台作业并行运行多个线程).
  3. 发送电子邮件后,我将从mongodb中删除该文档.
  4. 您还可以记录每封邮件的失败次数,并在3次尝试失败后将其删除.
  1. When a new message comes I store it in the mongodb.
  2. A background job then loads the message from mongodb via the atomic operation findAndModify and sets the flag Processing to true, so it does not process same message twice (because my background job runs multiple threads in parallel).
  3. Once the email has been sent I remove the document from mongodb.
  4. You can also keep count of the failures for each message and remove it after 3 failed attempts.

通常,我将mongodb用作队列服务仅出于一个原因:因为我需要按照指定的时间表发送电子邮件(每条消息均包含有关应在何时发送的信息).

In general I use mongodb as a queue service only for one reason: because I need to send emails by specified schedule (each message contains information on what time it should be sent).

如果没有任何时间表,需要立即处理消息,建议您调查现有的

If you do not have any schedule and need to process message immediately, I suggest that you look into existing queue services, because they probably handle all cases that you may not see without a deeper understanding of message queues.

在邮件处理期间后台作业崩溃时,您可以执行以下操作:

When background job crashes during message processing you could do following:

  1. 将此消息移到另一个消息队列错误集合中.

  1. Move this message to another, message queue errors collection or..

增加处理尝试在消息中进行计数,然后再次分配状态"New",以尝试再次处理它.只需确保后台作业是幂等的(可以多次处理同一条消息且不会破坏数据)并且具有事务性(当作业失败时,您必须撤消所做的更改.). 5次尝试(配置值)后作业失败时,执行#1.

Increase processing attempts counter in a message and again assign status "New", to try process it again. Just make sure that background job is idempotent (can process same message multiple times and not corrupt data) and transactional (when job fails you must undone changes that was made. if any). When job fails after 5 attempts (config value) perform #1.

一旦修复了消息处理的错误,您就可以通过分配新"状态并移至消息队列来再次处理它,或者仅删除此消息.它实际上取决于业务流程.

Once bug with message processing was fixed you could process it again once more by assigning "New" status and moving to the message queue, or just delete this message. It depends on business processes actually.

这篇关于MongoDB作为队列服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆