Use power of async Node.js in existing php project for faster operations.

Use power of async Node.js in existing php project for faster operations.

First of all, we need to understand what is async code and in which use cases we should use it.

synchronous code:

Generally, in our program code runs line by line. So sometimes in slow operation code has to wait until previous code finishes its execution. So, it is blocking code. But sometimes we can run our next code without waiting for previous code but synchronous code is not allowing us to do that. So, in these cases we should write asynchronous code.

asynchronous code:

Async program is like parallel execution of code. We are not waiting for previous code to complete its execution. So, code takes less time to complete its execution which leads in faster computing.

Let's understand difference between sync and async code by example.

Case:

Suppose there is an API which will give list out today's orders. The number of orders can be from few thousands to few million. But the API will send only 250 records at a time.
Suppose there are 100k total orders and API needs to be called at 400 times (100k / 250) to retrieve all the orders. Assume API is taking 1 second to return data then it will take around 400 seconds to get the list of all 100k data apart we needs to wait for 400s (7 minutes). No one can wait for that much time.

Here is the example of sync php code vs async Node.js code.
Normal sync php code:

<?php
$apiUrl = 'http://example.com/orders/';
$recordsPerPage = 250;
$response = sendRequest($apiRequest, ['start'=> 0, rows=> $recordsPerPage]);

// Now when we get first api response we will get totalRecords in api response.
// Suppose we get 100k records then we need to iterate loop for 400 times.

$apiResult = [];
for ($i=0; $i < (totalRecords / recordsPerPage); $i++) {
  $start = recordsPerPage * $i;
  $response = sendRequest($apiRequest, ['start' => $start, 'rows'=> $recordsPerPage]);
  $apiResult[] = $response;
  $i++;
}

// here we will have final apiResult after 400s (7 minutes).

?>

Now write that same code in async nodejs. First we will call api for first time. By that we will get totalRecords and then we will call next api calls asynchronously. For that we are using node async library.

async function fetchOrders() {
  apiUrl = 'http://example.com/orders/';
  recordsPerPage = 250;
  response = await sendRequest($apiRequest, ['start' => 0, 'rows'=> recordsPerPage]); // Waiting for api response
  totalRecords = response.totalRecords;

  // Now we have total 100k records. So just make array of all urs.
  urls = [];
  for (i=0; i< totalRecords/recordsPerPage; i++){
    urls.push(`http://example.com/orders/start={recordsPerPage * i}&rows={recordsPerPage}`);
    i++;
  }

  // urls array will be like [http://example.com/orders/start=0&rows=250, http://example.com/orders/start=250&rows=250, ... http://example.com/orders/start=99750&rows=250]
  // For below execution of code we are depends on totalRecords. So we have waited until above api return response. In sync code our all 400 api calls were executing synchronously. While here out first api call is running synchronously and all 399 calls will run asynchronously. So we will get faster result.
  apiResponse = [];

  // Now we have all urls now call all these urls parallelly. So api will not wait for previous call to finish its execution
  async.each(urls, function (url, key, callback) {
    let tempPromises = [];
    // This function will be call asynchronously.
    let promise = return new Promise(function (resolve, reject) {
     sendRequest(url, function(err, data)) {
      apiResponse.push(data);
      resolve(data);
     }
  });

  tempPromises.push(promise);

  Promise.all(tempPromises).then(function(values) {
    callback();
  });

  }, function (err) {
    // once all requests are finish we will get apiResponse here. It will take only few seconds.
  });
}

Conclusion:

It’s not always needs to use async code. You should use it when you needed. Even in async program there is some code that is depends on previous code to finish its execution. So first you have to make your plan which code should be sync and which code should be async. Once you find out where you can replace you sync code to async code you can replace it with modern async code.