Design Ideas of Timed Tasks in Cluster Mode

Timing tasks are a relatively common business scenario in software development, a scenario that is strongly coupled with time and executed regularly, such as

  • User monthly and annual bill statistics
  • Timed backup of data
  • Order timeout cancellation
  • Regular pull or push of certain files

In order to ensure the robustness of the service, we usually deploy it in a cluster. When a scheduled task meets the cluster, what kind of sparks will it collide?

In the case of cluster deployment, we need to control the execution nodes of timed tasks, so that one node obtains the right to execute, instead of all nodes performing the same task, resulting in the same task being executed multiple times.

How to control the cluster nodes? In other words, what if the execution right is allocated to a certain node?

Solution one:

Multiple nodes can be understood as multiple threads, and the task to be executed can be understood as one resource, so the application scenario is converted to multi-threaded operation of the same resource. But the difference is that in the case of multi-threaded concurrency, the lock controls the resource to ensure that only one thread can operate the resource at the same time. After the thread is changed to release the resource, other threads can still obtain the resource for operation.
Multiple nodes can be understood as multiple threads, and the task to be executed can be understood as one resource, so the application scenario is converted to multi-threaded operation of the same resource. But the difference is that in the case of multi-threaded concurrency, the lock controls the resource to ensure that only one thread can operate the resource at the same time. After the thread is changed to release the resource, other threads can still obtain the resource for operation.
Cluster timing tasks also need to "lock" the "resources" (timing tasks to be executed), but this timing task can only be executed once and cannot be executed repeatedly. This is the difference from multi-threaded concurrent.

  • Database resources
    Before the timing task is executed, multiple nodes write the same data to the database (the primary key is the same), which node writes successfully, and which node executes the timing task. The node that fails to add data will no longer execute the timing task at that moment.

Guess you like

Origin blog.csdn.net/weixin_36908494/article/details/108490742