What's the difference between: Asynchronous, Non-Blocking, Event-Base architectures?

nickb picture nickb · Oct 28, 2011 · Viewed 30k times · Source
  1. What's the difference between:

    • Asynchronous,
    • Non-Blocking, and
    • Event-base architectures?
  2. Can something be both asynchronous and non-blocking (and event-based)?

  3. What's most important in programming, to have something: asynchronous, non-blocking and/or event-base (or all 3)?

If you could provide examples, that would be great.

This question is being asked because I was reading this great StackOverflow article on a similar topic but it doesn't answer my questions above.

Answer

Rohit Karlupia picture Rohit Karlupia · Feb 28, 2012

Asynchronous Asynchronous literally means not synchronous. Email is asynchronous. You send a mail, you don't expect to get a response NOW. But it is not non-blocking. Essentially what it means is an architecture where "components" send messages to each other without expecting a response immediately. HTTP requests are synchronous. Send a request and get a response.

Non-Blocking This term is mostly used with IO. What this means is that when you make a system call, it will return immediately with whatever result it has without putting your thread to sleep (with high probability). For example non-blocking read/write calls return with whatever they can do and expect caller to execute the call again. try_lock for example is non-blocking call. It will lock only if lock can be acquired. Usual semantics for systems calls is blocking. read will wait until it has some data and put calling thread to sleep.

Event-base This term comes from libevent. non-blocking read/write calls in themselves are useless because they don't tell you "when" should you call them back (retry). select/epoll/IOCompletionPort etc are different mechanisms for finding out from OS "when" these calls are expected to return "interesting" data. libevent and other such libraries provide wrappers over these event monitoring facilities provided by various OSes and give a consistent API to work with which runs across operating systems. Non-blocking IO goes hand in hand with Event-base.

I think these terms overlap. For example HTTP protocol is synchronous but HTTP implementation using non-blocking IO can be asynchronous. Again a non-blocking API call like read/write/try_lock is synchronous (it immediately gives a response) but "data handling" is asynchronous.