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

Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024The Digital Insurer
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024The Digital Insurer
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 

Dernier (20)

Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 

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