Returning 200 within 3 Seconds

I’ve successfully deployed my small project, but I’m encountering an issue. My project has a callback URL where external systems send POST requests. Upon receiving the request, I process the data and return a 200 response.

The challenge I’m facing is that I need to send the 200 response within 3 seconds of receiving the request, while still processing the data (which, in my case, involves saving the data to MongoDB—it’s a simple operation).

I’ve tried several approaches, including using threading and async methods, but none of them seem to work within the 3-second window. I don’t want to resort to paid services like message queues (I’d prefer paying for Vercel if this issue can be resolved).

I also tried making the receiving process ping another URL for processing, but often the process gets terminated because the response is sent before the communication is established (this is based on my observation, though not completely certain).

My deployment is based on Python Flask. I’m looking for suggestions or solutions to handle this scenario, where a quick 200 response is returned while still processing the data in real-time. Any help would be appreciated!

@app.route('/callback_live', methods=['POST'])
def callback_live():
    pushed_data = request.json
    print("Received Push data", pushed_data)

    if pushed_data.get('code') == 0 and 'verify_info' in pushed_data.get('data', {}):
        print("Verification ping detected. No processing needed.")
        response = jsonify({"status": "success"})
        response.status_code = 200
        return response
    
    # Process the data if it's not a verification ping
    print("Processing data...")
    process_callback_json(pushed_data)

    # Threading is not possible, turned off
    # threading.Thread(target=process_callback_json, args=(pushed_data,)).start()

    # Return success response after processing
    response = jsonify({"status": "success"})
    response.status_code = 200
    return response