Upgrade to Pro — share decks privately, control downloads, hide ads and more …

JavaScript Code at the Speed of Light

JavaScript Code at the Speed of Light

Using Content Delivery Networks and CDNs for better #webperf.

Clay Smith

April 16, 2016
Tweet

More Decks by Clay Smith

Other Decks in Programming

Transcript

  1. JavaScript Code at the Speed of Light Clay Smith, New

    Relic JSConfUY - April 16, 2016 @smithclay
  2. ¿Por qué estoy aquí? • Been nerding out on internet

    protocols (last week: IETF 95). • Think a better understanding of the network layer is really important.
  3. More hosts === more complexity e.test/app.js index.html e.test/app.css "My App"

    g.test/analytics.js f.test/bootstrap.css z.test/font.woff z.test/foo.svg HTTP
  4. Are we building distributed systems? "A distributed system consists of

    a collection of distinct processes which are spatially separated, and which communicate with one another by exchanging messages." —Leslie Lamport, 1978 See also: http://dl.acm.org/citation.cfm?id=2482856
  5. J.M. Wing's Three Drivers of Computing Society Technology Science From:

    Computational Thinking and Thinking about Computing (2008)
  6. It's not fast enough Source Destination Great Circle Distance Round

    Trip Time in Fiber SFO London 5,375 miles 84 ms SFO Cape Town 10,124 miles 158 ms SFO Montevideo 6,554 miles 102 ms 1 ms at speed of light = Slightly less than width of Uruguay
  7. Origin Server Moving it closer with CDNs Browser CDN Edge

    HTTP HTTP Short distance Long Distance HTTP requests get routed to a nearby location using the CDN network.
  8. Use the headers • Set headers on responses control to

    caching: • Cache-Control: max-age=10, public • The bigger the max-age is, the longer it is cached. • Always verify settings (check response headers in Chrome web inspector or cURL) Great resource: http://www.mobify.com/blog/beginners-guide-to-http-cache-headers/
  9. File asset revisioning • The filename has an MD5 hash

    of the contents: app-d2ae34123.js • Cache-Control: max-age=31536000, public • Use a grunt and gulp plugin— someone has already done this (i.e. https://github.com/cbas/ grunt-rev)
  10. What if the CDN goes down? <script src="https://code.jquery.com/ jquery-1.12.2.min.js"></script> <script>window.jQuery

    || document.write('<script src="js/vendor/ jquery-1.12.2.min.js"><\/script>')</script> https://github.com/h5bp/html5-boilerplate/blob/master/dist/ index.html Future: https://w3c.github.io/network-error-logging/
  11. The OSI Model explained using a chivito Physical (ethernet/pan) Data

    Link (ethernet/rodajas de tomate) Network (IP/panceta) Transport (TCP/jamón cocido) Session (mozzarella) Presentation (JPG/mayonesa) Application (HTTP/Currasco) Lower Layers Upper Layers
  12. GET /cat.gif GET /app.css 200 a.js HTTP/2: one connection, many

    requests 404 favicon.ico Single TCP Connection (Multiplexing) Client Server
  13. HTTP/2 es tu amigo • No more concatenation (mega- app.js)

    • Adios, inlining. • Adios, domain sharding. • Server Push (highly experimental)
  14. Server Browser CDN Edge HTTP/2 HTTP/1 Some CDNs already support

    HTTP/2. Full list is here: https:// istlsfastyet.com/ H2 + CDNs
  15. How do we build complex [JavaScript apps] simply? One of

    the "unresolved questions in computing":