32

I want to understand basics of Event Driven web server, I know one of them is Tornado, but any other information is much appreciated.

Thanks

Anonymous
  • 1,287
  • 6
  • 20
  • 21

3 Answers3

32

There's a nice analogy of this described here:

http://daverecycles.tumblr.com/post/3104767110/explain-event-driven-web-servers-to-your-grandma

Relevant text from the above link:

Explain “Event-Driven” Web Servers to Your Grandma You’ve heard the term event-driven, event-based, or evented when it comes to web servers. Node.js is based on evented I/O. nginx is an asynchronous event-driven web server.

But what does the term mean? Here’s an easy way to think about it.

Let’s think of a web server as a pizza shop that needs to take orders over the phone (requests for web pages).

Traditional Web Server

The pizza shop hires operators (processes/threads) to take orders over the phone. Each operator has one phone line. After the operator is done taking the order, the operator keeps the customer on the line until the pizza (response web page) is done baking and then tells them it’s ready to pick up.

So the pizza shop needs to hire as many operators as the number of pizzas that may be baked at once in order to serve all customers who call in.

Event-Driven Web Server

The pizza shop only hires one operator, but has trained the operator to hang up after taking the order, and call the customer back when the pizza is ready to be picked up.

Now one operator can serve many customers.

Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411
Frank
  • 421
  • 4
  • 2
  • 3
    +1 Perfect example! [This](http://www.slideshare.net/simon/evented-io-based-web-servers-explained-using-bunnies) is great too. – Mick May 16 '13 at 14:29
  • 1
    The link is dead or not opening via protected proxy. If possible, please add the relevant text here. – YetAnotherBot Jan 07 '19 at 06:41
19

A web server needs to handle concurrent connections. There are many ways to do this, some of them are:

  • A process per connection.
  • A process per connection, and have a pool of processes ready to use.
  • A thread per connection.
  • A thread per connection, and have a pool of threads ready to use.
  • A single process, handle every event (accepted connection, data available to read, can write to client, ...) on a callback.
  • Some combination of the above.
  • ...

At the end, the distinction ends up being in how you store each connection state (explicitly in a context structure, implicitly in the stack, implicitly in a continuation, ...) and how you schedule between connections (let the OS scheduler do it, let the OS polling primitives do it, ...).

ninjalj
  • 42,493
  • 9
  • 106
  • 148
1

Event-driven manner aims at resolving the C10K Problem. It turns the traditional 'push model' into a 'pull model' to create a non-blocking evented I/O. Simply put, the event-driven architecture avoid spawning additional threads and thread context switching overheads, and usually ends up with better performance and less resource consumption.

Some overview from a rails developer, also includes analogy: http://odysseyonrails.com/articles/8

Tristan.Liu
  • 176
  • 1
  • 7
  • Note that link-only answers are discouraged, SO answers should be the end-point of a search for a solution (vs. yet another stopover of references, which tend to get stale over time). Please consider adding a stand-alone synopsis here, keeping the link as a reference. – kleopatra Jul 31 '13 at 07:54