SlideShare a Scribd company logo
Server Side Story
An asynchronous ballet
Simone Deponti - simone.deponti@abstract.itEuroPython 2013
Who am I
Python web developer since 2006
Work at Abstract as web developer
Plone contributor
Do more Javascript than I'm willing to
admit
Abstract @ EuroPython 2013
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)
A LITTLE BIT OF HISTORY
Let there be <a> link
The web was born as:
• Simple document retrieval
• Interlinking of documents
Abstract @ EuroPython 2013
Have you seen?
Next step:
• Searching documents
• Query parameters
• POST and Forms
• CGI
• We start having stuff that's not
documents
Abstract @ EuroPython 2013
Statefulness
HTTP is stateless
• stateless isn't good for applications
• cookies add statefulness
Abstract @ EuroPython 2013
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
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
CURRENT SITUATION
Web applications
So far:
• the server is the master
• all data lives there
• the client requests stuff and gets a
reply
Abstract @ EuroPython 2013
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
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
SOLUTIONS
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
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
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
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
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
Websockets in Python
from geventwebsocket.handler import WebSocketHandler
from 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 2013
https://gist.github.com/lrvick/1185629
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
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
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
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
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
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
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
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
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
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
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
Simone Deponti
simone.deponti@abstract.it
@simonedeponti
Questions?
Abstract @ EuroPython 2013

More Related Content

PPTX
Reactive programming with rx java
PPTX
Reactive programming
PPTX
Reactive programming
PPTX
Mini-Training: Let's have a rest
PDF
Introduction to reactive programming
PDF
Reactive java - Reactive Programming + RxJava
PPTX
Reactive programming intro
PDF
How netty works internally
Reactive programming with rx java
Reactive programming
Reactive programming
Mini-Training: Let's have a rest
Introduction to reactive programming
Reactive java - Reactive Programming + RxJava
Reactive programming intro
How netty works internally

What's hot (20)

PPTX
Reactive programming
PPTX
Training – Going Async
PPTX
Mini training - Reactive Extensions (Rx)
PDF
Олександр Хотемський:”Serverless архітектура та її застосування в автоматизац...
PDF
Matteo Vaccari - Going Frameworkless in the Backend - Codemotion Milan 2018
PDF
Declarative Concurrency with Reactive Programming
PPTX
Functional reactive programming
PDF
Reactive programming and RxJS
PDF
A pitfall when writing an asynchronous web application with Spring and Tomcat
PDF
Lightweight Java EE with MicroProfile
PPTX
Project Reactor By Example
PPTX
Managing state in modern React web applications
PDF
React Testing
PDF
Hexagonal architecture message-oriented software design
PDF
Hexagonal architecture - message-oriented software design (PHP Barcelona 2015)
PPTX
Hibernate performance tuning
PDF
Clojure 1.8 Direct-Linking WWH
PPTX
Akka for big data developers
PDF
Reactive programming - Observable
PPTX
Async Programming in C# 5
Reactive programming
Training – Going Async
Mini training - Reactive Extensions (Rx)
Олександр Хотемський:”Serverless архітектура та її застосування в автоматизац...
Matteo Vaccari - Going Frameworkless in the Backend - Codemotion Milan 2018
Declarative Concurrency with Reactive Programming
Functional reactive programming
Reactive programming and RxJS
A pitfall when writing an asynchronous web application with Spring and Tomcat
Lightweight Java EE with MicroProfile
Project Reactor By Example
Managing state in modern React web applications
React Testing
Hexagonal architecture message-oriented software design
Hexagonal architecture - message-oriented software design (PHP Barcelona 2015)
Hibernate performance tuning
Clojure 1.8 Direct-Linking WWH
Akka for big data developers
Reactive programming - Observable
Async Programming in C# 5
Ad

Similar to Server side story (20)

PDF
Real time web apps
PDF
Server-Sent Events (real-time HTTP push for HTML5 browsers)
PPT
Get Real: Adventures in realtime web apps
PPT
PDF
Real time web_apps_pycon2012-v1
PDF
Building real time applications with Symfony2
PDF
Real-Time with Flowdock
PPT
Real-Time Python Web: Gevent and Socket.io
PDF
HTML5/JavaScript Communication APIs - DPC 2014
PDF
Reactive & Realtime Web Applications with TurboGears2
PDF
WebSocket in Enterprise Applications 2015
PDF
WebCamp Ukraine 2016: Instant messenger with Python. Back-end development
PDF
Communication in Python and the C10k problem
PDF
Building Web APIs that Scale
PDF
IRJET- An Overview of Web Sockets: The Future of Real-Time Communication
PDF
Building Next Generation Real-Time Web Applications using Websockets
PPTX
Training Webinar: Enterprise application performance with server push technol...
PDF
Adding Realtime to your Projects
PDF
WebCamp 2016: Python. Вячеслав Каковский: Real-time мессенджер на Python. Осо...
PPTX
Think async
Real time web apps
Server-Sent Events (real-time HTTP push for HTML5 browsers)
Get Real: Adventures in realtime web apps
Real time web_apps_pycon2012-v1
Building real time applications with Symfony2
Real-Time with Flowdock
Real-Time Python Web: Gevent and Socket.io
HTML5/JavaScript Communication APIs - DPC 2014
Reactive & Realtime Web Applications with TurboGears2
WebSocket in Enterprise Applications 2015
WebCamp Ukraine 2016: Instant messenger with Python. Back-end development
Communication in Python and the C10k problem
Building Web APIs that Scale
IRJET- An Overview of Web Sockets: The Future of Real-Time Communication
Building Next Generation Real-Time Web Applications using Websockets
Training Webinar: Enterprise application performance with server push technol...
Adding Realtime to your Projects
WebCamp 2016: Python. Вячеслав Каковский: Real-time мессенджер на Python. Осо...
Think async
Ad

Recently uploaded (20)

PDF
cuic standard and advanced reporting.pdf
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPT
Teaching material agriculture food technology
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Electronic commerce courselecture one. Pdf
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Approach and Philosophy of On baking technology
PDF
Encapsulation theory and applications.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
A Presentation on Artificial Intelligence
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PPTX
Big Data Technologies - Introduction.pptx
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Machine learning based COVID-19 study performance prediction
PPTX
Cloud computing and distributed systems.
cuic standard and advanced reporting.pdf
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
MIND Revenue Release Quarter 2 2025 Press Release
Teaching material agriculture food technology
Mobile App Security Testing_ A Comprehensive Guide.pdf
Electronic commerce courselecture one. Pdf
MYSQL Presentation for SQL database connectivity
Chapter 3 Spatial Domain Image Processing.pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
Approach and Philosophy of On baking technology
Encapsulation theory and applications.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
A Presentation on Artificial Intelligence
“AI and Expert System Decision Support & Business Intelligence Systems”
NewMind AI Weekly Chronicles - August'25-Week II
Big Data Technologies - Introduction.pptx
Programs and apps: productivity, graphics, security and other tools
Machine learning based COVID-19 study performance prediction
Cloud computing and distributed systems.

Server side story

  • 1. Server Side Story An asynchronous ballet Simone Deponti - simone.deponti@abstract.itEuroPython 2013
  • 2. Who am I Python web developer since 2006 Work at Abstract as web developer Plone contributor Do more Javascript than I'm willing to admit Abstract @ EuroPython 2013
  • 3. 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)
  • 4. A LITTLE BIT OF HISTORY
  • 5. Let there be <a> link The web was born as: • Simple document retrieval • Interlinking of documents Abstract @ EuroPython 2013
  • 6. Have you seen? Next step: • Searching documents • Query parameters • POST and Forms • CGI • We start having stuff that's not documents Abstract @ EuroPython 2013
  • 7. Statefulness HTTP is stateless • stateless isn't good for applications • cookies add statefulness Abstract @ EuroPython 2013
  • 8. 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
  • 9. 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
  • 11. Web applications So far: • the server is the master • all data lives there • the client requests stuff and gets a reply Abstract @ EuroPython 2013
  • 12. 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
  • 13. 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
  • 15. 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
  • 16. 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
  • 17. 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
  • 18. 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
  • 19. 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
  • 20. Websockets in Python from geventwebsocket.handler import WebSocketHandler from 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 2013 https://gist.github.com/lrvick/1185629
  • 21. 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
  • 22. 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
  • 23. 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
  • 24. 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
  • 25. 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
  • 26. 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
  • 27. 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
  • 28. 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
  • 29. 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
  • 30. 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
  • 31. 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