Synchronous vs Asynchronous Work
Definition
Synchronous work
- In plain English
- Synchronous work is done while the user waits, before the response is sent.
- Think of it like
- It is like waiting at the counter while your sandwich is made.
Asynchronous work
- In plain English
- Asynchronous work is handed off to be done later, so the response can return right away.
- Think of it like
- It is like dropping off dry cleaning and getting a ticket to collect it tomorrow.
Why it exists
A refill is saved, but sending its confirmation takes another 800 ms. The user waits for work they do not need immediately.
A contract PDF takes three seconds to generate. Moving that work outside the request lets the app confirm acceptance sooner.
How it works
Background job
- In plain English
- A background job is a task that runs outside the user's request, often on separate worker machines.
- Think of it like
- Like the kitchen prepping sauces before the restaurant opens.
Worker
- In plain English
- A worker is a process whose job is to pick up and run background jobs.
- Think of it like
- Like the cleaner who works through the dry-cleaning tickets.
| Need | Choose | Example |
|---|---|---|
| Required before continuing | Synchronous | Payment approval |
| Can arrive later | Asynchronous | Receipt email |
A user requests a refill; the API saves it and a job, publishes the job, and responds before a worker sends SMS.
- Save the essential fact
Store the refill and its pending notification together in one database transaction.
- Hand off the work
Publish saved pending jobs to a durable waiting place and retry failed publishing.
- Respond honestly
Confirm the saved refill without claiming that the SMS has arrived.
- Run and monitor
Workers retry failed notifications and report jobs that remain stuck.
Writing the refill and then forgetting its notification after a crash is a real risk. Saving both together closes that gap.
Examples
Pharmacy refills
Saving the refill is synchronous. A background job sends its confirmation SMS after the response.
During a five-minute SMS outage, saved jobs wait for retries. Refills can succeed while essential storage and the handoff remain available.
Save refill and job → Confirm refill → Worker sends SMS later.
The insight
Do the essential part now, the rest later.
Recycling pickup
Route planning for 300 trucks runs overnight. A resident booking a pickup does not wait for that planning.
Booking a pickup saves the address and requested date. Overnight workers calculate tomorrow’s routes for 300 trucks.
- Monitor route completion before trucks depart.
- Alert staff if the overnight calculation fails.
The insight
Heavy work that nobody is waiting for belongs in the background.
Car rental
Generating a rental contract PDF takes three seconds. The app confirms acceptance and says the contract will arrive by email.
Show the job’s progress and a realistic estimate. Do not promise one minute unless the service can meet it.
Accepted rental → Saved PDF job → Worker builds PDF → Email contract.
The insight
Tell users honestly when something will arrive later.
Trade-offs
Pros
- Shorter request waits.
- Separate slow provider failures.
- Schedule heavy work off peak.
Cons
- Results arrive later.
- Saved jobs need monitoring.
- Safe handoff requires additional storage logic.
Use when
- The result can arrive after acceptance.
- Slow providers should not block essential work.
Avoid when
- The next user step needs the result.
- A tiny task needs no separate handoff.
Quick check
Takeaways
- Save essential work before responding.
- Hand work that can wait to background workers.
- Save pending jobs safely and monitor their completion.