# Asynchronism: Accept the Request Now, Do the Slow Work Later

> Learn how a system can reply at once and finish slow tasks in the background, and how the caller finds out the final result.

**In short:** Asynchronism means a system replies at once, finishes slow tasks in the background, and lets the caller find out the final result later.

From "System design", a free course by Venkat R. Murty: https://designervenkat.online/learn/system-design/asynchronism

## What You Will Learn

This lesson explains asynchronism. It is a design style where a system accepts a request, replies at once, and finishes the slow part later. By the end you should be able to find slow work inside a feature, decide whether that work can wait, and design how the user or another program learns the final result. You do not need any tools yet. Tools for running background work have their own lesson.

- **Asynchronism:** A design style in which the system accepts work now and finishes the slow part later, outside the time the caller spends waiting.
- **Caller:** Whatever sends a request, such as a web browser, a phone app, or another program.
- **Synchronous:** A flow where the caller sends a request and waits for the full answer before going on.
- **Asynchronous:** A flow where the caller gets a quick reply, and the real work finishes later on its own.
- **Latency:** The time one request takes, from the moment it is sent to the moment the reply arrives.
- **Capacity:** How much work a system can handle in a given amount of time.
- **Background job:** A piece of slow work that runs outside the user's request, so nobody waits on it.
- **Status:** A label that shows where a job is, such as queued, processing, done, or failed.

## What Is the Synchronous Flow Where the Caller Waits?

In a synchronous flow, every step happens inside one request. The caller sends the request, the server does all the work, and only then does the reply go back. This is simple to build and easy to follow. The trouble starts when one step is slow. The user's screen sits still, and the server keeps part of itself busy until the slow step ends.

### Why Do Slow Steps Hurt?

- **The user feels it:** A person who waits for a button to respond is slowed down each time. A slow step adds latency for that person every single time.
- **Other people wait too:** While one request holds server resources, fewer resources remain for everyone else. When many slow requests arrive together, they crowd out fast ones and reduce capacity.
- **Some work is simply long:** Sending an email, resizing images, or building a monthly report can take seconds or minutes. Making the reply wait for that work means the reply is late by the same amount.

## What Is the Asynchronous Flow?

An asynchronous flow splits one request into two parts. The first part is quick. The server checks the request, saves it, and promises to do the work. The second part runs later on a worker, which is a program that takes jobs from a line and finishes them. The first reply does not contain the final result. It contains a receipt, usually a job ID and an address where the caller can check progress.

Turning a normal web call into an asynchronous one is mostly a matter of answering early with the status 202 Accepted and handing the real work to a queue. A queue is a line that holds work until a worker is free. The steps below follow one job from start to finish.

1. **Check the request:** The server validates the input first. If the request is broken, it replies at once with an error such as 400 Bad Request, and no job is created.
2. **Accept and reply:** If the request is valid, the server saves the job and replies at once with 202 Accepted. The reply also says where to check the job's progress.
3. **Wait in the queue:** The job sits in a queue. The queue keeps the work safe even when every worker is busy.
4. **Do the slow task:** A worker takes the job, does the slow task, and updates the job's status as it moves along.
5. **Record the outcome:** The worker stores the result and marks the job done or failed, so the caller can find out what happened.

## How Does the Caller Learn the Result?

A job that finishes later must still tell somebody the outcome. There are three common ways to do this. The right choice depends on who is calling: a browser, a phone app, or another server.

### How Does Polling a Status Endpoint Work?

Polling means the caller asks again and again. The server offers a status address, which is a URL that reports the job's current state. The caller checks it every few seconds until the state is final. The reply to the first request can say where to check and suggest how long to wait between checks, so the caller does not flood the server.

Polling works with almost any HTTP client, and it does not need the caller to open a door for incoming messages. The cost is many small requests while a job is active.

```python
jobs = {}

def submit(job_id):
    jobs[job_id] = 'queued'
    return {'code': 202, 'status_url': '/jobs/' + job_id}

def run_worker(job_id):
    jobs[job_id] = 'done'

def get_status(job_id):
    return {'code': 200, 'status': jobs.get(job_id, 'unknown')}

reply = submit('pdf-1')
print(reply['code'], get_status('pdf-1')['status'])
run_worker('pdf-1')
print(get_status('pdf-1')['status'])

```

Output:

```text
202 queued
done

```

The sample above is a toy. It keeps jobs in a dictionary and runs the worker by hand. It shows the shape of the idea: the first reply says 202, and a later status check reports the real state.

> **Tip:** Keep the status endpoint separate from the job's own address. If a job exists but is not finished, return a normal success with a status such as processing. Returning 404 Not Found for a job that exists confuses callers, because 404 usually means the address is wrong.

### Webhooks and Push Messages

A webhook is a callback. The caller gives the server an address when it submits the job. When the job ends, the server sends the result to that address on its own. Webhooks suit server-to-server links, such as a payment company telling a shop that a refund has finished. The receiving side must offer a public HTTPS address that the sender can reach.

A third option keeps a live connection open, such as a WebSocket, so the server can push the result to a browser as soon as it is ready. This feels fast to the user, but it needs a connection that stays open, which adds work for both sides. Many designs use a status endpoint as a fallback in case the live connection drops.

## The Cost of Eventual Results

Asynchronous design buys speed, but the answer arrives later. For a short time, different parts of the system can disagree. Engineers call this eventual consistency: the system may not agree everywhere right now, but it should settle into the right state once the background work ends.

- **Failures:** A job can fail after the user has already seen a success message. The design must store the failure, tell the caller, and offer a way to try again. Async designs can control failures well, but only when someone plans the failure path.
- **Ordering:** The design must decide which order matters and protect it.
- **User feedback:** People need to know what is happening while work is pending. Clear names such as pending, processing, confirmed, and failed help both users and support staff understand the state.

Async also adds buffering, retries, duplicate handling, and monitoring. Each of these is extra work to build and to run. That is why async is a deliberate choice, not a free upgrade.

> **Tip:** Before you choose async, write down two things: what the user sees while the job is pending, and what the user sees if the job fails. If you cannot answer both, the design is not ready.

## Worked Example: A Video Upload

Video sites show the idea clearly. Uploading a file and preparing the video for viewing are two different jobs. Preparing means converting the file, called transcoding, into other sizes and formats so many devices can play it. One upload may need several resolutions and formats, and this work can take longer than the upload itself.

The steps below show how the upload screen stays quick. They describe the general pattern and what creators can observe in YouTube Studio. They do not describe YouTube's internal code.

1. **Receive the file:** The creator's browser sends the raw file to the service. This is the upload phase, where the creator's own computer sends the bits to the servers.
2. **Reply early:** When the file has arrived, the service saves it and marks the video as processing. The creator does not have to wait for the transcoding to finish.
3. **Queue the transcode:** The service adds a transcoding job to a queue. Transcoding workers pick up the job and create each output version.
4. **Publish a first version:** Low resolutions such as 360p are usually made first, so some version can play early.
5. **Mark it ready:** When the needed versions exist, the video's status changes to ready, and the watch page works for viewers. Until then, the status tells everyone the video is still in progress.

> **Tip:** Design write-ups disagree on when a video counts as ready. One pipeline sketch marks it available only after every resolution is done. Another lets it play at low quality while higher versions continue. Pick a rule on purpose and show it on the status page.

> **Tip:** Studio labels name a stage, not an error, and a status display can lag behind. Still, a job with no progress for a long time should trigger an alert for the team.

## When to Go Async, and When Not To

Async is a tool, not a default. Most good designs mix both styles. They keep a short synchronous path for what the user needs right now, then move the rest to background work.

- **Slow work:** Image processing, report building, and sending batches of messages can take seconds or minutes. Users should not wait for them, so they are good async candidates.
- **Non-urgent work:** If a result can arrive a minute later with no harm, such as a receipt email, async fits well.
- **Bursty work:** When traffic spikes, a queue can hold the extra jobs while workers catch up. This keeps the front door responsive during a rush.
- **Needed right now:** If the next screen cannot be built without the answer, such as a login check or a payment approval, keep the call synchronous.
- **Urgent messages:** A message that must be acted on immediately is a poor fit for async, because the delay of the queue may be too long.

### Check Your Understanding: Checkout

**Quick check:** A checkout page takes 8 seconds. It charges the card, makes a PDF invoice, and emails a receipt before it shows the thank-you page. The team wants a faster page without losing the order's accuracy. Which plan is best?

A. Move all three steps to background work and show the thank-you page before the card is charged.
B. Keep all three steps synchronous and buy a faster server so the page loads sooner.
C. Keep the card charge synchronous, move the invoice and receipt email to background work, and show the order status with a notice if the invoice fails.
D. Move the card charge to background work, and keep the invoice and email synchronous so the user sees them before the page loads.

**Answer:** C. The user needs to know the payment succeeded before the order counts, so the charge stays synchronous. The invoice and email can finish later. The order page shows their status, and a failed invoice is reported with a retry option, so the user is not left guessing.

### Check Your Understanding: Export Results

**Quick check:** Your service builds a large data export that takes about 20 minutes. A partner's server must know the moment it is ready. The partner's server has a public HTTPS address that your service can reach. Which approach fits best?

A. The partner's server checks the status address every 100 milliseconds for 20 minutes.
B. The partner's server holds one open request for 20 minutes and waits with no status page.
C. Your service sends a webhook to the partner's address when the export finishes.
D. A person at the partner company refreshes the download page by hand until the file appears.

**Answer:** C. A server-to-server link with a reachable address is the case webhooks were built for. The partner receives one message when the work ends, so no constant checking or long open connection is needed.

## Key Takeaways

Asynchronism means accepting work fast and finishing the slow part later. It keeps users and callers from waiting and helps the system handle more load. The price is that the result arrives later, so the design must say how the caller finds out. Use polling, webhooks, or live push, plan for failures and ordering, and keep the steps that need an answer right now synchronous.

## Sources

- [Microsoft Learn: Asynchronous Request-Reply pattern (Azure Architecture Center)](https://learn.microsoft.com/en-us/azure/architecture/patterns/asynchronous-request-reply)
- [DesignGurus: Synchronous vs Asynchronous Communication in System Design Interviews](https://www.designgurus.io/blog/sync-vs-async)
- [AlgoMaster: Sync vs Async Communication](https://algomaster.io/learn/system-design/sync-vs-async-communication)
- [SysDesAi: Async Request-Reply Pattern (Messaging and Communication Patterns)](https://www.sysdesai.com/learn/messaging-patterns/async-request-reply)
- [Akin Gundogdu: Async Request-Reply Pattern in Microservices](https://www.akingundogdu.com/blogs/async-request-reply-pattern-in-microservices/)
- [Codefarm: Designing YouTube, Video Upload, Transcoding and Streaming at Scale](https://codefarm.in/guides/system-design/09-expert-case-studies/youtube-video-streaming)
- [TechInterview: System Design, Video Processing Pipeline (YouTube/Netflix)](https://www.techinterview.org/post/3233464658/system-design-video-processing-pipeline/)
- [Fast.io: YouTube Upload Limit, File Size, Video Length, and Quotas](https://fast.io/resources/youtube-upload-limit/)
- [AetHoVPN: YouTube Video Is Stuck Processing HD: What Up to SD Means](https://www.aethovpn.com/en/blog/entertainment/youtube-video-stuck-processing-hd)
- [Swarmify: How Long Does YouTube Take to Process a Video?](https://swarmify.com/blog/how-long-does-it-take-youtube-to-process-video/)
