Transfer file takes too much time

Viewed 62

I have an empty API in laravel code with nginx & apache server. Now the problem is that the API takes a lot of time if I try with different files and the API responds quickly if I try with blank data.

Case 1 : I called the API with a blank request, that time response time will be only 228ms.

Case 2 : I called the API with a 5MB file request, then file transfer taking too much time. that's why response time will be too long that is 15.58s.

So how can we reduce transfer start time in apache or nginx server, Is there any server configuration or any other things that i missed up ?

When I searched on google it said keep all your versions up-to-date and use php-fpm, but when I configure php-fpm and http2 protocol on my server I noticed that it takes more time than above. All server versions are up-to-date with the current version.

1 Answers

This has more to do with the fact one request has nothing to process so the response will be prompt, whereas, the other request requires actual processing and so a response will take as long as the server requires to process the content of your request.

Depending on the size of the file and your server configuration, you might hit a limit which will result in a timeout response.

A solution to the issue you're encountering is to chunk your file upload. There are a few packages available so that you don't have to write that functionality yourself, an example of such a package is the Pionl Laravel Chunk Upload.

An alternative solution would be to offload the file processing to a Queue.

Update

When I searched on google about chunking it's not best solution for small file like 5-10 MB. It's a best solution for big files like 50-100 MB. So is there any server side chunking configuration or any other things or can i use this library to chunking a small files ? According to the library document this is a web library. What should I use if my API is calling from Android and iOS apps?

True, chunking might not be the best solution for smaller files but it is worthwhile knowing about. My recommendation would be to use some client-side logic to determine if sending the file in chunks is required. On the server use a Queue to process the file upload in the background allowing the request to continue processing without waiting on the upload and a response to be sent back to the client (iOS/Android app) in a timely manner.

Related