Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Background Jobs  ›  Lesson

Event-Driven Jobs

Event-Driven 12 minAn event-driven background job is started by something that happened, usually a message on a queue, so the user's request can finish without waiting for the slow work.

Event-Driven Jobs

When someone creates a Hop link, Hop also fetches the target page to read its title and check it against a malware list. Done inside the request, that can take four seconds while the user watches a spinner. None of that work needs to finish before the user gets their short code. It just needs to happen.

The rule

Something happened → drop a message → a worker does the slow part.

A background job is work done outside the user's request. An event-driven job starts because something happened:

  1. The request does only the essential part — save the link — and publishes an event, a small message such as link_created {id: 8812}, onto a queue.
  2. The request returns to the user immediately.
  3. A separate worker process takes messages from the queue and does the slow part.
user ─▶ Hop API ─(save + publish)─▶ queue ─▶ worker ─▶ fetch page, scan, update link
          │
          └─▶ "Here is hop.example/x7Kp" (in milliseconds)

Other events that suit this pattern: a user signs up (send a welcome email), a file is uploaded (make a thumbnail), an order is paid (start shipping).

Workers bring two useful properties. If work piles up, add more workers. If a worker crashes mid-job, the message stays on the queue and another worker retries it. That last point means a job may run twice, so jobs should be safe to repeat: "set status to safe" is fine to do twice; "add 1 to the count" is not.

The shortcut

For each step in a request, ask: "Does the user need this result before they can continue?" Yes → keep it in the request. No → make it an event and a job.

Splitting Hop's create-link requestStep 1 of 4
  1. Save the link

    The user needs the short code now → stays in the request.

The trap is forgetting the half-finished state. Between "saved" and "scanned", what does Hop do if someone clicks the link? Decide it explicitly — here, show a short "checking this link" page.

Drills

Answer fast — trust the rule.

quick drill

What starts an event-driven background job?

quick drill

Which task is the best candidate to move into a background job?

quick drill

A worker crashes halfway through a job. What usually happens?

quick drill

Because jobs can be retried, which job step is safest?

Remember

  • Something happened → drop a message → a worker does the slow part.
  • Shortcut: "does the user need this before continuing?" No → make it a job.
  • Trap: forgetting the half-finished state, and jobs that are unsafe to run twice.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainMove the safety scan out of the request

When a user creates a Hop link, Hop currently fetches the target page to read its title and check it against a malware list inside the request, which takes up to 4 seconds. Redesign this as an event-driven background job: describe the steps from the user's click to the finished check, and explain what the user experiences.