← Lessons

quiz vs the machine

Gold1380

Concurrency

Edge vs Level Triggered Notifications

Two ways a readiness interface tells you about data, and the drain rule that keeps edge mode correct.

5 min read · core · beat Gold to climb

Two notification styles

A readiness interface like epoll can report events in two modes. The choice changes how your loop must be written, and getting it wrong causes connections to stall mysteriously.

Level triggered

Level triggered mode reports a descriptor as ready for as long as the condition holds. If a socket has unread bytes, every epoll wait keeps reporting it ready until you read all the data. This is forgiving. You can read a little, return to the loop, and you will simply be told again that more remains.

Edge triggered

Edge triggered mode reports only the transition from not ready to ready. You get one notification when new data arrives, and nothing more until even newer data shows up. If you read only part of the buffer and stop, the leftover bytes will not trigger another event and the connection appears frozen.

The drain rule

  • In edge triggered mode you must read in a loop until you get EWOULDBLOCK
  • That guarantees you have consumed everything the single edge announced
  • Edge mode means fewer wakeups and less syscall overhead at high load
  • Level mode is easier to write but can wake you repeatedly for the same data

Key idea

Level triggered keeps reporting while data remains, while edge triggered fires once per transition and forces you to drain the socket until EWOULDBLOCK.

Check yourself

Answer to earn rating on the learn ladder.

1. In edge triggered mode, how do you avoid missing leftover data?

2. How does level triggered mode report a socket with unread data?