Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

enthusiast14's avatar

Doing tasks after sending response from API

I created a API in laravel and post requests come from external web app/android mobile app to my API.

Now what i want is that after i send json response from the API to the API caller, i want to save data to mysql table. From web i found that terminable middleware can do this - https://laravel.com/docs/5.8/middleware#terminable-middleware by which i can do my time consuming tasks after i sent response to the API caller so that i do not make api caller waiting for a long time.

is that right way i am going? what i have to keep in mind when implementing terminable middleware??. And does it replace laravel queue in some way?.

0 likes
5 replies
automica's avatar

@enthusiast14 I would handle this by firing an event to do your tasks once you hit the endpoint.

This allows you to be able to be able to queue your process intensive tasks, keep a track on their progress and also log when they have ran.

https://laravel.com/docs/7.x/events

I've not used terminable middleware, but it does also look like an option, depending on how intensive your save is. The problem there is that if many people hit the that endpoint, it could still overload your server resources.

enthusiast14's avatar

@automica my goal was to make a response with success message to the API caller who called my API endpoint to get/save data as fast as possible, then i will save related data to mysql. But you are saying event firing which going to happen after i send response to api caller?!, it's look like event related with queue.

But actually i want to compare queue with my easy techniques like terminable middleware. i am not using queue right now because of headache of using queue worker and making it up and running always in shared hosting.

automica's avatar

@enthusiast14 what are you intending to indicate with your success message? if you are returning a success to indicate they have 'done the thing/ save successful' then you'll want to do the thing first and then return success when that is successful.

A save should be quick enough to do inline, and there's nothing wrong with an API waiting for a second or 2 to return a response.

if you are worried about performance when the app is being used intensively, then I would worry about that at that point and consider that a good reason to move away from your shared server.

enthusiast14's avatar

@automica well what i am sending in success message is a curl call validation, then i am going to save data to mysql optionally which is not important, so is my first priority is sending the success message to the API caller.

Please or to participate in this conversation.