GOAT EngineerPreparing your learning space…

System Design · Practice problem · Easy

Run 3,000 Jobs That Come Due at Once

When many jobs come due together, accept them all at once, hold them in a durable queue, and let a counted set of workers run them at the pace your slowest dependency allows. Build that here, then run it.

Easy means one bottleneck to find. You solve it on a canvas that runs your design. The canvas needs JavaScript; everything on this page works without it.

What is the situation?

You run the back office for 3,000 small shops. Every shop's books close at midnight, and the moment they do, a job comes due to file that day's sales with the tax office's Filing Service. Your scheduler fires all 3,000 jobs in the same instant. You cannot refuse them and you cannot move them, because the closing time is the tax office's rule. Month end brings more filings than a normal night. The Filing Service takes about 100 filings a second, lets about 10 more wait in line, and turns the rest away. The scheduler waits up to 60 seconds for an answer on each job, then gives up and counts that job as failed. This problem starts the moment the jobs come due: working out when a job is due is already done for you. The Client on your canvas is the scheduler. The Filing Service is drawn as a store because it keeps a record of every filing it takes, and that record is how we check that a job really ran. Its account costs $120 a month, which counts against your budget. Design the path from the scheduler to the Filing Service, so every job runs and none waits too long.

What must your design do?

  • Every job runs: its filing is in the Filing Service's record by the time the run ends.
  • The scheduler gets a good answer for at least 99.9% of jobs within the 60 seconds it waits.
  • The oldest job still waiting to run is at most 60 seconds old on a normal night, and at most 120 seconds old at month end or after an outage at the Filing Service.
  • The system costs $700 a month or less.

What does Run test?

Run works without an account. It checks your design against 2 tests you can read before you start:

  • A quiet afternoon. Shops sent in corrections through the afternoon: 20 jobs a second for 30 seconds, never more than a few at once.
  • Midnight. At second 5, 3,000 jobs came due in the same instant, with a stray job about every 10 seconds around them. The run went on long enough to see every job finish.

What does Submit add?

Submit needs an account. It runs 2 more tests you cannot read in advance, and stops at the first one your design fails, showing what happened and why. Passing every test, or giving up, unlocks the written solution.

Which parts can you use?

Already on your canvas: Filing Service.

  • Load Balancer: about 50,003 a second, $18 a month each
  • App Server: about 2,000 a second, $45 a month each
  • Waiting Room: effectively unlimited, $25 a month each
  • Queue: effectively unlimited, $25 a month each
  • Worker: about 30 a second, $45 a month each
  • Database: about 5,000 a second, $120 a month each
  • Outbox Relay: about 5,000 a second, $40 a month each

Every connection takes 1 ms, and the budget is $700 a month.

What does this problem leave out?

  • Working out when a job is due. Every job arrives at the moment it comes due, nothing is held back until a later time, and moving a due time is not offered.
  • A server running out of threads or connections. A call waiting on a retry holds nothing here, so a design that keeps thousands of callers waiting is not charged for that, though a real one would be.
  • A real provider's replies. The Filing Service is a stand-in that keeps a record of each job. It sends no rate-limit headers, never changes its limit, and keeps one copy of a job it is sent twice.
  • A job that fails every time it is tried, and the dead-letter queue that would catch it.
  • Fairness between shops, and priorities between jobs.

Where does this problem fit?

Its category is Scheduling & workflows: Run the right thing once, on time. It practises queues & async, rate limiting & protection and reliability & ops.

For the whole system, read the interview breakdown Design a Distributed Job Scheduler (Cron).

See every practice problem.

By GOAT Engineer. Explore System Design · Privacy Policy · Contact.