防止平行执行缩放的ASP.NET核心托管服务

发布于 2025-02-04 14:37:49 字数 527 浏览 3 评论 0原文

我在AWS(AWS Fargate)上托管了Web API。 我想用背景服务。 问题是,如果我决定我的Web API扩展将执行该托管服务的多个实例。

我有SQL数据库和一些表“ Isemailsent”和“ Isreportsent”标志。

背景托管服务只需循环通过该字段是错误的所有行,因此该服务的隐含性可能相对容易。

但是,在“防止”多个实例运行的托管服务方面,有几个选项。在这种设置中,最简单的方法是什么? 首先,这是我的想法:

引入某种数据库锁定,这可能是危险的,而且表现不太表现。

使用AWS SQS和内部背景服务的Dequeing是选项,但希望在不需要的情况下避免使用它,因为我当前在应用程序中的任何地方都不使用SQS。

I have web api hosted on AWS (AWS fargate).
I want to make sending some emails and other stuff as Background service.
Problem is that in case I decide my web api to scale that multiple instances of that hosted service will be executed.

I have sql database and some table where "IsEmailSent" and "IsReportSent" flag exist.

Background hosted service just loops through all rows where that fields are false so implmenetation of that service could be relatively easy.

However, when it comes to "preventing" multiple instances run hosted service there are couple of options. What would be most easy way in this kind of setup?
This is what to my mind first:

Introducing some kind of database locking which could be dangerous and not too performant.

Using AWS SQS and dequeuing inside background service is option but would like to avoid it if not needed since I currently don't use SQS anywhere in app.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

巷子口的你 2025-02-11 14:37:49

为了管理多个实例,以免获得相同的项目,您需要一个“推”系统。通过队列实现的最佳方法。

如果您不想使用队列,另一种方法是控制实例,仅在一个实例中“启动”工人。您可以使用一个简单的数据库选择可用于项目的实例。

但是我重复一遍,最好的方法是通过队列或钩子

To manage multiple instances to not get the same item you need a "push" system. The optimum way to implement that is via queue.

If you don't want to use a queue, the other way is to control the instances, and "start" the worker in only one instance. You can use a simple database to select wich instance is available to work on items.

But I repeat, the best way to do it is via Queue or via Hooks

一个人练习一个人 2025-02-11 14:37:49

我有几个想法如何解决这个问题。

第一个,尝试将硕士服务成为您的多标准服务之一。这是唯一可以处理命令以及您可以在哪里开始电子邮件发送电子邮件的服务。其他服务将仅处理查询(获取方法)。为此,您可以使用配置文件。并运行具有不同配置的主人和奴隶。您可以找到另一种根据您的基础架构的方式。目标仅运行一项背景服务。这不喜欢这种方式。

第二个需要引入额外的服务。它也可以是带有运行背景服务的.NET应用程序,但是从读取数据库和发送电子邮件中,它一定无能为力。您将此应用程序作为一个实例运行。它没有缩放。另外,您可以使用Webhook从数据库中解除此服务。它允许您订阅某些事件并发送电子邮件,而无需访问其他服务的数据库。
如果您想缩放电子邮件发送者服务可以通过负载平衡器来解决它,或者应该查看事件总线(如果您已经在堆栈中使用它,则是不错的选择)。我比第一个更喜欢这个。

I have several idea how you can solve this problem.

The first one, try to make master service the one of your multiscaled services. It is the only one service who can handle commands and where you can start the background service for e-mail sending. Other services will handles only queries(GET methods). To achieve this you can use configuration file. And run master and slaves with different configuration. You can find another way that is more according your infrastructure. The goal is run only one background service. This don't prefer this way.

The second one require to introduce an additional service. It can be also .NET app with running background service, but it must do nothing else from reading DB and sending e-mail. You run this application as one instance. It haven't to be scaled. Also you can use WebHook to decouple this service from database. It allows you subscribe to some events and send e-mails without accessing DB of another service.
If you wanna scale e-mail sender service can solve it by load balancer or you should look at event bus(if you already use it in your stack it is good choice). This I would prefer more than the first one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文