Skip to content

C/C++

Web Server

We can add an application layer or a protocol layer on top of Reactor to build a web server. The Reactor we built in A Million Concurrency handles I/O events at the network layer. While it currently only echos what the client sends, we will extend it by adding support for HTTP and WebSocket protocols.

Epoll Trigger Mode

In Linux, epoll is a high-performance I/O event notification system that allows the application to monitor multiple file descriptors to determine if I/O operations can be performed on any of them.

There are two trigger modes: Level-Triggered (default) and Edge-Triggered.

A Million Concurrency

Building on top of epoll and the Reactor pattern, we will further enhance our TCP server to achieve the capability of handling a million concurrent requests in a non-blocking manner with a single thread.

Reactor

The Reactor pattern demultiplexes and dispatches I/O events to corresponding handlers. It enables non-blocking I/O by ensuring that a single thread, or event loop, can handle many connections or requests without waiting for individual operations to complete.

I/O Multiplexing

Using multiple threads to handle concurrent requests is a straightforward approach, as demonstrated in Network I/O. However, this method has limitations. A process can only create a finite number of threads, and performance tends to degrade as more resources are allocated to create and manage threads. This includes maintaining their states and coordinating their execution, which is not efficient. This approach can probably handle 1000 concurrent requests, but not much more. Let's discuss alternative methods, such as select, poll, and epoll.

Network I/O

Whether it's loading a webpage, sending a message that goes unanswered, or streaming a cat video, the network is at work behind the scenes. In essence, a network enables data transfer from one place to another, no matter the distance. At a technical level, this is achieved through two sockets communicating with each other. In this post, we'll explore how it works and implement a basic client-server communication using TCP sockets.