System Design Interview Guide [Distributed Task Scheduling]

Click "JavaEdge" below and select "Set as Star"

Pay attention to technical information as soon as possible!

Disclaimer~

Don’t overthink any article!

Everything cannot withstand scrutiny, because the world does not have the same growth environment, nor the same level of cognition, and“There is no solution that applies to everyone”< /span>;

Don’t rush to judge the views listed in the article, just put yourself into it and take a moderate look at yourself. You can “jump out and look at the current situation from an outsider’s perspective. What stage are you in before you are a commoner?.

What you think and do is all up to you"Find the path that suits you through constant practice"

1 Introduction

A task is a period of computing work that requires resources (CPU time, memory, storage, network bandwidth, etc.) to be completed within a specified time.

A system that satisfies task-level and system-level goals by intelligently allocating resources to tasks is called a task scheduler.

Task scheduler:

26533f3b84b1d58a408f3aaafd256d4d.png

The process of timely decision and allocation of resources to tasks is called task scheduling.

When we leave a comment on Facebook. We don't make comment posters wait until that comment is delivered to all followers. Delivery is delegated to an asynchronous task scheduler to be completed offline.

In a distributed system, many tasks run in the context of a single request from a user. Consider that popular systems like Facebook, WhatsApp or Instagram have hundreds of millions of users. These systems require a task scheduler to handle billions of tasks. Facebook uses Async to schedule all of its tasks based on billions of parallel asynchronous requests from its users.

Async is Facebook's own distributed task scheduler that schedules all its tasks. Some tasks are time-sensitive, such as tasks that should be run to notify the user that an event has started to be broadcast live. It would be pointless if the user only receives the notification after the live broadcast has ended. Certain tasks can be deferred, such as making friend suggestions to the user. Async schedules tasks according to appropriate priorities.

2 requirements

  • Availability: The system should be highly available for scheduling and executing tasks

  • Persistence: Tasks received by the system should be persistent and should not be lost

  • Scalability: The system should be able to schedule and execute an increasing number of tasks every day

  • Bounded wait time: This is how long the task needs to wait before starting execution. We cannot perform tasks after the expected time. Users should not wait indefinitely. Users should receive notifications if their wait time exceeds a certain threshold

3 Component design

3.1 Task scheduler architecture design

6564c9ad8e33cd2a19c4f49b466e1fbf.png
① Task Submitter (task submitter)

Accept the mission. There is no single task submitter. Instead, we have a set of nodes that receive more and more tasks.

② Database

All tasks received by task submitters are stored in the distributed database. Use a relational database to store:

  • task IDs

  • user IDs

  • Required resources

  • Execution cap

  • Total number of client attempts

  • Delay tolerance

  • ...

A non-relational database using a directed acyclic graph (DAG), a graph data structure that stores task-dependent data.

③ Batching and prioritization (batch processing and priority)

After storing the tasks in RDB, batch the tasks. Priority is based on attributes of the task, such as:

  • Delay tolerance

  • Or perform short-term tasks, etc.

Push the highest K priority tasks to the distributed queue, with K limiting the number of elements that can be pushed to the queue. The K value depends on many factors, such as:

  • Currently available resources

  • client

  • or task priority

  • Subscription level

④ Queue manager (queue manager)

The queue manager adds, updates, or deletes tasks from the queue. It keeps track of the type of queue we are using. It is also responsible for keeping the task in the queue until successful execution. If task execution fails, the task will appear in the queue again. The queue manager knows what queues should be run during peak hours and off-peak hours.

⑤ Resource manager

Know which resources are free. It pulls tasks from distributed queues and assigns resources to them. Resource manager:

  • Track the execution of each task

  • and sends its status back to the queue manager

If a task exceeds its capabilities or required resource usage, the task is terminated and status is sent back to the task submitter, who will notify the client about the task termination via an error message.

4 Execution upper limit

4.1 Task classification

  • Tasks that cannot be postponed - Urgent tasks

  • Deferrable tasks

  • Tasks that need to be performed regularly - periodic tasks

Multiple queues based on task category:

d7ea281a33e1c754eea8710b8dd08ae4.png

The system needs to ensure that tasks in the non-urgent queue are not starved to death. Once the latency limit of some task is about to be reached, it is moved to the urgent task queue for priority service.

4.2 Priority

Some tasks take a long time to execute and occupy resources, blocking other tasks. When scheduling tasks, the execution cap (execution cap) is an important parameter.

If we fully allocate resources to a single task and wait for that task to complete, some tasks may not stop and fail to complete execution due to task script errors. We allow users to set execution caps for their tasks. Stop task execution after the specified time, release resources and assign them to the next task in the queue. If task execution is stopped due to an execution limit, the system will notify these instances of the user they belong to. They need to take manual measures to deal with this situation.

5 Urgent execution of tasks

Some tasks need to be carried out urgently. For example, in the Facebook social application, users can mark themselves as safe in emergency situations, such as earthquakes. The tasks to perform this activity should be performed in time otherwise this feature will be useless to Facebook users. Sending an email notification to a customer that a certain amount of money has been debited from their account is another example of a task that needs to be performed urgently.

is a priority task, and the task scheduler maintains a delay tolerance (delay tolerance) parameter for each task, and when it is close to The task is executed when the delay tolerance is reached.

Delay tolerance is the maximum amount of time that task execution can be delayed. Tasks with the shortest delay tolerance are executed first. By using the latency tolerance parameter, tasks with longer latency tolerances can be postponed during peak hours to make room for urgent tasks.

6 Resource capacity optimization

Sometimes resources are close to the overload threshold (such as exceeding 80% utilization), which is the peak period. The same resource may be idle during off-peak hours. Therefore, it is necessary to consider how to better utilize resources during off-peak hours and how to keep resources available during peak hours.

Some tasks don't need to be performed urgently. For example, in Facebook social applications, suggesting friends is not an urgent task. It is possible to create a separate queue for such tasks and execute them during off-peak hours. If we consistently have more work to do than available resources, we may run into capacity issues and it's time to provision more resources.

7 Task idempotence

If the task executes successfully but for some reason the machine is unable to send the acknowledgment, the scheduler will schedule the task again. Perform the task again.

We don't want the final results to change when we run the task again. This is crucial for financial applications when transferring money. We require tasks to be idempotent. Idempotent tasks produce the same result no matter how many times they are executed.

This property is added by the developer in the implementation, by something (such as a name) to identify the property and override the old one.

8 Assessment

8.1 Availability

Task submission is completed by multiple nodes. If the node that submitted the task fails, other nodes will take over its place. The queue of push tasks is also distributed in nature to ensure availability. Since resources are constantly monitored for need to be added or removed, an effort is made to ensure that resources are always available. Every component in the design is distributed, greatly enhancing the availability of the entire system.

8.2 Persistence

We store tasks in a persistent distributed database and push tasks to a queue as execution time approaches. Once a task is submitted, it remains in the database until execution is complete.

8.3 Scalability

The task scheduler provides scalability because task submitters are distributed by design. More nodes can be added to the cluster to submit a large number of tasks.

These tasks are then saved into a distributed relational database that is also scalable.

Tasks are then pushed from the RDB to a distributed queue, which can scale as the number of tasks increases. More queues can be added for different types of tasks. More resources can also be added based on the resource to demand ratio.

8.4 Fault tolerance

Tasks are not removed from the queue when they are first sent for execution. If execution fails, the maximum allowed number of retries will be attempted. If the task contains an infinite loop, the task will be terminated after the specified time and the user will be notified.

reference:

  • Programming Selection Network

write at the end

Programming Select Network (www.javaedge.cn), a lifelong learning website for programmers, is now online!

Click to read the original text and visit the website!

Welcoming长按图片加好友, Our first time together and sharing软件行业趋势, 面试资源, 学习途径etc.

a432badc98eadff66e52b9362ee5f555.jpegAdd friends' notes [Technical Group Communication] to bring you into the group, where you can find more tutorial resources.

After following the official account, send a private message in the background:

  • Reply[Architect], get the architect learning resource tutorial

  • Reply to [Interview] to obtain the latest and most complete interview materials for major Internet companies.

  • Reply to [Resume] to get a variety of resume templates with beautiful styles and rich content.

  • return route line, Java P7Technology management complete list of best learning routes

  • ReplyBig Data, get Java transformation The most comprehensive mind map of the entire Internet for big data research and development

  • WeChat [ssshflz] private message [Side Business], join the side business communication group

  • Click[Read the original text] to access One-stop learning website for programmers

Guess you like

Origin blog.csdn.net/qq_33589510/article/details/134725203