evented i/o based web servers, explained using bunnies

10
Evented I/O based web servers, explained using bunnies Simon Willison Part of a talk given at Full Frontal 2009

Upload: simon-willison

Post on 07-Nov-2014

36.134 views

Category:

Technology


1 download

DESCRIPTION

Part of a talk about Node.js given at Full Frontal 2009 in Brighton. I've added some additional explanatory notes.

TRANSCRIPT

Page 1: Evented I/O based web servers, explained using bunnies

Evented I/O based web servers, explained using bunnies

Simon WillisonPart of a talk given at Full Frontal 2009

Page 2: Evented I/O based web servers, explained using bunnies

Your Web Server(using a bunny)

Page 3: Evented I/O based web servers, explained using bunnies

Your Web Server(using a bunny)

Happy hamster

Single threaded (one bunny), so can only handle one request at a time

Page 4: Evented I/O based web servers, explained using bunnies

Your Web Server(using a bunny)

Impatient hamsters

(The hamsters are using web browsers to visit your site)

Page 5: Evented I/O based web servers, explained using bunnies

Your Web Server(using threads,aka bunnies)

Happy hamsters

5 bunnies = can handle 5 requests at the same time

Page 6: Evented I/O based web servers, explained using bunnies

Your Web Server(using threads,aka bunnies)

fetching a web API(2 seconds)

Long running operations cause a thread to block, causing requests to

build up in a queue

Page 7: Evented I/O based web servers, explained using bunnies

Impatient hamsters

fetching a web API(2 seconds)

uploading an image(3 seconds)

fetching a web API(2 seconds)

comet long polling(10 seconds)

fetching a web API(2 seconds)

Page 8: Evented I/O based web servers, explained using bunnies

Your Web Server(event loop, aka

hyperactive squid)

Replace the bunnies with a single hyperactive squid. The squid runs up

and down as fast as it can dealing with each hamster in turn. It fires off any

long running I/O operations and then moves on to the next hamster. When the I/O operation reports progress, it does a little more work on behalf of

the corresponding hamster.

Page 9: Evented I/O based web servers, explained using bunnies

Bad code

• rows = database.fetch(category = 'news')

• template = read_file('homepage.html')

• json = fetch_url('http://.../')

These functions block and wait for results - blocking the squid and causing

the entire event loop (and hence server) to pause until they complete

Page 10: Evented I/O based web servers, explained using bunnies

Good code

• database.fetch(category = 'news', callback)

• read_file('homepage.html', callback)

• fetch_url('http://.../', callback)

These functions specify a callback to be executed as soon as the I/O operation

completes