BreadcrumbHomeResourcesBlog Why You Should Use Asynchronous PHP February 28, 2020 Why You Should Use Asynchronous PHPPHP DevelopmentBy Maurice KherlakianDevelopers rely on asynchronous PHP calls to fulfill requests without bogging down the application — but that hasn't always been the case. In this blog, we look at asynchronous PHP, how it works, how it's used, and how it compares to synchronous PHP. Let's get started with the basicsTable of ContentsWhat is Asynchronous PHP?What Is Synchronous PHP?What Are Synchronous and Asynchronous PHP Calls?Does Asynchronous Processing Boost PHP Performance?Which Model Should You Use? What Are the Benefits of Asynchronous PHP?Projects to Get Started with Asynchronous PHPTraining: PHP Classes for Beginners to Advanced LevelsTable of Contents1 - What is Asynchronous PHP?2 - What Is Synchronous PHP?3 - What Are Synchronous and Asynchronous PHP Calls?4 - Does Asynchronous Processing Boost PHP Performance?5 - Which Model Should You Use? 6 - What Are the Benefits of Asynchronous PHP?7 - Projects to Get Started with Asynchronous PHP8 - Training: PHP Classes for Beginners to Advanced LevelsBack to topWhat is Asynchronous PHP?Asynchronous PHP refers to PHP code that is written using the asynchronous model. In other words, asynchronous applications can multi-task. This is critical because traditionally, there’s a lot of time when a CPU sits idle while a PHP application manages I/O tasks. Not only does this slow overall application performance, but also, it lowers hardware utilization.The idea behind asynchronous PHP is to take full advantage of all available CPU cycles to boost PHP performance. It does this by keeping non-blocking I/O tasks running in the background, and letting the CPU process other tasks as it’s waiting for data and instructions needed to complete the I/O tasks. By making it possible for your applications to manage many requests at once, they can take full advantage of available hardware resources, which can boost ROI and service levels at the same time.How Do You Enable Asynchronous PHP?By using extensions such as Swoole, or a framework for PHP such as ReactPHP, you can make it possible for your PHP applications to accept requests and responses asynchronously, using event loops. Back to topWhat Is Synchronous PHP?Synchronous PHP code is sequential. Individual tasks must be completed before you can start another one. So with synchronous PHP, the CPU can process only one I/O task at a time. When it completes one task, it moves on to the next one in the queue.PHP was originally created to support synchronous development, so most PHP developers are used to writing only synchronous code with the language. Prefork with either mod_php with Apache or php-fpm with either Apache or Nginx.Back to topWhat Are Synchronous and Asynchronous PHP Calls?There are two types of API calls; synchronous calls, which must be completed before code execution resumes on the API; and asynchronous calls, which do not pause code execution on the API — instead issuing a "callback" when the call is completed.Back to topDoes Asynchronous Processing Boost PHP Performance?Yes. Because asynchronous processing enables the management and completion of more than one task at a time, you can dramatically boost PHP performance by using it instead of synchronous PHP. In some benchmarks, you can increase PHP performance (throughput) by 100 times, by enabling asynchronous PHP via Swoole, instead of using a process accelerator such as php-fpm with synchronous code. That’s because php-fpm does not support asynchronous, real-time communications using protocols such as Websockets.On-Demand Webinar: Scalability, Asynchronous Processing, and Optimization for PHPThis webinar shows how asynchronous processing can help increase PHP performance, among other performance improvement tips.Back to topWhich Model Should You Use? As with most questions around runtime, web servers, and programming in general, the decision to use synchronous or asynchronous PHP will vary between use cases. To use asynchronous PHP, your code needs to support it. Today, most of the ready-to-go PHP extensions can only support synchronous models because they use methods such as blocking I/O calls. It is possible to rewrite your code to support asynchronous processes by using a framework such as Swoole to manage background processing. For example, calls that require I/O would need to be sent to Swoole to manage in the background so that your application can continue on to the next instruction. When the data comes back from the I/O call, Swoole would then need to return control of the I/O call to your application, so it can hand the requested data to the process.Back to topWhat Are the Benefits of Asynchronous PHP?Refactoring your applications to support asynchronous processes may require some effort. However, by doing do, you can:Improve PHP performance and user experienceMaximize hardware utilizationPotentially, reduce your data center footprintBack to topProjects to Get Started with Asynchronous PHPSince your code has to be written in a way to support asynchronous processes, you’ll need to utilize a project that can do that. Here are our top-three recommendations:Swoole – Coroutine PHP ansychronous programming frameworkReactphp – Event-driven, non-blocking I/O with PHPAmphp – Non-blocking concurrency framework for PHPBack to topTraining: PHP Classes for Beginners to Advanced LevelsAre you a beginner, intermediate, or advanced PHP developer looking to increase your PHP skills? No need to feel overwhelmed. Take a class from Zend by Perforce. Our team of PHP experts teach a variety of online and onsite PHP courses.SEE OUR PHP CLASSESAdditional Resources101 Guide: Developing Web Applications with PHPBlog: What Is PHP? Service: Zend PHP Performance Audit Blog: What Is a PHP Function?Blog: What Is PHP Foreign Function Interface?Blog: Continuous Delivery Benefits and Barriers This blog was originally published on December 18, 2019. It has been updated for accuracy and clarity.Back to top