3. ASP.NET Web Forms
public class FirstPage: System.Web.UI.Page
{
public System.Web.UI.WebControls.Label lblMessage;
private int i = 0;
protected void Page_Load(object sender, EventArgs e)
{
i = i + 1;
lblMessage.Text = i.ToString();
}
}
6. How Web Server Works?
• Create socket
• Bind socket
• Listen on socket
• Accept new connection from a client
• Receive data from the client
• Process the request somehow
• Send response to the client
• Wait for a new connection
7. How Web Server Works?
• Create socket
• Bind socket
• Listen on socket
• Accept new connection from a client
Separate thread
• Receive data from the client
• Process the request somehow
• Send response to the client
• Wait for a new connection
9. How Web Server Works?
• Create socket
• Bind socket
• Listen on socket
• Accept new connection from a client
Separate thread
• Receive data from the client
• Process the request somehow
• Send response to the client
• Wait for a new connection
10.
11. Threads cost
• Memory overhead
– Default thread stack size in Linux – 8M
• Context switches cost
– It takes ~2-3µs to switch context in Linux
– http://blog.tsunanet.net/2010/11/how-long-does-
it-take-to-make-context.html
18. Ability to handle more than 10,000
simultaneous connections with a
low memory footprint (~2.5 MB
per 10k inactive HTTP keep-alive
connections)
Wikipedia
19. How is that possible?
• Non-blocking networking using fastest
interface on any given platform
– kqueue (FreeBSD 4.1+)
– epoll (Linux 2.6+)
– rt signals (Linux 2.2.19+)
– /dev/poll (Solaris 7 11/99+)
– event ports (Solaris 10)
– select
– poll
20. • Python web framework and async
networking library
• Originally developed for FriendFeed
• Ideal for long polling, WebSockets and long-
lived connections
www.tornadoweb.com
27. Web Site Node
Show list of currently
Runs many real-time
running apps with their
applications
titles
App
Changes titles
whenever it wants
28. Web Site Node
Show list of currently
Runs many real-time
running apps with their
applications
titles
How would you make App
sure user can see updated
Changes titles
titles on the web? whenever it wants
29. Web Site Node
300-400ms
Show list of currently
Runs many real-time
running apps with their
applications
titles
What if… App
App
App
App
App
App
Changes titles
Changes titles
whenever it wants
Changes titles
whenever it wants
Changes titles
whenever it wants
Changes titles
whenever it wants
Changes titles
whenever it wants
whenever it wants
30. Non-blocking Requests
class AppUpdatesHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def get(self):
http = tornado.httpclient.AsyncHTTPClient()
request = tornado.httpclient.HTTPRequest(
"http://www.frontend.com", method="POST", ...)
http.fetch(request, callback=self.on_response)
self.finish()
def on_response(self, response):
if not response.error:
log.debug("Title updated”)
33. How does it work?
• Event loop
– Tornado.ioloop.IOLoop
– Level-triggered, uses epoll on Linux and kqueue
on BSD
• Futures
– Result of an async operation
34. Coroutines
class AppUpdatesHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
@tornado.gen.coroutine
def get(self):
http = tornado.httpclient.AsyncHTTPClient()
request = tornado.httpclient.HTTPRequest(
"http://www.frontend.com",
method="POST", ...)
self.finish()
response = yield http.fetch(request)
if not response.error:
log.debug("Title updated”)
35. Conclusions
• Web has changed, so programmers should
too
• You want to face C10k problem
• There means to solve it
• Learn to think asynchronously
• Check out www.tornadoweb.org