AI Hints
Home›System Design Tutorial›Batch, Stream and Schedules
System Design · Lesson 74 of 120

Scheduled Jobs

Cron at scale.

⏱ 2 min read▶ Video at 1:28:48🎓 Free
Watch this lesson in the System Design courseWatch on YouTube ↗Full System Design course

Every night at two, Shoply sends abandoned cart reminders. On one server, that's a simple cron job. But with twenty identical servers, each one would send the reminders. Customers get twenty emails.

Scheduled Jobs
From the video: Scheduled Jobs
  • Every night at two
  • One server: easy
  • Twenty servers: twenty emails

Running a job exactly once

There are standard fixes. Use a single scheduler service, separate from the web servers. Or use a distributed lock, so only the server that grabs the lock runs the job. Make the job idempotent, so running it twice is harmless anyway. And record each run, so you can see when one was missed.

One schedulernot on every server
Distributed lockfirst one wins
Idempotent jobtwice is harmless
Record runsspot missed ones

A lock for the nightly job

Here's the lock idea. All twenty servers wake up at two, and try to claim the job for tonight's date. The claim works only if nobody has claimed it yet, like a database insert with a unique key. Run it. Server eighteen wins the race, and the other nineteen skip the job.

nightly.pypython
import random
random.seed(4)

claims = set()   # stands in for a table with a unique key

def try_claim(job, date):
    key = (job, date)
    if key in claims:
        return False
    claims.add(key)
    return True

servers = list(range(1, 21))
random.shuffle(servers)        # they wake in random order
for s in servers:
    if try_claim("cart-reminders", "2026-11-12"):
        print(f"server {s} runs the job")
skipped = len(servers) - 1
print(f"{skipped} servers skipped it")
Output
server 18 runs the job
19 servers skipped it

Summary

Scheduled jobs must run once, even with many servers. Use one scheduler or a lock, and make the job idempotent. Next, a quiz on messaging.

Key takeaways
  • One scheduler or a lock
  • Idempotent jobs
  • Record every run
Next up: Messaging Quiz — test yourself