SlideShare une entreprise Scribd logo
1  sur  116
Télécharger pour lire hors ligne
Scaling with event–
       based webservers
       Morten Siebuhr


       Open Source Days
       March 5th 2011




1/66
whoami(1)

       Defining “web–server work”

            Killing Apache

         Event–based servers

             Using Node.js

              Questions




2/66
whoami(1)




3/66
Computer Scientist



       Distributed Systems / Scientific Computing




          (Web–)developer @ One.com




4/66
Web–development @ one.com:



         Rich Internet Applications
         e–mail, calendar, galleries, . . .




           + 900.000 customers




5/66
6/66
Defining
       “Web–server work”




7/66
Typical web–server work




8/66
Typical web–server work



            Serving static files




8/66
Typical web–server work



            Serving static files


           Talking to databases




8/66
Typical web–server work



             Serving static files


           Talking to databases


       Not doing lots of computations




8/66
Typical web–server work



                 Serving static files


                Talking to databases


           Not doing lots of computations


       ≈ 100 → 1000 connections served ASAP




8/66
(A)typical web–server work




9/66
(A)typical web–server work



           Persistent connections




9/66
(A)typical web–server work



           Persistent connections


        = ∞ simultaneous connections




9/66
(A)typical web–server work



            Persistent connections


        = ∞ simultaneous connections


       ≈ 10000 simultaneous connections




9/66
(A)typical web–server work



                  Persistent connections


              = ∞ simultaneous connections


            ≈ 10000 simultaneous connections


       (But we don’t care as much about latency. . . )




9/66
waiting...




10/66
sleep(1.0)




11/66
12/66
Response time w. sleep(1).
                                25 s
                                                                                                      WSGI


                                20 s
        Average response time




                                15 s



                                10 s



                                 5s



                                 0s
                                       0   100   200   300     400      500      600      700   800      900   1000
                                                                Concurrent requests




13/66
Response time w. sleep(1).
                                25 s
                                                                                                           WSGI


                                20 s
        Average response time




                                15 s



                                10 s



                                 5s



                                 0s
                                       0   1000   2000   3000    4000    5000     6000       7000   8000     9000   10000
                                                                  Concurrent requests




14/66
Threading




15/66
Use threads




16/66
Wasted resources!




17/66
Add threads




18/66
Response time w. sleep(1).
                                25 s
                                                                                                   WSGI
                                                                                                  Apache

                                20 s
        Average response time




                                15 s



                                10 s



                                 5s



                                 0s
                                       0   100   200   300     400      500      600      700   800    900   1000
                                                                Concurrent requests




19/66
Response time w. sleep(1).
                                25 s
                                                                                                        WSGI
                                                                                                       Apache

                                20 s
        Average response time




                                15 s



                                10 s



                                 5s



                                 0s
                                       0   1000   2000   3000    4000    5000     6000       7000   8000   9000   10000
                                                                  Concurrent requests




20/66
21/66
22/66
23/66
Memory usage w. sleep(1).
                             1500 MB
                                                                                                 WSGI
                                                                                                Apache

                             1250 MB



                             1000 MB
        Max Virtual Memory




                             750 MB



                             500 MB



                             250 MB



                               0 MB
                                       0   100   200   300     400      500      600     700   800   900   1000
                                                                Concurrent requests




25/66
Memory usage w. sleep(1).
                             1500 MB
                                                                                                      WSGI
                                                                                                     Apache

                             1250 MB



                             1000 MB
        Max Virtual Memory




                             750 MB



                             500 MB



                             250 MB



                               0 MB
                                       0   1000   2000   3000    4000    5000     6000      7000   8000   9000   10000
                                                                  Concurrent requests




26/66
A single connection




27/66
A single connection

                 =

              1 thread




27/66
A single connection

                        =

                    1 thread

                        =

        1 C–stack & kernel data structures




27/66
A single connection

                         =

                     1 thread

                         =

        1 C–stack & kernel data structures

                         +

        Locking & switching overhead &c. . .




27/66
28/66
Add memory!




29/66
Add memory!




        Memory is cheap




29/66
Add memory!




                   Memory is cheap



        Except, actually using it isn’t cheap. . .




29/66
Memory Wall


        1986 → 2000:




30/66
Memory Wall


           1986 → 2000:


        + 55% CPU speed P/A




30/66
Memory Wall


                                 1986 → 2000:


                           + 55% CPU speed P/A


                           + 10% RAM speed P/A


        (Source: http://www.cs.virginia.edu/papers/Hitting_Memory_Wall-wulf94.pdf)




30/66
31/66
Threads and connections mapped 1:1




32/66
Threads and connections mapped 1:1



              We use a lot of threads




32/66
Threads and connections mapped 1:1



                         We use a lot of threads



        Which we don’t have the memory bandwidth to move around.




32/66
Events!




33/66
waiting...




34/66
35/66
36/66
Event-basics




37/66
Event-basics



         Event queue




37/66
Event-basics



         Event queue



          Event loop




37/66
Event-basics



          Event queue



           Event loop



        “The Event Loop”




37/66
A single connection




38/66
A single connection

                 =

              1 “Event”




38/66
A single connection

                   =

                1 “Event”

                   =

        Some variables & function




38/66
A single connection

                    =

                1 “Event”

                    =

        Some variables & function

                    =

        Hash table & some pointers




38/66
A single connection

                          =

                       1 “Event”

                          =

              Some variables & function

                          =

              Hash table & some pointers

                          +

        Queue of events waiting to be processed




38/66
39/66
39/66
39/66
39/66
Why not earlier




40/66
Why not earlier




        Simply not a problem!




40/66
Why not earlier




                            Simply not a problem!




        Traditional back–end programmers not used to thinking this way




40/66
What we gain




41/66
What we gain



        Relatively low memory use




41/66
What we gain



        Relatively low memory use



           No threading issues




41/66
What we gain



                  Relatively low memory use



                     No threading issues



        Client-side programmers have no preconditions.




41/66
Node.js




42/66
Node.js is a set of bindings to the V8 JavaScript engine
                     for scripting network programs.




                                                      — Ryan Dahl




43/66
Node.js is a set of bindings to the V8 JavaScript engine
                     for scripting network programs.




                                                      — Ryan Dahl




43/66
JavaScript




44/66
JavaScript


        Built for event-based programming.




44/66
JavaScript


        Built for event-based programming.




44/66
JavaScript


        Built for event-based programming.




        Netscape needed something fast. . .




44/66
JavaScript


                 Built for event-based programming.




                   Netscape needed something fast. . .
        . . . JavaScript “designed” and implemented in 14 days




44/66
JavaScript


                 Built for event-based programming.




                   Netscape needed something fast. . .
        . . . JavaScript “designed” and implemented in 14 days




44/66
Google V8




        JIT’ing JavaScript to native machine code




45/66
Google V8




        JIT’ing JavaScript to native machine code




                = Compiling server code




45/66
Node.js




        Non–browser V8–bindings for various libraries




                  Everything non–blocking




46/66
A minimal webserver in Node.js. . .


        // Import HTTP package
        var http = require ( ’ http ’) ;

        // Set up basic server
        var server = http . createServer ( function ( req , res ) {
           res . writeHead (200 , { ’ Content - Type ’: ’ text / plain ’ }) ;
           res . end ( ’ Hello World  n ’) ;
        }) ;

        // Start the server
        server . listen (8124 , " 127.0.0.1 " ) ;
        console . log ( ’ Server running at http : / / 1 2 7 . 0 . 0 . 1 : 8 1 2 4 / ’) ;




47/66
. . . with “database”

        // Import HTTP package
        var http = require ( ’ http ’) ;

        // Set up basic server
        var server = http . createServer ( function ( req , res ) {
           setTimeout ( function () {
              res . writeHead (200 , { ’ Content - Type ’: ’ text / plain ’ }) ;
              res . end ( ’ Hello World  n ’) ;
           } , 1000) ;
        }) ;

        // Start the server
        server . listen (8124 , " 127.0.0.1 " ) ;
        console . log ( ’ Server running at http : / / 1 2 7 . 0 . 0 . 1 : 8 1 2 4 / ’) ;




48/66
Response time w. sleep(1).
                                25 s
                                                                                                   WSGI
                                                                                                  Apache
                                                                                                  Node.js
                                20 s
        Average response time




                                15 s



                                10 s



                                 5s



                                 0s
                                       0   100   200   300     400      500      600      700   800     900   1000
                                                                Concurrent requests




49/66
Response time w. sleep(1).
                                25 s
                                                                                                        WSGI
                                                                                                       Apache
                                                                                                       Node.js
                                20 s
        Average response time




                                15 s



                                10 s



                                 5s



                                 0s
                                       0   1000   2000   3000    4000    5000     6000       7000   8000    9000   10000
                                                                  Concurrent requests




50/66
Memory usage w. sleep(1).
                             1500 MB
                                                                                                      WSGI
                                                                                                     Apache
                                                                                                     Node.js
                             1250 MB



                             1000 MB
        Max Virtual Memory




                             750 MB



                             500 MB



                             250 MB



                               0 MB
                                       0   1000   2000   3000    4000    5000     6000      7000   8000   9000   10000
                                                                  Concurrent requests




51/66
Connections in Node.js




52/66
Connections in Node.js

                  =

               1 “Event”




52/66
Connections in Node.js

                     =

                 1 “Event”

                     =

        1 Closure + callback function




52/66
Connections in Node.js

                     =

                 1 “Event”

                     =

        1 Closure + callback function

                     =

          1 struct + some pointers




52/66
Connections in Node.js

                          =

                      1 “Event”

                          =

             1 Closure + callback function

                          =

               1 struct + some pointers

                          +

        Event queue operations + function calls




52/66
Alternatives




53/66
Alternatives

        Well–written thread program!




53/66
Alternatives

        Well–written thread program!
                   Erlang
            http://www.erlang.org/




53/66
Alternatives

        Well–written thread program!
                   Erlang
            http://www.erlang.org/

                Google GO
              http://golang.org/




53/66
Alternatives

        Well–written thread program!
                    Erlang
            http://www.erlang.org/

                 Google GO
              http://golang.org/

             Lua Event Machine
          https://github.com/esmil/lem




53/66
Alternatives

        Well–written thread program!
                    Erlang
            http://www.erlang.org/

                 Google GO
              http://golang.org/

             Lua Event Machine
          https://github.com/esmil/lem

                    Nginx
            http://wiki.nginx.org/




53/66
Alternatives

        Well–written thread program!
                    Erlang
            http://www.erlang.org/

                 Google GO
              http://golang.org/

             Lua Event Machine
          https://github.com/esmil/lem

                    Nginx
            http://wiki.nginx.org/

                   Varnish
         http://www.varnish-cache.org/




53/66
Working with Node.js




54/66
I’m new to JavaScript!




55/66
56/66
JavaScript WTFs


                         Always add ;!


                  Function arguments. . .


                this   scoped to callee object!


        Stacktraces + events sometimes look strange




57/66
JSLint




58/66
JSLint

        www.crockfordfacts.com




58/66
Nice code!


        def process ( user ) :
          user = getAuth ( user )
          if not user :
            return ’ Fail ’

          db = getC onnnecti on ( params )
          data = db . getUserData ( user )
          if not data :
            return ’ Fail ’

          return data




59/66
Boomerang code

        function process ( user , callback ) {
          getAuth ( req . username , function ( err , user ) {
            if ( err ) return callback ( err ) ;

              g etDB Co n ne ct io n ( params , function ( err , db ) {
                 if ( err ) return callback ( err ) ;

                db . getUserData ( user , function ( err , data ) {
                   if ( err ) return callback ( err ) ;

                     return callback ( null , data ) ;
                  }) ;
               }) ;
            }) ;
        }




60/66
NPM




61/66
Express.js



        var app = require ( ’ express ’) . createServer () ;

        app . get ( ’/ ’ , function ( req , res ) {
           setTimeout ( function () {
              res . send ( ’ Hello World ’) ;
           } , 1000) ;
        }) ;

        app . listen (3000) ;




62/66
Express.js - Chat server

        var app = require ( ’ express ’) . createServer () ,
            chat = [];

        app . get ( ’/ ’ , function ( req , res ) {
           chat . push ( res ) ;
        }) ;

        app . get ( ’ /: msg ’ , function ( req , res ) {
           chat . forEach ( function ( conn ) {
              conn . write ( req . params . msg ) ;
           }) ;
        }) ;

        app . listen (3000) ;




63/66
Questions

          (The end)




64/66
http://nodejs.org/




        https://github.com/joyent/node




65/66
function process ( user , callback ) {
          var user = null , db = null ;
          function done_ () {
            if ( user && db ) {
              db . getUserData ( user , function ( err , data ) {
                 if ( err ) return callback ( err ) ;
                 return callback ( null , data ) ;
              }) ;
            }
          }

            getAuth ( req . username , function ( err , retuser ) {
               if ( err ) return callback ( err ) ;
               user = retuser ;
               done_ () ;
            }) ;

            g et DBCo n ne ct i on ( params , function ( err , retdb ) {
               if ( err ) return callback ( err ) ;
               db = retdb ;
               done_ () ;
            }) ;
        }




66/66

Contenu connexe

En vedette

High Availability Django - Djangocon 2016
High Availability Django - Djangocon 2016High Availability Django - Djangocon 2016
High Availability Django - Djangocon 2016Frankie Dintino
 
PyCon AU 2012 - Debugging Live Python Web Applications
PyCon AU 2012 - Debugging Live Python Web ApplicationsPyCon AU 2012 - Debugging Live Python Web Applications
PyCon AU 2012 - Debugging Live Python Web ApplicationsGraham Dumpleton
 
PyCon US 2012 - Web Server Bottlenecks and Performance Tuning
PyCon US 2012 - Web Server Bottlenecks and Performance TuningPyCon US 2012 - Web Server Bottlenecks and Performance Tuning
PyCon US 2012 - Web Server Bottlenecks and Performance TuningGraham Dumpleton
 
Django deployment and rpm+yum
Django deployment and rpm+yumDjango deployment and rpm+yum
Django deployment and rpm+yumWalter Liu
 
MongoATL: How Sourceforge is Using MongoDB
MongoATL: How Sourceforge is Using MongoDBMongoATL: How Sourceforge is Using MongoDB
MongoATL: How Sourceforge is Using MongoDBRick Copeland
 
Python Deployment with Fabric
Python Deployment with FabricPython Deployment with Fabric
Python Deployment with Fabricandymccurdy
 
Django Rest Framework and React and Redux, Oh My!
Django Rest Framework and React and Redux, Oh My!Django Rest Framework and React and Redux, Oh My!
Django Rest Framework and React and Redux, Oh My!Eric Palakovich Carr
 
OpenStack API's and WSGI
OpenStack API's and WSGIOpenStack API's and WSGI
OpenStack API's and WSGIMike Pittaro
 

En vedette (9)

High Availability Django - Djangocon 2016
High Availability Django - Djangocon 2016High Availability Django - Djangocon 2016
High Availability Django - Djangocon 2016
 
PyCon AU 2012 - Debugging Live Python Web Applications
PyCon AU 2012 - Debugging Live Python Web ApplicationsPyCon AU 2012 - Debugging Live Python Web Applications
PyCon AU 2012 - Debugging Live Python Web Applications
 
PyCon US 2012 - Web Server Bottlenecks and Performance Tuning
PyCon US 2012 - Web Server Bottlenecks and Performance TuningPyCon US 2012 - Web Server Bottlenecks and Performance Tuning
PyCon US 2012 - Web Server Bottlenecks and Performance Tuning
 
Django deployment and rpm+yum
Django deployment and rpm+yumDjango deployment and rpm+yum
Django deployment and rpm+yum
 
MongoATL: How Sourceforge is Using MongoDB
MongoATL: How Sourceforge is Using MongoDBMongoATL: How Sourceforge is Using MongoDB
MongoATL: How Sourceforge is Using MongoDB
 
Python Deployment with Fabric
Python Deployment with FabricPython Deployment with Fabric
Python Deployment with Fabric
 
Django Rest Framework and React and Redux, Oh My!
Django Rest Framework and React and Redux, Oh My!Django Rest Framework and React and Redux, Oh My!
Django Rest Framework and React and Redux, Oh My!
 
OpenStack API's and WSGI
OpenStack API's and WSGIOpenStack API's and WSGI
OpenStack API's and WSGI
 
Beyond OpenStack
Beyond OpenStackBeyond OpenStack
Beyond OpenStack
 

Dernier

Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...CzechDreamin
 
Using IESVE for Room Loads Analysis - UK & Ireland
Using IESVE for Room Loads Analysis - UK & IrelandUsing IESVE for Room Loads Analysis - UK & Ireland
Using IESVE for Room Loads Analysis - UK & IrelandIES VE
 
WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024Lorenzo Miniero
 
Working together SRE & Platform Engineering
Working together SRE & Platform EngineeringWorking together SRE & Platform Engineering
Working together SRE & Platform EngineeringMarcus Vechiato
 
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdfThe Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdfFIDO Alliance
 
ECS 2024 Teams Premium - Pretty Secure
ECS 2024   Teams Premium - Pretty SecureECS 2024   Teams Premium - Pretty Secure
ECS 2024 Teams Premium - Pretty SecureFemke de Vroome
 
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdfIntroduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdfFIDO Alliance
 
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...panagenda
 
IESVE for Early Stage Design and Planning
IESVE for Early Stage Design and PlanningIESVE for Early Stage Design and Planning
IESVE for Early Stage Design and PlanningIES VE
 
State of the Smart Building Startup Landscape 2024!
State of the Smart Building Startup Landscape 2024!State of the Smart Building Startup Landscape 2024!
State of the Smart Building Startup Landscape 2024!Memoori
 
WebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM PerformanceWebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM PerformanceSamy Fodil
 
A Business-Centric Approach to Design System Strategy
A Business-Centric Approach to Design System StrategyA Business-Centric Approach to Design System Strategy
A Business-Centric Approach to Design System StrategyUXDXConf
 
Easier, Faster, and More Powerful – Notes Document Properties Reimagined
Easier, Faster, and More Powerful – Notes Document Properties ReimaginedEasier, Faster, and More Powerful – Notes Document Properties Reimagined
Easier, Faster, and More Powerful – Notes Document Properties Reimaginedpanagenda
 
Long journey of Ruby Standard library at RubyKaigi 2024
Long journey of Ruby Standard library at RubyKaigi 2024Long journey of Ruby Standard library at RubyKaigi 2024
Long journey of Ruby Standard library at RubyKaigi 2024Hiroshi SHIBATA
 
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdfLinux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdfFIDO Alliance
 
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...FIDO Alliance
 
Google I/O Extended 2024 Warsaw
Google I/O Extended 2024 WarsawGoogle I/O Extended 2024 Warsaw
Google I/O Extended 2024 WarsawGDSC PJATK
 
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...FIDO Alliance
 
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdfHow Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdfFIDO Alliance
 

Dernier (20)

Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
 
Overview of Hyperledger Foundation
Overview of Hyperledger FoundationOverview of Hyperledger Foundation
Overview of Hyperledger Foundation
 
Using IESVE for Room Loads Analysis - UK & Ireland
Using IESVE for Room Loads Analysis - UK & IrelandUsing IESVE for Room Loads Analysis - UK & Ireland
Using IESVE for Room Loads Analysis - UK & Ireland
 
WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024
 
Working together SRE & Platform Engineering
Working together SRE & Platform EngineeringWorking together SRE & Platform Engineering
Working together SRE & Platform Engineering
 
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdfThe Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
 
ECS 2024 Teams Premium - Pretty Secure
ECS 2024   Teams Premium - Pretty SecureECS 2024   Teams Premium - Pretty Secure
ECS 2024 Teams Premium - Pretty Secure
 
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdfIntroduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
 
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
 
IESVE for Early Stage Design and Planning
IESVE for Early Stage Design and PlanningIESVE for Early Stage Design and Planning
IESVE for Early Stage Design and Planning
 
State of the Smart Building Startup Landscape 2024!
State of the Smart Building Startup Landscape 2024!State of the Smart Building Startup Landscape 2024!
State of the Smart Building Startup Landscape 2024!
 
WebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM PerformanceWebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM Performance
 
A Business-Centric Approach to Design System Strategy
A Business-Centric Approach to Design System StrategyA Business-Centric Approach to Design System Strategy
A Business-Centric Approach to Design System Strategy
 
Easier, Faster, and More Powerful – Notes Document Properties Reimagined
Easier, Faster, and More Powerful – Notes Document Properties ReimaginedEasier, Faster, and More Powerful – Notes Document Properties Reimagined
Easier, Faster, and More Powerful – Notes Document Properties Reimagined
 
Long journey of Ruby Standard library at RubyKaigi 2024
Long journey of Ruby Standard library at RubyKaigi 2024Long journey of Ruby Standard library at RubyKaigi 2024
Long journey of Ruby Standard library at RubyKaigi 2024
 
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdfLinux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
 
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
 
Google I/O Extended 2024 Warsaw
Google I/O Extended 2024 WarsawGoogle I/O Extended 2024 Warsaw
Google I/O Extended 2024 Warsaw
 
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...
 
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdfHow Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
 

Scaling with event-based webservers

  • 1. Scaling with event– based webservers Morten Siebuhr Open Source Days March 5th 2011 1/66
  • 2. whoami(1) Defining “web–server work” Killing Apache Event–based servers Using Node.js Questions 2/66
  • 4. Computer Scientist Distributed Systems / Scientific Computing (Web–)developer @ One.com 4/66
  • 5. Web–development @ one.com: Rich Internet Applications e–mail, calendar, galleries, . . . + 900.000 customers 5/66
  • 7. Defining “Web–server work” 7/66
  • 9. Typical web–server work Serving static files 8/66
  • 10. Typical web–server work Serving static files Talking to databases 8/66
  • 11. Typical web–server work Serving static files Talking to databases Not doing lots of computations 8/66
  • 12. Typical web–server work Serving static files Talking to databases Not doing lots of computations ≈ 100 → 1000 connections served ASAP 8/66
  • 14. (A)typical web–server work Persistent connections 9/66
  • 15. (A)typical web–server work Persistent connections = ∞ simultaneous connections 9/66
  • 16. (A)typical web–server work Persistent connections = ∞ simultaneous connections ≈ 10000 simultaneous connections 9/66
  • 17. (A)typical web–server work Persistent connections = ∞ simultaneous connections ≈ 10000 simultaneous connections (But we don’t care as much about latency. . . ) 9/66
  • 20. 12/66
  • 21. Response time w. sleep(1). 25 s WSGI 20 s Average response time 15 s 10 s 5s 0s 0 100 200 300 400 500 600 700 800 900 1000 Concurrent requests 13/66
  • 22. Response time w. sleep(1). 25 s WSGI 20 s Average response time 15 s 10 s 5s 0s 0 1000 2000 3000 4000 5000 6000 7000 8000 9000 10000 Concurrent requests 14/66
  • 27. Response time w. sleep(1). 25 s WSGI Apache 20 s Average response time 15 s 10 s 5s 0s 0 100 200 300 400 500 600 700 800 900 1000 Concurrent requests 19/66
  • 28. Response time w. sleep(1). 25 s WSGI Apache 20 s Average response time 15 s 10 s 5s 0s 0 1000 2000 3000 4000 5000 6000 7000 8000 9000 10000 Concurrent requests 20/66
  • 29. 21/66
  • 30. 22/66
  • 31. 23/66
  • 32.
  • 33. Memory usage w. sleep(1). 1500 MB WSGI Apache 1250 MB 1000 MB Max Virtual Memory 750 MB 500 MB 250 MB 0 MB 0 100 200 300 400 500 600 700 800 900 1000 Concurrent requests 25/66
  • 34. Memory usage w. sleep(1). 1500 MB WSGI Apache 1250 MB 1000 MB Max Virtual Memory 750 MB 500 MB 250 MB 0 MB 0 1000 2000 3000 4000 5000 6000 7000 8000 9000 10000 Concurrent requests 26/66
  • 36. A single connection = 1 thread 27/66
  • 37. A single connection = 1 thread = 1 C–stack & kernel data structures 27/66
  • 38. A single connection = 1 thread = 1 C–stack & kernel data structures + Locking & switching overhead &c. . . 27/66
  • 39. 28/66
  • 41. Add memory! Memory is cheap 29/66
  • 42. Add memory! Memory is cheap Except, actually using it isn’t cheap. . . 29/66
  • 43. Memory Wall 1986 → 2000: 30/66
  • 44. Memory Wall 1986 → 2000: + 55% CPU speed P/A 30/66
  • 45. Memory Wall 1986 → 2000: + 55% CPU speed P/A + 10% RAM speed P/A (Source: http://www.cs.virginia.edu/papers/Hitting_Memory_Wall-wulf94.pdf) 30/66
  • 46. 31/66
  • 47. Threads and connections mapped 1:1 32/66
  • 48. Threads and connections mapped 1:1 We use a lot of threads 32/66
  • 49. Threads and connections mapped 1:1 We use a lot of threads Which we don’t have the memory bandwidth to move around. 32/66
  • 52. 35/66
  • 53. 36/66
  • 55. Event-basics Event queue 37/66
  • 56. Event-basics Event queue Event loop 37/66
  • 57. Event-basics Event queue Event loop “The Event Loop” 37/66
  • 59. A single connection = 1 “Event” 38/66
  • 60. A single connection = 1 “Event” = Some variables & function 38/66
  • 61. A single connection = 1 “Event” = Some variables & function = Hash table & some pointers 38/66
  • 62. A single connection = 1 “Event” = Some variables & function = Hash table & some pointers + Queue of events waiting to be processed 38/66
  • 63. 39/66
  • 64. 39/66
  • 65. 39/66
  • 66. 39/66
  • 68. Why not earlier Simply not a problem! 40/66
  • 69. Why not earlier Simply not a problem! Traditional back–end programmers not used to thinking this way 40/66
  • 71. What we gain Relatively low memory use 41/66
  • 72. What we gain Relatively low memory use No threading issues 41/66
  • 73. What we gain Relatively low memory use No threading issues Client-side programmers have no preconditions. 41/66
  • 75. Node.js is a set of bindings to the V8 JavaScript engine for scripting network programs. — Ryan Dahl 43/66
  • 76. Node.js is a set of bindings to the V8 JavaScript engine for scripting network programs. — Ryan Dahl 43/66
  • 78. JavaScript Built for event-based programming. 44/66
  • 79. JavaScript Built for event-based programming. 44/66
  • 80. JavaScript Built for event-based programming. Netscape needed something fast. . . 44/66
  • 81. JavaScript Built for event-based programming. Netscape needed something fast. . . . . . JavaScript “designed” and implemented in 14 days 44/66
  • 82. JavaScript Built for event-based programming. Netscape needed something fast. . . . . . JavaScript “designed” and implemented in 14 days 44/66
  • 83. Google V8 JIT’ing JavaScript to native machine code 45/66
  • 84. Google V8 JIT’ing JavaScript to native machine code = Compiling server code 45/66
  • 85. Node.js Non–browser V8–bindings for various libraries Everything non–blocking 46/66
  • 86. A minimal webserver in Node.js. . . // Import HTTP package var http = require ( ’ http ’) ; // Set up basic server var server = http . createServer ( function ( req , res ) { res . writeHead (200 , { ’ Content - Type ’: ’ text / plain ’ }) ; res . end ( ’ Hello World n ’) ; }) ; // Start the server server . listen (8124 , " 127.0.0.1 " ) ; console . log ( ’ Server running at http : / / 1 2 7 . 0 . 0 . 1 : 8 1 2 4 / ’) ; 47/66
  • 87. . . . with “database” // Import HTTP package var http = require ( ’ http ’) ; // Set up basic server var server = http . createServer ( function ( req , res ) { setTimeout ( function () { res . writeHead (200 , { ’ Content - Type ’: ’ text / plain ’ }) ; res . end ( ’ Hello World n ’) ; } , 1000) ; }) ; // Start the server server . listen (8124 , " 127.0.0.1 " ) ; console . log ( ’ Server running at http : / / 1 2 7 . 0 . 0 . 1 : 8 1 2 4 / ’) ; 48/66
  • 88. Response time w. sleep(1). 25 s WSGI Apache Node.js 20 s Average response time 15 s 10 s 5s 0s 0 100 200 300 400 500 600 700 800 900 1000 Concurrent requests 49/66
  • 89. Response time w. sleep(1). 25 s WSGI Apache Node.js 20 s Average response time 15 s 10 s 5s 0s 0 1000 2000 3000 4000 5000 6000 7000 8000 9000 10000 Concurrent requests 50/66
  • 90. Memory usage w. sleep(1). 1500 MB WSGI Apache Node.js 1250 MB 1000 MB Max Virtual Memory 750 MB 500 MB 250 MB 0 MB 0 1000 2000 3000 4000 5000 6000 7000 8000 9000 10000 Concurrent requests 51/66
  • 92. Connections in Node.js = 1 “Event” 52/66
  • 93. Connections in Node.js = 1 “Event” = 1 Closure + callback function 52/66
  • 94. Connections in Node.js = 1 “Event” = 1 Closure + callback function = 1 struct + some pointers 52/66
  • 95. Connections in Node.js = 1 “Event” = 1 Closure + callback function = 1 struct + some pointers + Event queue operations + function calls 52/66
  • 97. Alternatives Well–written thread program! 53/66
  • 98. Alternatives Well–written thread program! Erlang http://www.erlang.org/ 53/66
  • 99. Alternatives Well–written thread program! Erlang http://www.erlang.org/ Google GO http://golang.org/ 53/66
  • 100. Alternatives Well–written thread program! Erlang http://www.erlang.org/ Google GO http://golang.org/ Lua Event Machine https://github.com/esmil/lem 53/66
  • 101. Alternatives Well–written thread program! Erlang http://www.erlang.org/ Google GO http://golang.org/ Lua Event Machine https://github.com/esmil/lem Nginx http://wiki.nginx.org/ 53/66
  • 102. Alternatives Well–written thread program! Erlang http://www.erlang.org/ Google GO http://golang.org/ Lua Event Machine https://github.com/esmil/lem Nginx http://wiki.nginx.org/ Varnish http://www.varnish-cache.org/ 53/66
  • 104. I’m new to JavaScript! 55/66
  • 105. 56/66
  • 106. JavaScript WTFs Always add ;! Function arguments. . . this scoped to callee object! Stacktraces + events sometimes look strange 57/66
  • 108. JSLint www.crockfordfacts.com 58/66
  • 109. Nice code! def process ( user ) : user = getAuth ( user ) if not user : return ’ Fail ’ db = getC onnnecti on ( params ) data = db . getUserData ( user ) if not data : return ’ Fail ’ return data 59/66
  • 110. Boomerang code function process ( user , callback ) { getAuth ( req . username , function ( err , user ) { if ( err ) return callback ( err ) ; g etDB Co n ne ct io n ( params , function ( err , db ) { if ( err ) return callback ( err ) ; db . getUserData ( user , function ( err , data ) { if ( err ) return callback ( err ) ; return callback ( null , data ) ; }) ; }) ; }) ; } 60/66
  • 112. Express.js var app = require ( ’ express ’) . createServer () ; app . get ( ’/ ’ , function ( req , res ) { setTimeout ( function () { res . send ( ’ Hello World ’) ; } , 1000) ; }) ; app . listen (3000) ; 62/66
  • 113. Express.js - Chat server var app = require ( ’ express ’) . createServer () , chat = []; app . get ( ’/ ’ , function ( req , res ) { chat . push ( res ) ; }) ; app . get ( ’ /: msg ’ , function ( req , res ) { chat . forEach ( function ( conn ) { conn . write ( req . params . msg ) ; }) ; }) ; app . listen (3000) ; 63/66
  • 114. Questions (The end) 64/66
  • 115. http://nodejs.org/ https://github.com/joyent/node 65/66
  • 116. function process ( user , callback ) { var user = null , db = null ; function done_ () { if ( user && db ) { db . getUserData ( user , function ( err , data ) { if ( err ) return callback ( err ) ; return callback ( null , data ) ; }) ; } } getAuth ( req . username , function ( err , retuser ) { if ( err ) return callback ( err ) ; user = retuser ; done_ () ; }) ; g et DBCo n ne ct i on ( params , function ( err , retdb ) { if ( err ) return callback ( err ) ; db = retdb ; done_ () ; }) ; } 66/66