18

I use php and laravel as my web service.

I want to know does laravel store and process requests in these situation?

  1. requests to different controllers from many users;
  2. requests to the same controller from the same user.

Is the laravel store these requests in a queue by the sequence the requests reached?

Is laravel parallel process requests for different users, and in sequence for the same user?

For example, there are two requests from the user. The two requests route to two methods in the same controller. While the first request will cost a long time for the server side processing, the second one will cost very little time. When a user set up the first request then the second one, though the second one cost very little time, the server side will not process the second request until it finish processing the first one.

So I want to know how does laravel store and process the requests?

HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
LF00
  • 27,015
  • 29
  • 156
  • 295
  • 7
    There are nothing about Laravel. This could help http://stackoverflow.com/questions/1430883/simultaneous-requests-to-php-script – rNix Dec 21 '16 at 06:05

2 Answers2

24

Laravel does not process requests directly, this is something managed by your webserver and PHP. Laravel receives a request already processed by your webserver, because it is only a tool, written in PHP, which processes the data related to a request call. So, as long as your webserver knows how to execute PHP and calls the proper index.php file, Laravel will be booted and process the request data it receives from the webserver.

So, if your webserver is able to receive 2 different calls (usually they do that in the hundreds), it will try to instantiate 2 PHP (sub)processes, and you should have 2 Laravel instances in memory running in parallel.

So if you have code which depend on anther code, which may take too long to execute depending on many other factors, you'll have to deal with that yourself, in your Laravel app.

What we usually do is to just add data to a database and then get a result back from a calculation done with data already in the datastore. So it should not matter the order the data get to the datastore, which one got in first, the end result is always the same. If you cannot rely on this kind of methodology, you'll have to prepare your app to deal with it.

enter image description here

Antonio Carlos Ribeiro
  • 86,191
  • 22
  • 213
  • 204
  • the two requests in the example have no dependency on each other, just processed by two methods in same controller. – LF00 Dec 21 '16 at 15:34
  • It doesn't matter, every request is independent, because they are called independently and spawned in different processes by PHP-FPM. That's how a user which asks for a static page will get it way before another user which asked for a really complex report. Edited to add a flow chart. – Antonio Carlos Ribeiro Jan 01 '17 at 16:01
  • Awesome diagram. Can you clarify a few things for me? In the diagram, it looks PHP FPM is taking care of the PHP part of the request life cycle. The web server will have already received the request and is calling out to PHP FPM to do more work on it. 1) Is PHP FPM spawning each process on demand and then ending it for each request? Or is it using some sort of "process pool" (I'm thinking along the lines of thread pools) to re-use them, clearing state in the Laravel instance each time it re-uses a process to handle a new request? – Matt Welke Dec 25 '22 at 01:47
  • 2) What type of IPC does the web server use to communicate with the PHP FPM system? The form of IPC I'm most familiar with is HTTP. But, I feel like if PHP were capable of receiving HTTP requests by itself, we wouldn't be using a web server in the first place. I'm also aware that if a process spawns another as a subprocess, it can communicate full duplex with it. Is the web server spawning PHP FPM as a subprocess and using those mechanisms to communicate with it while it receives requests? – Matt Welke Dec 25 '22 at 01:53
  • To add some more context, the reason I have these questions about the overall architecture is because I started web dev with other languages where the HTTP server built into the standard library. For example, Go. So PHP's style of doing things feels quite different. – Matt Welke Dec 25 '22 at 01:55
3

Everything in PHP starts as a separate process. These processes are independent form each other until some shared resource comes in Picture.

In your case one user is handled one session and sessions are file based by default. The session file is shared resource for processes which means you can only make one call to PHP at a time for one user.

Multiple user can invoke any number of processes at once depending on your systems capabilities.

anwerj
  • 2,386
  • 2
  • 17
  • 36