What if your app never froze waiting for slow tasks again?
Why Long-running operations (async responses) in Rest API? - Purpose & Use Cases
Imagine you send a request to a server to process a big file or run a complex report. You wait and wait, but the server takes a long time to respond. Meanwhile, your app is frozen or the user wonders if it even works.
Waiting for the server to finish everything before replying makes your app slow and unresponsive. If the process takes minutes, the user might give up or the connection might time out. Also, the server can get overloaded handling many long tasks at once.
With asynchronous responses, the server quickly replies that the task started and gives a way to check progress later. This frees your app to keep working smoothly while the server finishes the job in the background.
POST /process (wait until done, then respond)
POST /process
respond immediately with task ID
GET /status/{taskID} to check progressThis lets apps handle big or slow tasks without freezing, improving user experience and server efficiency.
Uploading a large video to a website: the server accepts the upload, immediately replies with a job ID, and processes the video in the background while you can continue browsing.
Waiting for long tasks blocks apps and frustrates users.
Async responses let servers reply quickly and finish work later.
Users get smoother experiences and can check progress anytime.
Practice
Solution
Step 1: Understand client-server interaction in async operations
Clients start a task and receive a URL to check progress later, avoiding waiting.Step 2: Identify the benefit of async responses
This approach keeps apps responsive and user-friendly by not blocking clients.Final Answer:
To let clients start a task and check its progress later without waiting. -> Option BQuick Check:
Async responses = clients check progress later [OK]
- Thinking server processes all requests instantly
- Assuming client waits for task completion
- Ignoring the use of status URLs
Solution
Step 1: Recall HTTP status codes for async start
202 Accepted means the request is accepted but processing is not complete yet.Step 2: Compare with other codes
200 OK means completed, 404 means missing resource, 500 means server error.Final Answer:
202 Accepted -> Option CQuick Check:
Async start = 202 Accepted [OK]
- Using 200 OK for async start
- Confusing 404 with async status
- Assuming 500 means task started
{"status": "accepted", "status_url": "/tasks/123/status"}What should the client do next to get the task result?
Solution
Step 1: Identify the purpose of status_url
The status_url is provided for the client to check the task progress or result.Step 2: Determine correct HTTP method to check status
GET requests are used to retrieve information, so client should GET the status URL.Final Answer:
Send a GET request to /tasks/123/status to check progress. -> Option DQuick Check:
Check progress = GET status URL [OK]
- Using POST instead of GET to check status
- Expecting server to push result without polling
- Using DELETE to check or cancel unintentionally
response = start_task()
if response.status_code == 202:
status_url = response.json()["status_url"]
result = requests.post(status_url)
print(result.json())What is the error in this code?
Solution
Step 1: Review HTTP method used to check status
The code uses POST to check status, but status URLs require GET requests.Step 2: Confirm other parts of code
Checking status_url existence and status_code handling are good practices but not the main error here.Final Answer:
Using POST instead of GET to check the status URL. -> Option AQuick Check:
Status check = GET, not POST [OK]
- Using POST instead of GET for status check
- Ignoring missing status_url key
- Not handling unexpected status codes
Solution
Step 1: Understand async response design
Server accepts task, returns 202 with status URL for client to poll progress.Step 2: Evaluate other options
Immediate processing (200 OK) blocks client; 404 is wrong status; holding connection open blocks client.Final Answer:
Client POSTs image, server returns 202 Accepted with a status URL; client polls status URL until done. -> Option AQuick Check:
Async design = 202 + status URL + polling [OK]
- Returning 200 OK immediately for slow tasks
- Using 404 for slow processing
- Keeping connection open until done
Ready to prove it?
Pass the test and this pattern moves unknown
