将数据库与消息队列解耦的最佳实践
我正在构建一个允许用户上传图像的网络应用程序。图片上传后,需要调整为一种或多种尺寸,每种尺寸都需要发送到Amazon s3进行存储。每种尺寸图像的元数据和 URL 存储在 Web 服务器上的单个数据库记录中。我使用消息队列来异步执行调整大小和上传(因为每个请求可能会出现大图像和多个调整大小)。当调整大小/上传任务完成时,需要使用 url 更新数据库记录。
我的问题是执行任务的工作人员将无权访问数据库。我正在考虑在任务完成后使用适当的信息来更新数据库记录,从工作线程向 Web 应用程序发起 http 回调。还有其他选择或理由我应该以其他方式这样做吗?
我使用 python/pylons 作为 Web 后端,使用 mysql 作为数据库,使用 celery/amqp 作为消息传递。
谢谢!
I am building a web application that allows a user to upload an image. When the image is uploaded, it needs to be resized to one or more sizes, each of which needs to be sent to Amazon s3 for storage. Metadata and urls for each size of the image are stored in a single database record on the web server. I'm using a message queue to perform the resizing and uploading asynchronously (as there is potential for large images and multiple resizes per request). When the resize/upload task completes, the database record needs to be updated with the url.
My problem is that the worker executing the task will not have access to the database. I was thinking of firing off a http callback from the worker back to the web application after the task is complete with the appropriate information for updating the database record. Are there any other alternatives or reasons I should do this another way?
I'm using python/pylons for the web backend, mysql for the database and celery/amqp for messaging.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来您的目标不是将数据库与 MQ 解耦,而是与工作人员解耦。因此,您可以创建另一个接收完成通知的队列,并让另一个工作线程来接收通知并相应地更新数据库。
It seems that your goal is not to decouple the database from the MQ, but rather from the workers. As such, you can create another queue that receives completion notifications, and have another single worker that picks up the notifications and updates the database appropriately.