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

Event Driven Architecture

Event Driven Architecture

Have you ever wondered what ReactPHP/NodeJs meant by "non blocking I/O"? Or why would being "event-driven" allow nginx to be more efficient than Apache?

If you find it easier to understand abstract concepts like these by implementing them, then you're in luck because in this talk we'll create a simple HTTP server, then develop an event loop and finally create a simple Promise class.

A78c676a05cd6d653609b237ed515601?s=128

Loïc Faugeron

August 17, 2016
Tweet

Transcript

  1. Event Driven Architecture Understanding Event Loops and the point of

    async Loïc Faugeron http://constant.co @epiloic
  2. Robert C. Martin - https://blog.8thlight.com/uncle-bob/2015/08/06/let-the-magic-die.html “If you’ve ever written a

    web server, then the magic is gone” @epiloic
  3. 1. HTTP Server 2. Event Loop 3. Scheduling 4. Promise

    5. Thread Pool
  4. 1. A story of Input / Output Also known as

    “I/O”
  5. What is “I/O”? Server / Client communication via sockets 5

    @epiloic
  6. Example: HTTP Server 6 @epiloic

  7. Creating a socket 7 @epiloic

  8. 8 @epiloic Caution! When the queue is full... Picture: Nick

    Webb (CC BY 2.0 - https://flic.kr/p/5AykUp)
  9. Looping to infinite and beyond 9 @epiloic

  10. Creating dedicated sockets for clients 10 @epiloic

  11. 11 @epiloic Unqueueing To avoid... Picture: Nick Webb (CC BY

    2.0 - https://flic.kr/p/5AykUp)
  12. Waiting for data to be received… 12 @epiloic

  13. I/O is slow Think about latency See https://gist.github.com/jboner/2841832#file-latency-txt

  14. 14 @epiloic Meanwhile More queueing Picture: Nick Webb (CC BY

    2.0 - https://flic.kr/p/5AykUp)
  15. HTTP application 15 @epiloic

  16. Waiting for data to be sent… 16 @epiloic

  17. I/O is really slow And unreliable See https://gist.github.com/jboner/2841832#file-latency-txt

  18. 18 @epiloic Meantime More queueing Picture: Nick Webb (CC BY

    2.0 - https://flic.kr/p/5AykUp)
  19. Saying goodbye to the client 19 @epiloic

  20. Queue System Handles ~100 clients Picture: lorenz.markus97 (CC BY 2.0

    - https://flic.kr/p/szWiqL)
  21. Handling 10 000 clients? If only we could do something

    else while waiting... 21 @epiloic See the C10K problem: http://www.kegel.com/c10k.html by Dan Kegel
  22. 2. Hang on! ready = poll(to_watch) Did you see what

    I did there?
  23. Many implementations select, poll, epoll, kqueue, IOCP 23 @epiloic See

    epoll VS kqueue: http://www.eecs.berkeley.edu/~sangjin/2012/12/21/epoll-vs-kqueue.html by Sangjin Han
  24. Example: HTTP Server 24 @epiloic

  25. Collecting sockets to watch 25 @epiloic

  26. Waiting for sockets to be ready 26 @epiloic

  27. HTTP server socket is ready 27 @epiloic

  28. Collecting more sockets to watch 28 @epiloic

  29. HTTP client socket is ready 29 @epiloic

  30. No longer watching closed sockets 30 @epiloic

  31. Receiving events in a loop That’s an Event Loop! 31

    @epiloic
  32. Refactoring Event Loop 32 @epiloic

  33. No distinctions between sockets 33 @epiloic

  34. Injecting Event Loop in callbacks 34 @epiloic

  35. Allows decoupled code Put Server / Client logic in callbacks

    35 @epiloic
  36. Event Loop usage 36 @epiloic

  37. Adding server socket to Event Loop 37 @epiloic

  38. Handling new client connections 38 @epiloic

  39. Handling received data 39 @epiloic

  40. Efficient Queue Using blocking time Picture: Sean McEntee (CC BY

    2.0 - https://flic.kr/p/a7zKqg)
  41. “Blocking, If it’s not solving all your problems, You simply

    aren’t using enough of it.” 41 @epiloic
  42. Handling 0 clients? If only we could do something else

    while waiting... 42 @epiloic
  43. 3. Wait a second! poll(to_watch, timeout) Did you see what

    I did there?
  44. Schedulers One off, periodic, idle, etc... 44 @epiloic

  45. Example: One off Scheduler 45 @epiloic

  46. Adding timer callbacks 46 @epiloic

  47. Checking if any timers are due 47 @epiloic

  48. Calling due timers’ callbacks 48 @epiloic

  49. Removing one off timers afterwards 49 @epiloic

  50. Scheduled Event Loop 50 @epiloic

  51. Setting a smart timeout 51 @epiloic

  52. Ticking the clock 52 @epiloic

  53. Efficient Queue Using waiting time Picture: fdecomite (CC BY 2.0

    - https://flic.kr/p/BW7Hh)
  54. “Waiting, If it’s not solving all your problems, You simply

    aren’t using enough of it.” 54 @epiloic
  55. Nesting more callbacks? If only we could make async code

    feel more sync... 55 @epiloic
  56. 4. Async what you did there... No more puns, I

    promise
  57. Futures and promises Async functions return a value, register callbacks

    via this value 57 @epiloic
  58. Example: Deferrer / Promise 58 @epiloic

  59. Injecting Deferrer’s setter in Promise 59 @epiloic

  60. Passing callback to Promise... 60 @epiloic

  61. … Which sets it in the Deferrer 61 @epiloic

  62. Resolving the Deferrer, calls callbacks 62 @epiloic

  63. Chaining calls To make it feel sync Picture: Mark Skipper

    (CC BY 2.0 - https://flic.kr/p/4FQ8DU)
  64. “Callback, If it’s not solving all your problems, You simply

    aren’t using enough of it.” 64 @epiloic
  65. Usage example? If only we could have a usage example...

    65 @epiloic
  66. 5. Filesystem U/O More U/O (Uh Oh) than I/O

  67. Slowest I/O? Filesystem See https://gist.github.com/jboner/2841832#file-latency-txt

  68. Poor non-blocking support Cannot be used with poll() 68 @epiloic

    See asynchronous disk I/O: http://blog.libtorrent.org/2012/10/asynchronous-disk-io/ by Arvid Norberg
  69. Solution? Use the force a Thread Pool 69 @epiloic

  70. Non Blocking Filesystem 70 @epiloic

  71. Deferring FS call to Thread Pool 71 @epiloic

  72. Calling callbacks, once thread is done 72 @epiloic

  73. Blocking I/O Wrapped, in Threads Picture: Jamie (CC BY 2.0

    - https://flic.kr/p/9Av7cj)
  74. “Threading, If it’s not solving all your problems, You simply

    aren’t using enough of it.” 74 @epiloic
  75. “Threading, If it’s not solving all your problems, You simply

    aren’t using enough of it.” 75 @epiloic NOT! Limits are good
  76. 76 @epiloic Infinite pool Error cause: Filesystem limit Picture: Nick

    Webb (CC BY 2.0 - https://flic.kr/p/5AykUp)
  77. 77 @epiloic Infinite limit Error cause: HDD/SSD burned Picture: Nick

    Webb (CC BY 2.0 - https://flic.kr/p/5AykUp)
  78. True story Usually pool of 4 threads 78 @epiloic See

    ReactPHP filesystem - a word of caution: https://blog.wyrihaximus.net/2015/03/reactphp-filesystem/#a-word-of-caution by Cees-Jan Kiewiet
  79. ConclusI/On

  80. Making use of waiting time, to handle more clients 80

    @epiloic Non blocking Idle Sync Feeling Blocking Event Loop Scheduling Promise Thread Pool
  81. 81 @epiloic Used by... nginx See how it outperforms Apache:

    https://www.nginx.com/blog/nginx-vs-apache-our-view/ Picture: Thijs Feryn (https://speakerdeck.com/thijsferyn/varnish-or-nginx-symfony-live)
  82. 82 @epiloic Used by... Libuv (Node.js) Node.js is a trademark

    of Joyent, Inc. and is used with its permission. We are not endorsed by or affiliated with Joyent. See an Introduction to libuv: https://nikhilm.github.io/uvbook/introduction.html by Nikhil Marathe
  83. 83 @epiloic Used by… You?

  84. Thank you 84 @epiloic Transcript of this presentation, in a

    blog article: https://gnugat.github.io/2016/04/27/event-driven-architecture.html