liveBook · Manning

Event-driven architecture

As explained in Latency

Event-driven concurrency coordinates work by responding to events that signal changes or operation completions rather than blocking execution while waiting.

Event-driven architecture manages concurrent operations by responding to events instead of dedicating threads to tasks or blocking while waiting. An event loop monitors network connections, file descriptors, timers, and other sources, then dispatches detected events to handlers. A single thread can therefore coordinate many I/O-bound operations with relatively low resource overhead. section 9.2.4

Web server with asynchronous database work

A web server can accept thousands of connections without assigning one thread to every request. When a request needs a database query, it starts the query asynchronously and handles other events. Query completion later triggers a continuation that resumes request processing. section 9.2.4

A practical event loop follows this division of responsibility:

  • Wait for events from operating-system interfaces such as epoll or io_uring on Linux and kqueue on macOS; waiting may block when no work exists. section 9.2.4
  • Dispatch each detected event to its handler, which must not block because it would stop the loop from processing other events. section 9.2.4
  • Defer unfinished work and resume it when a later completion event signals that the required operation has finished. section 9.2.4
Figure 9.3 The event loop is the heart of event-based concurrency—it polls for events from the OS and invokes appropriate event handlers. For example, if a packet arrives from the network, the event loop polls for them using an I/O polling API such as io_uring, epoll, or kqueue, and then hands over the received messages to event handlers. When the event-loop polling is done, the event loop calls itself to continue polling.
Figure 9.3: Event loop is the heart of event-based concurrency. The event loop polls for events from the OS and invokes appropriate event handlers. For example, if a packet arrives from the network, the event loop polls for them using an I/O polling API such as io_uring, epoll, or kqueue, and then hands over the received messages to event handlers. When the event loop polling is done, the event loop calls itself to continue polling.

The event-loop diagram shows the polling-and-dispatch cycle, including how operating-system I/O notifications reach event handlers before polling resumes. section 9.2.4

The model is especially effective for I/O-bound applications, where thread-based designs would spend substantial time waiting. It reduces resource overhead, but handler design is critical: one blocking handler can prevent every other event from being processed. section 9.2.4 section 9.7

How Latency covers it

How deep it goes
Explains polling, dispatch, asynchronous continuation, and blocking constraints, but stops short of a complete implementation.
Examples
Uses a high-connection web server, asynchronous database queries, network buffers, timers, and operating-system I/O polling.
What you can do after
Apply it via an event loop that polls operating-system events and dispatches non-blocking handlers.
How it connects
Connects event loops and non-blocking operations with threading, coroutines, parallelism, and concurrency control.

Also explained in

Compare how 10 books explain event-driven architecture →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Latency. The section links point to the parts of the book it draws on.