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.

Begin by establishing the difference:
  • Synchrone : Action that is executed at the time of his call
  • Asynchrone: Action is not executed at the time of his call

Synchronous processing

We’ll start with an example of synchronous processing with the registration of a user on a site.

Below is a summary of actions that will be required:
  1. The user completes and submits a form
  2. Validation of data by application
  3. Insertion in a database
  4. Send an email confirmation of registration
  5. response returned to the user


If, however, step 4 is not performed correctly (eg the mail server down), the process is blocked and prevents the user to continue, or ask him to wait longer than necessary.

We’ll see how solve this problem by treating at least sending mail asynchronously

Asynchronous processing

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.

There is a lot of queuing system (ActiveMQ, IronMQ, RabbitMQ, Kafka, SQS …) that you choose as needed in your application. At BoostMyShop we mainly use RabbitMQ


Synchronous programming

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:



We’ll optimize it by treating parallel queries.

Parallelized queries.

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: promise

Asynchronous programming

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.

I/O (Input/Output)

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!

Event loop

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. event_loop


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.

However it seems clear that it is not possible to predict the order of arrival of the responses.


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.


Want to say something ?