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

From application latency to animated data (and ...

ctranxuan
December 09, 2015

From application latency to animated data (and to APIs streaming)

We all know that is better to code an application with good response time...
But how much effort do you make fot the optimisation of the application latency?
Why Netflix hasc created its own format (JSON-G) to improve the display of its movies cover?
Come and discover how our brain works and how it has an effect on how we perceive the latency. Why our brain pays attention to movements? Why we will shift smoother from animated UI to animated data?
And come to get an overview of some trends to reduce the latency.

ctranxuan

December 09, 2015
Tweet

More Decks by ctranxuan

Other Decks in Programming

Transcript

  1. £at€nc€... i$ Møn€¥ £øss £at€nc€... i$ Møn€¥ £øss Amazon found

    every 100 milliseconds of Amazon found every 100 milliseconds of apps latency cost them 1% in sales. apps latency cost them 1% in sales. Source: http://perspectives.mvdirona.com/2009/10/31/TheCostOfLatency.aspx Google found an extra 0.5 seconds in Google found an extra 0.5 seconds in search page generation time dropped search page generation time dropped traffic by 20% traffic by 20% Source: http://highscalability.com/latency-everywhere-and-it-costs-you-sales-how-crush-it
  2. -100ms = -1% revenue & -5% -100ms = -1% revenue

    & -5% page views page views Source: Source: Source: http://www.webperformancetoday.com/2011/11/08/velocity-faster-mobile-sites-business-kpis-case-studies/ http://www.ebaytechblog.com/2013/03/29/measuring-real-user-experience-with-site-speed-gauge/ https://blog.radware.com/applicationdelivery/applicationaccelerationoptimization/2013/06/web-performance- poverty-line/
  3. Round Trip Delays (ping time) Round Trip Delays (ping time)

    In ADSL, first useful piece of data in 60ms (HTTPS) In 4G / LTE, first useful piece of data in 300 ms (450 ms in 3G)
  4. Constraints Constraints HTTP 1.1 HTTP 1.1 SSL / TLS SSL

    / TLS Increasing global traffic Increasing global traffic Mobile First (3G / 4G) Mobile First (3G / 4G)
  5. HTTP HTTP 1996 1996 1999 1999 HTTP/1.0 HTTP/1.0 HTTP/1.1 HTTP/1.1

    static content small size objects small # of objects 201x 201x dynamic content larger size objects more objects Matthew Yohe [CC BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0) or GFDL (http://www.gnu.org/copyleft/fdl.html)], via Wikimedia Commons more objects even more larger objects
  6. HTTP HTTP Web page: ~ 2 Mb ~ 80 elements

    ~ 38 TCP connections source: http://httparchive.org
  7. HTTP/1.x issues HTTP/1.x issues Web page: Web page: ~ 2

    Mb ~ 2 Mb ~ 80 elements ~ 80 elements Head of line blocking Head of line blocking
  8. HTTP/1.x: spriting HTTP/1.x: spriting By Kriplozoik CC BY 3.0 (http://creativecommons.org/licenses/by/3.0)],

    via Wikimedia Commons #home { left: 0px; width: 46px; background: url('img_navsprites.gif') 0 0; } #prev { left: 63px; width: 43px; background: url('img_navsprites.gif') -47px 0; } #next { left: 129px; width: 43px; background: url('img_navsprites.gif') -91px 0; }
  9. HTTP/1.x: concatenation HTTP/1.x: concatenation <html> <head> <title>AlpesJUG</title> <meta charset="utf-8"> <meta

    http-equiv="X-UA-Compatible" content="IE=edge"> <meta content="width=device-width,initial-scale=1" name="viewport"> <link rel="stylesheet" href="assets/styles/main-f11f93b619.css"> <link rel="stylesheet" href="assets/styles/theme-564f6756da.css"> <link rel="stylesheet" href="assets/styles/ie-d41d8cd98f.css"> </head> <body>...</body> <script src="scripts/vendor-a6d64f3f39.js"></script> <script src="scripts/app-0d43bb3b4b.js"></script> <script src="scripts/theme-439fdb6f87.js"> </html> 1 change => invalidate all the cache
  10. HTTP HTTP 1996 1996 1999 1999 2009 2009 2015 2015

    HTTP/1.0 HTTP/1.0 HTTP/1.1 HTTP/1.1 HTTP/2 HTTP/2 static content small size objects small # of objects more objects even more larger objects mobile devices Matthew Yohe [CC BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0) or GFDL (http://www.gnu.org/copyleft/fdl.html)], via Wikimedia Commons S SP PD DY Y dynamic content larger size objects more objects RFC 7540
  11. HTTP/2: multiplexing HTTP/2: multiplexing Multiplexing of requests concurrent requests 1

    TCP connection streams: bidirectional frames source: http://www.splicemarketing.co.uk/_blog/Splice_Blog/post/the-arrival-of-http2/#.VlrbuiCrSV4
  12. HTTP/2: HTTP/2: headers compression headers compression Method: GET Scheme: HTTPS

    Accept: image/webp,image/*,*/*;q=0.8 Accept-Language: en-US,en Path: /resource Host: http://www.alpesjug.org/ User-Agent: Mozilla/5.0 Path: /images + HPACK compression + HPACK compression 2nd, 3rd, etc. req 2nd, 3rd, etc. req
  13. HTTP/2: TLS HTTP/2: TLS optional TLS (but for all browsers,

    it's mandatory) TLS 1.2 min no compression no renegociation stronger crypto
  14. <html> <head> <title>AlpesJUG</title> <link rel="stylesheet" href="assets/styles/main-f11f93b619.css"> <link rel="stylesheet" href="assets/styles/theme-564f6756da.css"> <link

    rel="stylesheet" href="assets/styles/ie-d41d8cd98f.css"> </head> <body>...</body> <script src="scripts/vendor-a6d64f3f39.js"></script> <script src="scripts/app-0d43bb3b4b.js"></script> <script src="scripts/theme-439fdb6f87.js"> </html> HTTP/2: Benefits HTTP/2: Benefits <img src="data:image/png;base64,iVBORw0KGgoAAA ...E2AAA7JN+/OU8bK5CYII=" alt="html_inline_alpesjug.png" title="html_inline_alpesjug.png">
  15. HTTP/2: next HTTP/2: next bottleneck? bottleneck? 1001010101011 01100011010 000010101011010 101010101011

    0110100101110010 01010111111110001010 01011101010111 10101010101 00000101010110 101010110101 TCP TCP
  16. Netflix Netflix OSFA API OSFA API optimized for none of

    the devices chatty API (lots of calls)
  17. source: Ben Christensen, Functional reactive programming in the Netflix API

    Netflix Netflix def Observable<Map> getVideos(userId) { return VideoService.getVideos(userId) // we only want the first 10 of each list .take(10) .flatMap({ Video video -> def m = video.getMetaData(); def b = video.getBookmark(); def r = video.getRating(); // compose these together return Observable.zip(m, b, r { metadata, bookmark, rating -> // no transform to complete dictionary // of data we want for each Video return [id: video.videoId] << metadata << bookmark << rating }) }) } The "zip" operator combines the 3 asynchronous Observables into 1
  18. http://netflix.com/genreList/1249?rowOffset=0&rowSize=5&titleprops=name,boxshot http://netflix.com/genreList/1249/setRating?titleId=5&view=movieDetailPage Netflix: problematic Netflix: problematic Rest API Rest API

    http://netflix.com/genreList/1249 "Rest-ish" API "Rest-ish" API http://netflix.com/genreList/1249?titleprops=name,ratings "RPC-ish" API "RPC-ish" API
  19. Netflix: problematic Netflix: problematic « Netflix's domain is a «

    Netflix's domain is a graph problem not a tree graph problem not a tree problem » problem »
  20. Netflix Netflix 㱺 㱺 Middleware Middleware 㱺 㱺 Cache Cache

    㱺 㱺 JSON extension: JSON extension: JSONGraph (Virtual JSON) JSONGraph (Virtual JSON)
  21. Netflix Netflix Cache HTTP D a t a S o

    u r c e https://... /model.json Router DataSource Client View Middleware Recommandation Service Title Service Rating Service Model Express
  22. Netflix Netflix « The data is the API: « The

    data is the API: if you know the data, you if you know the data, you know the API » know the API »
  23. What's wrong with What's wrong with polling? polling? Bob Bob

    Caroll Caroll Alice Alice Erik Erik Pull too fast: waste resources Pull at the wrong moment: get blocked Pull too slow: run behind source: Erik Meijer https://www.youtube.com/watch?v=FvMuPtuvP5w
  24. Real-time UX... i$ Rea£-time Real-time UX... i$ Rea£-time Mon€¥ Mon€¥

    Amazon found every 100 milliseconds of Amazon found every 100 milliseconds of apps latency cost them 1% in sales. apps latency cost them 1% in sales. Source: http://perspectives.mvdirona.com/2009/10/31/TheCostOfLatency.aspx Google found an extra 0.5 seconds in Google found an extra 0.5 seconds in search page generation time dropped search page generation time dropped traffic by 20% traffic by 20% Source: http://highscalability.com/latency-everywhere-and-it-costs-you-sales-how-crush-it
  25. Solutions for dynamic Solutions for dynamic data data Long polling

    Long polling WebSockets WebSockets Server-Sent Events Server-Sent Events
  26. WebSockets WebSockets Server-Sent Server-Sent Events Events TCP low-level protocol HTTP

    upgrade handshake GET /chat HTTP/1.1 Host: example.com:8000 Upgrade: websocket Connection: Upgrade Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ== Sec-WebSocket-Version: 13 GET /stream HTTP/1.1 1 Host: example.com Accept: text/event-stream
  27. WebSockets WebSockets Server-Sent Server-Sent Events Events binary + text binary

    + text native browsers native browsers support support text text ... but polyfills! ... but polyfills!
  28. WebSockets WebSockets Server-Sent Server-Sent Events Events var websocket = new

    WebSocket('ws://websocketserver/echo'); websocket.onopen = function () { ... }; websocket.onmessage = function (e) { ... }; websocket.onerror = function (error) { ... }; var eventSource = new EventSource('http://sseserver/echo'); eventSource.onopen = function () { ... }; eventSource.onmessage = function (e) { ... }; eventSource.onerror = function (error) { ... }; eventSource.addEventListener('foo', function(e) { ... }, false);
  29. "Perfs" "Perfs" Use case: Préchargement de 500 Tweets sur une

    page web (nginx configuré en tant que proxy) Source http://matthiasnehlsen.com/blog/2013/05/01/server-sent-events-vs-websockets/
  30. Streaming vs HTTP Polling Streaming vs HTTP Polling 10 messages

    of 1 byte : 7ms vs 220 ms 10 messages of 1 byte : 7ms vs 220 ms 100 messages of 10 bytes : 57 vs 986 ms 100 messages of 10 bytes : 57 vs 986 ms 1000 messages of 100 bytes : 200 vs 10210 ms 1000 messages of 100 bytes : 200 vs 10210 ms 5000 messages of 1000 bytes : 1,2 sec vs 54 sec 5000 messages of 1000 bytes : 1,2 sec vs 54 sec Source http://blog.arungupta.me/rest-vs-websocket-comparison-benchmarks/​
  31. Support (bis) Support (bis) Some Java Servers Some Java Servers

    Clients libraries in... Clients libraries in...
  32. WebSockets WebSockets Server-Sent Server-Sent Events Events chat, chat, chat !

    ​Share living editing GPS GoogleMap-like Games Use cases Use cases Fintech / Trading Betting Games Realtime timetables Animated data apps (charts, monitoring, etc.)
  33. What if...? What if...? ... I have just a ...

    I have just a JSON API? JSON API?
  34. API Server Load API Server Load polling polling w/ streamdata.io

    w/ streamdata.io 10,000 concurrent clients API Server CPU API Server CPU
  35. JSON-Patch (RFC 6902) JSON-Patch (RFC 6902) [{"title":"Value 0","price":66,"param1":"1","param2":"22","param3":"33"}, {"title":"Value 1","price":63,"param1":"11","param2":"2","param3":"53"},

    {"title":"Value 2","price":85,"param1":"1","param2":"22","param3":"33"}, {"title":"Value 3","price":21,"param1":"31","param2":"12","param3":"4"}, {"title":"Value 4","price":10,"param1":"151","param2":"22","param3":"33"}, {"title":"Value 5","price":6,"param1":"11","param2":"21","param3":"33"}, {"title":"Value 6","price":60,"param1":"11","param2":"222","param3":"33"}] [{"op":"replace","path":"/2/price","value":5}, {"op":"replace","path":"/3/param2","value":"32"}] [{"title":"Value 0","price":66,"param1":"1","param2":"22","param3":"33"}, {"title":"Value 1","price":63,"param1":"11","param2":"2","param3":"53"}, {"title":"Value 2","price":5,"param1":"1","param2":"22","param3":"33"}, {"title":"Value 3","price":21,"param1":"31","param2":"32","param3":"4"}, {"title":"Value 4","price":10,"param1":"151","param2":"22","param3":"33"}, {"title":"Value 5","price":6,"param1":"11","param2":"21","param3":"33"}, {"title":"Value 6","price":60,"param1":"11","param2":"222","param3":"33"}]
  36. Efficiently Turn APIs into Efficiently Turn APIs into Real-Time Experiences

    Real-Time Experiences Push, don't poll Dynamic Cache Incremental Data
  37. Refresh and Waiting Refresh and Waiting Poor UX: low emotional

    engagement Poor UX: low emotional engagement Poor SEO: poor dwell time Poor SEO: poor dwell time
  38. 0.5s is the latency objective “The more others invest in

    amazing UI, the more yours seems lousy” Einstein « Relativity concepts applied to UI », OpenRoadMedia, 1937 Conclusions Conclusions The world is more and more dynamic and we've got the tools Be ready to animate data, become streamers!
  39. References References What is Server-Sent Events? WebSockets vs SSE Getting

    started with WebSockets and SSE Server-Sent Events: The simplest realtime browser spec WebSockets / SSE WebSockets / SSE