This Is What I Learned As a Sync PHP Developer About Async PHP

I worked with Laravel for years. I used sync PHP. A request comes, a process runs, and a response goes out. I never needed async.

Then I read about the new PHP 8.6 Polling API. It changed my view on how PHP handles tasks.

Here is the breakdown of how async works.

The Problem with Blocking I/O

When you call an API, your code waits. Example: $response = Http::get('https://api.example.com');

If that API takes 300ms, your PHP process does nothing for 300ms. It sits in a sleep state. It holds memory and occupies a worker slot. If all your workers are sleeping, your server stops accepting new requests.

The Async Solution

Async lets you do other work during those 300ms. Instead of waiting, you run other tasks.

But how do you know when the data arrives? This is where the kernel comes in.

The Evolution of Polling

  1. select() PHP has had stream_select() since PHP 4. It asks the kernel: "Is any data ready on these sockets?" The problem is the rescan tax. If you have 10,000 connections, you must send the whole list to the kernel every time. This is slow and hits limits.

  2. epoll / kqueue This is a kernel feature, not a language feature. Linux uses epoll. macOS uses kqueue. Instead of scanning a full list, the kernel maintains a ready-list. It only tells you which specific sockets have data. This scales to thousands of connections without extra cost.

  3. Fibers (PHP 8.1) Fibers allow you to pause a function anywhere in the call stack. A Fiber does not wake up by itself. It is like a paused YouTube video. Someone must call $fiber->resume() to play it again.

The Missing Link: PHP 8.6

Async I/O requires three parts: • Pause: Fibers (Now in PHP core) • Decide: The Event Loop (Plain PHP code) • Know: Kernel Polling (The gap)

Until now, PHP lacked a native way to "know" which socket is ready without using old tools or C extensions.

PHP 8.6 closes this gap. It brings a native Polling API to the core. It will automatically use epoll on Linux and kqueue on Mac.

The Big Picture

Async is not magic. An event loop is just PHP code that decides when to call resume() on a Fiber.

Fibers provide the ability to pause. epoll provides the intelligence to know when to unpause.

If you only use sync PHP, you do not need to change your Laravel apps today. But understanding this model makes async libraries like ReactPHP or Amp much easier to master.

Build, do not just consume. Run the code yourself to see how it works.

Source: https://dev.to/alamriku/sync-php-developer-hisebe-async-php-bujhte-giye-yaa-shikhlaam-fibers-epoll-aar-php-86-462j