Successfully reported this slideshow.
Your SlideShare is downloading. ×

Meet a parallel, asynchronous PHP world

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Loading in …3
×

Check these out next

1 of 203 Ad

More Related Content

Slideshows for you (20)

Viewers also liked (20)

Advertisement

Similar to Meet a parallel, asynchronous PHP world (20)

More from Steve Maraspin (19)

Advertisement

Recently uploaded (20)

Meet a parallel, asynchronous PHP world

  1. 1. PHPDay 2015 – Verona, Italy, May 16th 2015 Meet a Parallel Asynchronous PHP World
  2. 2. STEVE @MARASPIN
  3. 3. http://www.mvlabs.it/
  4. 4. USE CASE
  5. 5. 6
  6. 6. 7
  7. 7. AWESOME PHP MUM
  8. 8. How does it work? 9
  9. 9. We have a client… 10
  10. 10. 11 What do you want for lunch?
  11. 11. 12
  12. 12. 13 Cool. BRB!
  13. 13. PHP Makes I/O Requests… 14
  14. 14. PHP awaits for I/O… 15
  15. 15. I/O Ready 16
  16. 16. Content Delivered 17
  17. 17. DELICIOUS LUNCH!
  18. 18. GOING ENTERPRISE
  19. 19. TROUBLES ARISE
  20. 20. 21
  21. 21. 22
  22. 22. 23 ~20s
  23. 23. WHY SO MUCH TIME?
  24. 24. 25
  25. 25. 26
  26. 26. 27
  27. 27. 28
  28. 28. 29
  29. 29. 30
  30. 30. 31 What do you want for lunch?
  31. 31. 32
  32. 32. 33
  33. 33. 34
  34. 34. 35 What do you want for lunch?
  35. 35. Gotta look for a better strategy… 37
  36. 36. 38 What do you want for lunch?
  37. 37. 39 What do you want for lunch?
  38. 38. 40 What do you want for lunch?
  39. 39. 41
  40. 40. 42
  41. 41. 43
  42. 42. 44
  43. 43. 45
  44. 44. 46
  45. 45. 47
  46. 46. 48
  47. 47. 49 curl_multi_add_handle
  48. 48. 50 curl_multi_exec curl_multi_info_read curl_multi_getcontent
  49. 49. 51 curl_multi_exec curl_multi_info_read curl_multi_getcontent
  50. 50. 52 curl_multi_exec curl_multi_info_read curl_multi_getcontent
  51. 51. 53
  52. 52. 54 ~20s ~2s
  53. 53. 55 Ordered Results
  54. 54. 56 No More!
  55. 55. Solution #1: PHP Asynchronous Calls 57
  56. 56. 58 • Within PHP • Easy to use Pros Cons • Limited set of available commands
  57. 57. VARIETY WANTED
  58. 58. CHEFS TO THE RESCUE
  59. 59. 61
  60. 60. Simple Message Queue
  61. 61. Multiple Consumers / Workers
  62. 62. More Decoupling…
  63. 63. JACK OF ALL TRADES
  64. 64. 66 Useful if already within stack & simple system – please take a look: http://antirez.com/news/88
  65. 65. 67
  66. 66. 68 lPush
  67. 67. 69 brPop
  68. 68. 70
  69. 69. 71
  70. 70. 72 batch_basic_publish / basic_publish
  71. 71. 73 basic_consume
  72. 72. 74
  73. 73. 75 What do you want for lunch?
  74. 74. 76 What do you want for lunch?
  75. 75. 77 What do you want for lunch?
  76. 76. 78
  77. 77. 79
  78. 78. 80
  79. 79. 81
  80. 80. 82 ?
  81. 81. Solution #2: Message Queues 83
  82. 82. 84 • Scales well • Can be made resilient • Decoupling Pros Cons • Extra software components • Lack of Feedback to invoking process
  83. 83. 85
  84. 84. 86 http://gearman.org/
  85. 85. 87 addTaskBackground
  86. 86. 88 addFunction
  87. 87. 89
  88. 88. 90 addTaskBackground
  89. 89. 91 addTask
  90. 90. 92
  91. 91. 93 ~20s
  92. 92. 94 OK, so just prepare …
  93. 93. 95
  94. 94. 96
  95. 95. 97
  96. 96. 98
  97. 97. 99
  98. 98. 100
  99. 99. 101
  100. 100. 102
  101. 101. 103 OK, so just prepare … We need multiple (parallel) workers if we wish to improve performance
  102. 102. Solution #3: Job Server 104
  103. 103. 105 • Scales well • Decoupling • Possibly resilient • Return Values Pros Cons • Extra software components • Extra processes
  104. 104. PHP FAMILY BUSINESS
  105. 105. 107 What do you want for lunch? What do you want for lunch? What do you want for lunch?
  106. 106. 108
  107. 107. 109
  108. 108. 110
  109. 109. 111
  110. 110. 112 Background exec
  111. 111. 113
  112. 112. 114 What do you want for lunch?
  113. 113. 115 ? ? ? Same problem as before…
  114. 114. Solution #4: Exec & Co. 116
  115. 115. 117 • Very easy to implement Pros Cons • No return values • Extra processes
  116. 116. PHP THREADS
  117. 117. pthreads "pthreads requires a build of PHP with ZTS (Zend Thread Safety) enabled" 119
  118. 118. pthreads "pthreads requires a build of PHP with ZTS (Zend Thread Safety) enabled" 120
  119. 119. MUM GOT DAUGHTER(S)
  120. 120. 122
  121. 121. 123 pcntl_fork
  122. 122. 124 pcntl_waitpid
  123. 123. 125
  124. 124. 126 What do you want for lunch? What do you want for lunch?
  125. 125. 127
  126. 126. 128
  127. 127. 129 What do you want for lunch?
  128. 128. Solution #5: Forking processes 130
  129. 129. 131 • Within PHP • Allows for parallelism • Exploits available CPU cores Pros Cons • Feedback (besides exit status) difficult to get • Parallelism bound to # of available CPU cores • Not to be used within Apache or web servers in general
  130. 130. Latency Comparison Numbers L1 cache reference 0.5 ns Branch mispredict 5 ns L2 cache reference 7 ns Mutex lock/unlock 25 ns Main memory reference 100 ns Send 1K bytes over 1 Gbps network 10,000 ns Read 4K randomly from SSD* 150,000 ns Read 1 MB sequentially from memory 250,000 ns Round trip within same datacenter 500,000 ns Read 1 MB sequentially from SSD* 1,000,000 ns Disk seek 10,000,000 ns Read 1 MB sequentially from disk 20,000,000 ns Send packet CA->Netherlands->CA 150,000,000 ns Let's consider these numbers… Source: https://gist.github.com/jboner/2841832
  131. 131. Latency Comparison Numbers L1 cache reference 0.5 ns Branch mispredict 5 ns L2 cache reference 7 ns Mutex lock/unlock 25 ns Main memory reference 100 ns Send 1K bytes over 1 Gbps network 10,000 ns Read 4K randomly from SSD 150,000 ns Read 1 MB sequentially from memory 250,000 ns Round trip within same datacenter 500,000 ns Read 1 MB sequentially from SSD 1,000,000 ns Disk seek 10,000,000 ns Read 1 MB sequentially from disk 20,000,000 ns Send packet CA->Netherlands->CA 150,000,000 ns We got an I/O issue! Source: https://gist.github.com/jboner/2841832
  132. 132. Blocking I/O happens when…  Sending an e-mail  Communicating with Databases  Communicating with other services  Reading/writing data on Disk  Reading/writing data on Network  …
  133. 133. 136 What do you want for lunch?
  134. 134. 137
  135. 135. 138 What do you want for lunch?
  136. 136. 139
  137. 137. 140 What do you want for lunch?
  138. 138. 141
  139. 139. 142
  140. 140. 143
  141. 141. 144
  142. 142. 145
  143. 143. 146
  144. 144. 147
  145. 145. 148
  146. 146. 149
  147. 147. 150
  148. 148. EVENTS
  149. 149. DIY TIMER
  150. 150. I/O Multiplexing 154
  151. 151. PHP stream_select 156 Runs the equivalent of the select() system call on the given arrays of streams with a timeout specified by tv_sec and tv_usec
  152. 152. 157
  153. 153. 158
  154. 154. 159
  155. 155. Solution #6: stream_select 160
  156. 156. 161 • Within PHP • Removes I/O wait overhead Pros Cons • Cumbersome within large projects • Performances could be better
  157. 157. Libevent • API providing a mechanism to execute a callback when a specific event occurs on a file descriptor or after a timeout • supports /dev/poll, kqueue(2), event ports, POSIX select(2), Windows select(), poll(2), and epoll(4) notification mechanisms 162
  158. 158. 163
  159. 159. 164
  160. 160. Solution #7: libevent 165
  161. 161. 166 • Within PHP • More performant than select Pros Cons • Cumbersome within large projects • Might behave differently within different OSs
  162. 162. WHY A LOUSY OVEN?
  163. 163. WHEN WE COULD USE…
  164. 164. A REACTOR!
  165. 165. Reactor Pattern
  166. 166. event loop (event1, callback1) (event2, callback2) on(event1)  (callback1) on(event2)  (callback2)
  167. 167.  Implements reactor pattern  Formerly known as Node.PHP  Non-blocking I/O  Event Loop http://reactphp.org
  168. 168. React libevlibevent inotifyepollpolevent portskqueueselect
  169. 169. 175
  170. 170. 176 Out of loop: Ignored!
  171. 171. 177
  172. 172. HTTP Socket Stream Event Loop
  173. 173. 179
  174. 174. 180 Promise
  175. 175. 181
  176. 176. 182 Streams
  177. 177. 183
  178. 178. 184
  179. 179. 185 https://bugs.php.net/bug.php?id=64696 "epoll" itself (used by Libevent on Linux) doesn't support regular files. Epoll expects whether sockets, or pipes"
  180. 180. 186 Libev uninstalled…
  181. 181. 187
  182. 182. 188
  183. 183. 189
  184. 184. Solution #8: React 190
  185. 185. 191 • Wrapper around many event handlers • Good abstractions Pros Cons • Still in 0.x • New paradigm
  186. 186. SUMMARIZING THINGS
  187. 187. 193 Parallel
  188. 188. 194 Asynchronous
  189. 189. 195
  190. 190. WHAT'S NEXT?
  191. 191. HACK ASYNC http://docs.hhvm.com/manual/en/hack.async.php
  192. 192. Node.JS Galore • https://github.com/chobie/php-uv 198
  193. 193. Node.JS Galore • https://github.com/JosephMoniz/node.php 199
  194. 194. Interesting resources… • Ratchet http://socketo.me/ • Cooperative Multitasking using coroutines https://nikic.github.io/2012/12/22/Cooperative- multitasking-using-coroutines-in-PHP.html • Recoil https://github.com/recoilphp/recoil 200
  195. 195. THANK YOU!
  196. 196. Picture Credits Spider: https://www.flickr.com/photos/janajermakova/10616495683/ Table: https://www.flickr.com/photos/turnstonefurniture/5731379672/ Gnocchi: https://www.flickr.com/photos/cookingetc/6864270056/ Skeptical Dog: https://www.flickr.com/photos/jacobyarborough/13564967155/ Kitchen Accident: https://www.flickr.com/photos/aleksiaaltonen/4803518904/ Variety: https://www.flickr.com/photos/gammaman/6135388116/ Chefs: https://www.flickr.com/photos/lesroches/8700412835/ Man in Kitchen: https://www.flickr.com/photos/paullew/4542993694/ Family: https://www.flickr.com/photos/gareth1953/5173076989/ Organize: https://www.flickr.com/photos/7502393@N04/472028888/ Timer: https://www.flickr.com/photos/lucidscience/5079439604/ Oven: https://www.flickr.com/photos/bonedaddy/2889601088 Presentation: https://www.flickr.com/photos/oecd_development_centre/10168550803/ Reactor: https://www.flickr.com/photos/mandj98/2468396121/ Stirring: https://www.flickr.com/photos/nicoleabalde/7191104180/ Table Ready: https://www.flickr.com/photos/thefalcondale/6940598837/ Other pictures by Steve Maraspin, or from Fotolia Archives 202
  197. 197. Stefano Maraspin @maraspin s.maraspin@mvlabs.it

×