The Truth About Async PHP: Fibers, epoll, and PHP 8.6

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

Then I read about the new PHP 8.6 Polling API. It changed how I see everything.

Here is what I learned about how async works under the hood.

The IO Problem

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

If that call takes 300ms, your CPU does nothing for 300ms. It stays in a sleep state. This is blocking I/O.

If you have three API calls:

  • API A: 300ms
  • API B: 400ms
  • API C: 200ms

Sequential total: 900ms. Async total: 400ms (the time of the slowest call).

Async allows your process to do other work while waiting for data.

Select vs. epoll

To do async, you need to know which socket has data ready.

  1. select() PHP has used stream_select() since version 4. It works by asking the kernel to watch a list of sockets. The problem: Every time data arrives, you must scan the entire list again. This is a rescan tax. It also has a limit of about 1024 connections.

  2. epoll (Linux) / kqueue (macOS) These are kernel features. Instead of scanning a list, the kernel keeps a ready-list. It only tells you which specific sockets are ready. This scales to thousands of connections without extra work.

epoll is not a PHP feature. It is a Linux feature. Go, Rust, and Node.js all use it.

Fibers: The Pause Button

PHP 8.1 introduced Fibers. I thought Fibers would wake up on their own. They do not.

A Fiber is like a paused video. It stays paused until someone calls $fiber->resume().

An Event Loop is just a piece of PHP code that decides when to call resume().

Async I/O requires three parts:

  • Pause: Fibers (PHP 8.1 core)
  • Decide: The Event Loop (Plain PHP code)
  • Know: Kernel Polling (epoll/kqueue)

Before PHP 8.6, PHP had the "Pause" and "Decide" parts, but the "Know" part relied on the old select() or slow C extensions.

PHP 8.6 closes this gap. It brings a native Polling API into the core. Now, PHP can use epoll or kqueue directly without extra extensions.

The Takeaway

If you use Laravel with PHP-FPM, you do not need to change anything today.

But understand this: Async is not magic. It is just a smart way to manage waiting time.

Stop just consuming code. Run a simple script. Break it. That is how you truly learn.

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