DuckTeam is the Nickname Of the BoostMyShop Dev Team. Each week the DuckTeam has a Training Camp based on the latest Web Technologies, Technical Innovations or some softwares interesting the Team members.
This week we discuss the treatment and synchronous / asynchronous programming.
The objective of this article is to briefly present the different ways to make asynchronous, be it in architecture or directly within the code itself.
- Synchrone : Action that is executed at the time of his call
- Asynchrone: Action is not executed at the time of his call
We’ll start with an example of synchronous processing with the registration of a user on a site.
- The user completes and submits a form
- Validation of data by application
- Insertion in a database
- Send an email confirmation of registration
- response returned to the user
We’ll see how solve this problem by treating at least sending mail asynchronously
Using the previous example, we will simply replace the sending mail by sending a évennement in a queuing system (FIFO: First In First Out).
A FIFO is simply a queue in which we will stack the requests send by email, and be able to give control to the user even before the email is sent. These applications will then be processed asynchronously by another process dedicated to sending mail.
The advantages of process requests for asynchronous mail shipments are manifold, but the 3 most significant are:
- Avoid unnecessary waiting for the user
- Tolerant to failure (at least for the email server)
- Parallelization tasks for more consumer (worker) and / or multiple servers (horizontal scalability Comfort)
Asynchronous processing requires however an amendment to the level of the architecture with the addition of a new service (Message queuing) that will support recording and distributing photographs of events.
After seeing the synchronous and asynchronous processing, we will talk about synchronous and asynchronous programming. Let’s start with the synchronous programming.
Take the case of an application requiring to several API calls to retrieve information about the Star Wars books via the Google API example:
Several options are possible to retrieve this information.
The first reaction we might have is to simply iterate over all URLs to call this way:
This code is easy to read and understand. But the big problem to retrieve this information in this way is the processing time. The script will take at least 4 Cumulative HTTP requests in total, + processing time in PHP:
This method is to send all HTTP requests simultaneously, and begin to treat them once all the responses. In the example below we will use GuzzleHttp:
It is already much better than sequential treatment, but we realize that we can not start treating our answers queries only when all HTTP requests processed, which can remain insufficient if PHP processing time is therefore:
Now we’ve seen 2 options to proceed synchronously, we‘ll see what we can make asynchronous programming. But before you provide an example of how we will approach 2 points namely, the event loop and i / o.
The first notion to know, regarding the I / O (Input Output). Put simply, an I / O is any input output of a program which is independent of the CPU (memory access, network access, disk access). The I / O are two features of interest. The first is that we are unable to determine the end, and the second is that I / O is blocking!
The event loop is a single loop that will wait photographs of events and distribute messages. For example, waiting for an event corresponding to the end of an HTTP request once it received it, the event loop will be in charge of distributing messages to different « listener« . The listener are simply services that will provide the event loop a callback method to call for a given évennement.
To treat our HTTP requests asynchronously, we‘re going to use ReactPHP. ReactPHP will therefore enable us to continue the execution of your program while our HTTP queries return an answer:
So we perceive that we can begin processing the first response to our requests even before all requests are completed.
We flew over the subject, but the aim of this article was to present the various options available to consume several APIs in the same script, for example, or reduce the display time of registration page.