How to Handle Long Running Operations in REST APIs
long running operations in REST APIs, avoid blocking the client by using asynchronous processing and returning a 202 Accepted status with a status URL. Clients can then poll this URL or use callbacks/webhooks to get the operation result once complete.Why This Happens
Long running operations take a lot of time to finish, like processing large files or complex calculations. If the server tries to handle them synchronously, the client waits too long and may time out or get errors.
This happens because HTTP requests expect quick responses, and blocking the request causes poor user experience and server overload.
from flask import Flask, request import time app = Flask(__name__) @app.route('/process', methods=['POST']) def process(): # Simulate a long task time.sleep(15) # blocks for 15 seconds return {'status': 'done'} if __name__ == '__main__': app.run(debug=True)
The Fix
Change the API to start the long task asynchronously and immediately return a 202 Accepted response with a URL to check the task status. This way, the client is not blocked and can check back later.
from flask import Flask, request, jsonify import threading import time app = Flask(__name__) # Store task status tasks = {} def long_task(task_id): time.sleep(15) # simulate long work tasks[task_id] = 'done' @app.route('/process', methods=['POST']) def process(): task_id = str(len(tasks) + 1) tasks[task_id] = 'running' thread = threading.Thread(target=long_task, args=(task_id,)) thread.start() return jsonify({'task_id': task_id, 'status_url': f'/status/{task_id}'}), 202 @app.route('/status/<task_id>') def status(task_id): status = tasks.get(task_id, 'not found') return jsonify({'task_id': task_id, 'status': status}) if __name__ == '__main__': app.run(debug=True)
Prevention
To avoid blocking clients with long operations, always design APIs to handle such tasks asynchronously. Use background workers, message queues, or async frameworks.
- Return
202 Acceptedwith a status URL. - Implement status endpoints for clients to poll.
- Consider webhooks or callbacks to notify clients when done.
- Use timeouts and retries on client side.
Related Errors
Common related issues include:
- Timeout errors: Client or server times out waiting for response.
- Resource exhaustion: Server blocks threads causing overload.
- Client confusion: No feedback on progress leads to poor UX.
Fixes involve async processing, status updates, and clear API contracts.