C10K¶
The C10K problem is the challenge of handling 10,000 concurrent network connections on a single server. The term was coined by Dan Kegel in 1999 and became a landmark milestone in server design.
Traditional thread-per-connection or process-per-connection models collapse under this load: spawning 10,000 threads consumes gigabytes of stack memory and floods the OS scheduler. The solution is event-driven I/O, using epoll, kqueue, or WSAPoll to watch thousands of file descriptors simultaneously in a single thread, only waking when a descriptor is actually ready.
C10K-scale concurrency historically required external tools: nginx as a front-end proxy, FrankenPHP workers, or userland event loops from ReactPHP, AMPHP, or Revolt. PHP 8.6 addresses this with the native Io\Poll API, which exposes epoll/kqueue directly without leaving PHP.
The successor challenge, C10M or 10 million connections, requires kernel bypass techniques, that are out of scope for userland PHP.
<?php
// Without C10K-aware design: one process per connection
// Fails at high concurrency — each fork() is expensive
$server = stream_socket_server('tcp://0.0.0.0:8080');
while ($client = stream_socket_accept($server)) {
if (pcntl_fork() === 0) { // child per connection: does not scale to 10K
handle($client);
exit;
}
}
// C10K-aware: single process, event-driven, PHP 8.6 Io\Poll
use Io\Poll\Context;
use Io\Poll\Event;
$context = new Context();
$server = stream_socket_server('tcp://0.0.0.0:8080');
stream_set_blocking($server, false);
$context->add($server, Event::Read, function ($handle) use ($context) {
$client = stream_socket_accept($handle->stream, 0);
if ($client === false) { return; }
stream_set_blocking($client, false);
$context->add($client, Event::Read | Event::OneShot, function ($ch) {
echo fread($ch->stream, 8192);
fclose($ch->stream);
});
});
$context->run(); // handles thousands of connections in one thread
?>
See also The C10K Problem — Dan Kegel and PHP RFC: Polling API.
Related : Polling, Event Loop, Non-blocking, Async, Concurrency, Edge Triggered, One-shot, Socket, Numeronym