Linux Internals
epoll Explained: How Linux Servers Handle Thousands of Connections
By NileVPS Team · Published · 5 min read
اقرأ المقال بالعربيA web server on a small VPS can keep tens of thousands of connections open while running only a handful of worker processes. Nginx, Node.js, Redis and many Go and Rust services share the same trick: instead of dedicating a thread to every connection, they ask the Linux kernel which connections are ready to read or write. The interface that makes this efficient is called epoll. This article explains what it does, why it replaced older approaches, and what to check on your own server.
The problem epoll solves
A network connection is just a file descriptor (fd). Reading from a socket with no data blocks the caller until data arrives. The simplest design gives each connection its own thread so that blocking does not stall everybody else. That works for a few hundred clients, but every thread needs stack memory and the scheduler must switch between all of them. With thousands of mostly idle keep-alive or WebSocket connections, this wastes memory and CPU, a scaling wall known as the C10K problem.
The alternative is non-blocking I/O with readiness notification: put all sockets in non-blocking mode, then ask the kernel which of them have something to do right now, and serve only those.
select and poll: the older answers
Unix has offered select() and poll() for decades. Both take a list of file descriptors and return when at least one is ready. Their weakness is that the full list is passed to the kernel on every call, and the kernel scans every entry each time. With 10,000 connections where only 20 are active, the program still pays for checking all 10,000 on every loop iteration. select() also has a fixed limit, usually 1024 descriptors, set by FD_SETSIZE.
How epoll works
epoll splits the work into a setup step and a waiting step, so the kernel keeps the interest list between calls:
epoll_create1()creates an epoll instance, which is itself a file descriptor.epoll_ctl()adds, modifies or removes a socket from the interest list, together with the events you care about (for exampleEPOLLINfor readable,EPOLLOUTfor writable).epoll_wait()sleeps until one or more registered sockets become ready, then returns only the ready ones.
Internally, when a packet arrives and a socket becomes readable, the kernel places that socket on a ready list attached to the epoll instance. epoll_wait() simply hands that list back. The cost of each wait depends on the number of ready events, not on the total number of watched connections. That is why one event-loop thread can comfortably supervise a very large number of sockets.
Level-triggered vs edge-triggered
epoll supports two notification modes:
| Mode | Behaviour | Typical use |
|---|---|---|
| Level-triggered (default) | epoll_wait() keeps reporting the fd while data remains unread | Simpler code, behaves like poll() |
Edge-triggered (EPOLLET) | Reports only when the state changes, for example new data arrives | High-performance loops; you must read until EAGAIN |
Edge-triggered mode reduces repeated wake-ups, but if your code reads only part of the buffer and waits again, it may never be notified about the remaining data. Libraries such as libuv, Tokio and the Go runtime handle these details for you.
Avoiding the thundering herd
When several processes wait on the same listening socket, a new connection could wake all of them while only one can accept it. Linux provides EPOLLEXCLUSIVE and the SO_REUSEPORT socket option to spread new connections across workers. Nginx exposes the latter through listen 443 ssl reuseport;.
Where you meet epoll in practice
You rarely call epoll directly unless you write C, but you configure software built on it:
- Nginx picks epoll automatically on Linux. The line
use epoll;in theeventsblock is optional, whileworker_connectionssets how many connections each worker may hold. - Node.js uses libuv, which drives an epoll loop on Linux. CPU-heavy code in your handlers blocks that loop and every other client waits.
- Redis handles clients in a single event loop based on epoll, which is why one slow command affects everyone.
- Python asyncio, Go's network poller and Rust's Tokio all use epoll under the hood on Linux.
Checking and tuning a server
Every connection consumes a file descriptor, so descriptor limits are the first thing to check on a busy VPS:
ulimit -n # per-process limit in the current shell
cat /proc/sys/fs/file-max # system-wide limit
cat /proc/$(pgrep -o nginx)/limits | grep "open files"
ss -s # summary of open socketsFor a systemd service, raise the limit in an override instead of editing the unit file:
sudo systemctl edit nginx
# add:
# [Service]
# LimitNOFILE=65535
sudo systemctl daemon-reload && sudo systemctl restart nginxIn Nginx, worker_rlimit_nofile should be at least as large as worker_connections, remembering that a proxied request uses two descriptors: one to the client and one to the upstream. Also review net.core.somaxconn and the backlog parameter if you see connection drops during traffic bursts.
Is epoll still relevant with io_uring?
Yes. io_uring is a newer asynchronous interface that also covers disk I/O and can batch system calls, and some projects are adopting it. Still, epoll remains the stable, widely supported foundation of nearly every Linux network server, and understanding it explains how your stack behaves under load. For the broader picture of why waiting on I/O matters, see our guide on server I/O. If you want a server to experiment with these settings, you can deploy a Linux VPS in a few minutes.
FAQ
Do I need to enable epoll manually in Nginx?
No. Nginx detects the best available method at build and start time and uses epoll on Linux by default. Adding use epoll; is harmless but usually unnecessary. What matters more is setting worker_connections and the open-file limit to match your expected traffic.
Does epoll make my application faster for every workload?
epoll makes waiting on many connections cheap. It does not speed up CPU-heavy work such as image processing or complex queries. If a request spends most of its time computing, an event loop can even hurt latency unless that work is moved to worker threads or separate processes.
Is epoll available on Windows or macOS?
epoll is specific to Linux. macOS and the BSDs offer kqueue, and Windows uses I/O completion ports (IOCP). Cross-platform libraries such as libuv and Tokio pick the right mechanism for each operating system, so the same code runs everywhere.