调度作业 - 优化设计
我正在为以下用例寻找最佳设计。
我正在构建一个系统,用户可以在其中创建具有结束日期/时间的实体。我需要安排一项作业,以便在到达结束时间后立即将这些实体的状态更改为“已过期”。
这是我能想到的两个解决方案
- 一个作业每分钟运行一次,并运行一个查询来检查过期的实体(endTime > sysdate)。 问题:数据库负载,每分钟运行此查询可能会给数据库带来负载。
- 创建实体后,立即为每个实体安排作业。 问题:系统中将创建太多作业,每天将创建 1000 个此类实体。
还有比上面两个更好的解决方案吗?人们一般如何做到这一点?
I'm looking for an optimal design for the following usecase.
I'm building a system where users create entities with end date/time. I need to schedule a job to change the status of these entities to Expired as soon as the end time is reached.
These are the two solutions that i can think of
- A job runs every minute and runs a query to check for expired entities (endTime > sysdate). Problem: Load on Database, Running this query every minute may put load on the database.
- Schedule a job for each entity, as soon as the entity is created.
Problem: Too Many Jobs will get created in system, 1000's of these entities will be created daily.
Is there any better solution than the above two ? How do people do this in general ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
每分钟一次查询并不是一个很重的负载。我有一个 Windows 服务,它执行以下操作:
管理 voip 会话的计划、启动它们、结束它们、开始录制、结束录制、清除录制驱动器上的录制空间、显示 OSD 消息。这些任务中的每一个都位于自己的带有计时器的线程中,并且大约每 4 秒运行一次查询。数据库中有超过 100,000 条记录,并且 Web 应用程序的 100 个用户同时访问同一数据库。它运行良好,已经运行了大约 3 年。
根据您的框架,可能有更好的选择,但我目前还没有找到。例如,.NET 有任务并行库,我还没有使用过,但我听说它可以提供一些东西。不管怎样,多年来我一直在使用方法#1,而且它从来没有让我错过。
当您考虑它时,即使是基于事件的系统也处于带有一些函数指针的循环中。问题是您是否手动管理它以及它是如何线程化的。数据库负载应该微不足道。
A query every minute is not a very heavy load. I have an windows service that does the following:
Manages a schedule of voip sessions, starts them, ends them, starts recordings, ends recordings, clears recording space on the recording drive, displays OSD messages. Each of these tasks are in their own thread with a timer and run their query about every 4 seconds. There are over 100,000 records in the database and 100's of users of the web app that accesses this same database at a time. It runs fine and has been for about 3 years now.
Depending on your framework, there may be better options, but I haven't found any at the moment. For instance, .NET has the Task Parallel Library, which I haven't used yet, which I hear has some things to offer here. Anyways, I have been using approach #1 for years, and it has never done me wrong.
When you think about it, even an event based system is sitting in a loop somewhere with some function pointers. The question is whether or not you are manually managing it and how it is threaded. The db load should be insignificant.
我认为你可以结合两种想法。
每小时运行一个获取数据的作业。
现在仅为每个在接下来的 60 分钟内过期的对象创建作业。
现在60分钟的时间只是一个例子..
更改它以满足您的需要。
I my opinion you can combine two ideas.
In every hour run a job that gets data .
Now create jobs for each of the objects that expire in the next 60 mins only.
now the 60 mins time is just example..
change it to suit your needs.