32
Server Side Story An asynchronous ballet Simone Deponti - [email protected] EuroPython 2013

Server side story

  • View
    845

  • Download
    2

Embed Size (px)

DESCRIPTION

Modern web development is split into two diverging areas: frontend and backend. Gone are the days in which the server side code could manage the UI, and the growt of rich web interfaces and mobile applications poses new challenge in the way we approach web development. In this talk, we will show how the paradigms of server side development have been radically changed by this trend, and how emerging client side technologies such as Websockets are radically altering the landscape of web development, and how we can address such technologies using Python. We will also highlight the challenges that such new paradigms arise in the area of security, performance, and system architecture.

Citation preview

Page 1: Server side story

Server Side Story

An asynchronous ballet

Simone Deponti - [email protected] 2013

Page 2: Server side story

Who am I

Python web developer since 2006Work at Abstract as web developerPlone contributorDo more Javascript than I'm willing to admit

Abstract @ EuroPython 2013

Page 3: Server side story

Am I in the wrong room?

This talk will focus on:• Where web development comes from• Challenges of real-time applications• Patterns and tools to deal with that

Abstract @ EuroPython 2013

CODE SAMPLE FREE! (ALMOST)

Page 4: Server side story

A LITTLE BIT OF HISTORY

Page 5: Server side story

Let there be <a> link

The web was born as:• Simple document retrieval• Interlinking of documents

Abstract @ EuroPython 2013

Page 6: Server side story

Have you seen?

Next step:• Searching documents• Query parameters• POST and Forms• CGI• We start having stuff that's not

documents

Abstract @ EuroPython 2013

Page 7: Server side story

Statefulness

HTTP is stateless• stateless isn't good for applications• cookies add statefulness

Abstract @ EuroPython 2013

Page 8: Server side story

Javascript

Executing code on the client was needed:• provisions for scripting were there• we lacked a language and a way to

interact with the document• Javascript and the DOM came

Abstract @ EuroPython 2013

Page 9: Server side story

AJAX

Javascript was limited to mangling the DOM• AJAX added the ability to make

requests "on the side"• One-page applications• State lives on the client, too!

Abstract @ EuroPython 2013

Page 10: Server side story

CURRENT SITUATION

Page 11: Server side story

Web applications

So far:• the server is the master• all data lives there• the client requests stuff and gets a

reply

Abstract @ EuroPython 2013

Page 12: Server side story

Web applications (server side)

Every "classic" web app does this on every request:• pull up the state• process the request• assemble a response• save the new state and return it back

Abstract @ EuroPython 2013

Page 13: Server side story

I've got something to say!

The server can speak only when asked.This maps poorly on these cases:• Notifications• Chat systems• Real time applications

Abstract @ EuroPython 2013

Page 14: Server side story

SOLUTIONS

Page 15: Server side story

Polling

Polling is the simplest solution, and the worst• You keep annoying the server• Wasting memory, CPU, bandwidth on

both sides just to annoy a component of your stack

• Logarithmic polling isn't a solution

Abstract @ EuroPython 2013

Page 16: Server side story

COMET

Also called long polling• Still based on request-reply• The server replies very slowly,

allowing time to elapse and hoping that something happens

Abstract @ EuroPython 2013

Page 17: Server side story

BOSH

The formal way to do COMET, as done by XMPP:• Client starts a long-polling connection• If something happens on the client

side, client sends a second request, server replies on the first and closes, second remains open

• Before expiration time, server sends empty message and closes, client reopens connection.

Abstract @ EuroPython 2013

http://xmpp.org/extensions/xep-0124.html#technique

Page 18: Server side story

You might not be able to deal with this

If your execution model calls for one thread/process per request, you're out of luck.You must use an asynchronous server.

Abstract @ EuroPython 2013

Page 19: Server side story

Websockets

A new protocol• a bidirectional connection tunneled

through HTTP (similar to a raw TCP connection)

• HTTP 1.1 has provisions for this• Sadly, it's the part of the protocol

where many implementors got bored

Abstract @ EuroPython 2013

http://tools.ietf.org/html/rfc6455

Page 20: Server side story

Websockets in Python

from geventwebsocket.handler import WebSocketHandlerfrom gevent.pywsgi import WSGIServer[...]

@app.route('/api')def api(): if request.environ.get('wsgi.websocket'): ws = request.environ['wsgi.websocket'] while True: message = ws.wait() ws.send(message) return if __name__ == '__main__': server = WSGIServer([...],handler_class=WebSocketHandler) [...]

Abstract @ EuroPython 2013https://gist.github.com/lrvick/1185629

Page 21: Server side story

Asyncronous I/O

Is hard.• Processes and traditional threads don't

scale for real-time apps• Callback based systems are one

solution• Green threads are another one

In Python, there is no clean and easy solution.

Abstract @ EuroPython 2013

http://stackoverflow.com/a/3325985/967274

Page 22: Server side story

Tulip (PEP 3156)

Tries to provide a common groud for all frameworks targeting asyncronous I/O• Has a pluggable event loop interface• Uses futures to abstract callbacks• Allows using callbacks or coroutines

(via yield from)• Uses the concept of transports and

protocols

Abstract @ EuroPython 2013

Page 23: Server side story

Tulip (PEP 3156)

Pros and cons• Tulip isn't simple or straightforward• Because of its constraints• Reinventing the whole Python

ecosystem ain't an option• Tulip is a library, frameworks can build

on top of it• Planned to land in 3.4• Conveniently wraps parts of the

standard library

Abstract @ EuroPython 2013

Page 24: Server side story

Tulip and websockets

Tulip supports websockets• Has an example in the source, solving

the broadcaster use case• The example is fairly complete (and

complex)

Abstract @ EuroPython 2013

http://code.google.com/p/tulip/source/browse/examples/wssrv.py

Page 25: Server side story

Architectural considerations

Most of the quick fixes you're used to won't work• Caching can't be layered over the

frontend to mask problems• Code flow must receive extra care• You still need to deal with an awful lot

of synchronous calls, and orchestrate them with asynchronous calls

Abstract @ EuroPython 2013

http://python-notes.boredomandlaziness.org/en/latest/pep_ideas/async_programming.html

Page 26: Server side story

Shar(d)ed state

Real time applications behave like clusters• State is sharded between nodes• You must orchestrate and deal with

inconsistencies• People doing clusters (or cloud

systems) have already some theoretical work done

Abstract @ EuroPython 2013

Page 27: Server side story

Scaling

Once you're able to manage the shared state, scaling becomes easier and linear.However, due to the nature of the control flow, it will cost more than with non-realtime web applications.

Abstract @ EuroPython 2013

Page 28: Server side story

Security considerations

Websockets have security provisions to avoid blatant abuses.However:• Authentication and authorization are

delegated to the application• Statefulness is a double-edged sword• The protocol itself is fairly new and

underdeployed

Abstract @ EuroPython 2013

Page 29: Server side story

Security considerations (2)

Websockets use the origin model• These provisions are relevant for

browsers only• Intended to impede browser hijacking• RFC clearly states that validation of

data should always be performed on both sides

• Resist the urge to allow for arbitrary objects to be passed between server and client

Abstract @ EuroPython 2013

Page 30: Server side story

Conclusions

• Real time applications require a fundamental change of paradigm

• This paradigm change spawned a new protocol on the client side

• The server side should abandon the standard model and embrace event based asynchronous systems

• In python we already have tools to deal with these challenges, but the landscape is fragmented

Abstract @ EuroPython 2013

Page 31: Server side story

Conclusions (2)

• Python 3.4 will (hopefully) lay a common layer to address these architectural problems

• There are fundamental architectural changes that the paradigm brings to web applications, making it almost equal to native applications

• The area still needs to mature to fully expose challenges especially in the area of security

Abstract @ EuroPython 2013

Page 32: Server side story

Simone [email protected]@simonedeponti

Questions?

Abstract @ EuroPython 2013