← Lessons

quiz vs the machine

Silver1050

Concurrency

Blocking vs Non Blocking IO

How a socket call behaves when data is not ready, and why that single choice shapes a server.

4 min read · intro · beat Silver to climb

What blocking means

When a thread calls read on a blocking socket and no data has arrived, the kernel parks that thread until bytes show up. The thread sits idle, holding its stack and its place in the scheduler. One thread can therefore serve only one slow connection at a time, which is why the simplest servers spin up one thread per client.

What non blocking means

A socket put into non blocking mode behaves differently. If the read would have to wait, it returns immediately with the error EWOULDBLOCK instead of sleeping. The thread is free to go do other useful work and come back later. Nothing is lost, but the program now owns the job of deciding when to retry.

Why this matters

  • Blocking is simple to reason about but ties up a thread per pending operation
  • Non blocking frees the thread but forces you to poll or to ask the kernel which sockets are ready
  • Non blocking is the foundation that event loops and IO multiplexing are built on top of

Most high concurrency servers choose non blocking sockets and then add a readiness notification layer so a handful of threads can watch thousands of connections.

Key idea

Blocking IO parks the thread when data is not ready, while non blocking IO returns at once and leaves the waiting decision to your code.

Check yourself

Answer to earn rating on the learn ladder.

1. What does a non blocking read return when no data is available yet?

2. Why does blocking IO limit concurrency in a simple server?