meet a parallel, asynchronous php world

203
PHPDay 2015 – Verona, Italy, May 16 th 2015 Meet a Parallel Asynchronous PHP World

Upload: stefano-maraspin

Post on 28-Jul-2015

462 views

Category:

Software


3 download

TRANSCRIPT

Page 1: Meet a parallel, asynchronous PHP world

PHPDay 2015 – Verona, Italy, May 16th 2015

Meet a Parallel Asynchronous PHP World

Page 2: Meet a parallel, asynchronous PHP world

STEVE @MARASPIN

Page 3: Meet a parallel, asynchronous PHP world
Page 4: Meet a parallel, asynchronous PHP world

http://www.mvlabs.it/

Page 5: Meet a parallel, asynchronous PHP world

USE CASE

Page 6: Meet a parallel, asynchronous PHP world

6

Page 7: Meet a parallel, asynchronous PHP world

7

Page 8: Meet a parallel, asynchronous PHP world

AWESOME PHP MUM

Page 9: Meet a parallel, asynchronous PHP world

How does it work?

9

Page 10: Meet a parallel, asynchronous PHP world

We have a client…

10

Page 11: Meet a parallel, asynchronous PHP world

11

What do you want for lunch?

Page 12: Meet a parallel, asynchronous PHP world

12

Page 13: Meet a parallel, asynchronous PHP world

13

Cool. BRB!

Page 14: Meet a parallel, asynchronous PHP world

PHP Makes I/O Requests…

14

Page 15: Meet a parallel, asynchronous PHP world

PHP awaits for I/O…

15

Page 16: Meet a parallel, asynchronous PHP world

I/O Ready

16

Page 17: Meet a parallel, asynchronous PHP world

Content Delivered

17

Page 18: Meet a parallel, asynchronous PHP world

DELICIOUS LUNCH!

Page 19: Meet a parallel, asynchronous PHP world

GOING ENTERPRISE

Page 20: Meet a parallel, asynchronous PHP world

TROUBLES ARISE

Page 21: Meet a parallel, asynchronous PHP world

21

Page 22: Meet a parallel, asynchronous PHP world

22

Page 23: Meet a parallel, asynchronous PHP world

23

~20s

Page 24: Meet a parallel, asynchronous PHP world

WHY SO MUCH TIME?

Page 25: Meet a parallel, asynchronous PHP world

25

Page 26: Meet a parallel, asynchronous PHP world

26

Page 27: Meet a parallel, asynchronous PHP world

27

Page 28: Meet a parallel, asynchronous PHP world

28

Page 29: Meet a parallel, asynchronous PHP world

29

Page 30: Meet a parallel, asynchronous PHP world

30

Page 31: Meet a parallel, asynchronous PHP world

31

What do you want for lunch?

Page 32: Meet a parallel, asynchronous PHP world

32

Page 33: Meet a parallel, asynchronous PHP world

33

Page 34: Meet a parallel, asynchronous PHP world

34

Page 35: Meet a parallel, asynchronous PHP world

35

What do you want for lunch?

Page 36: Meet a parallel, asynchronous PHP world
Page 37: Meet a parallel, asynchronous PHP world

Gotta look for a better strategy…

37

Page 38: Meet a parallel, asynchronous PHP world

38

What do you want for lunch?

Page 39: Meet a parallel, asynchronous PHP world

39

What do you want for lunch?

Page 40: Meet a parallel, asynchronous PHP world

40

What do you want for lunch?

Page 41: Meet a parallel, asynchronous PHP world

41

Page 42: Meet a parallel, asynchronous PHP world

42

Page 43: Meet a parallel, asynchronous PHP world

43

Page 44: Meet a parallel, asynchronous PHP world

44

Page 45: Meet a parallel, asynchronous PHP world

45

Page 46: Meet a parallel, asynchronous PHP world

46

Page 47: Meet a parallel, asynchronous PHP world

47

Page 48: Meet a parallel, asynchronous PHP world

48

Page 49: Meet a parallel, asynchronous PHP world

49

curl_multi_add_handle

Page 50: Meet a parallel, asynchronous PHP world

50

curl_multi_exec

curl_multi_info_read

curl_multi_getcontent

Page 51: Meet a parallel, asynchronous PHP world

51

curl_multi_exec

curl_multi_info_read

curl_multi_getcontent

Page 52: Meet a parallel, asynchronous PHP world

52

curl_multi_exec

curl_multi_info_read

curl_multi_getcontent

Page 53: Meet a parallel, asynchronous PHP world

53

Page 54: Meet a parallel, asynchronous PHP world

54

~20s ~2s

Page 55: Meet a parallel, asynchronous PHP world

55

Ordered Results

Page 56: Meet a parallel, asynchronous PHP world

56

No More!

Page 57: Meet a parallel, asynchronous PHP world

Solution #1: PHP Asynchronous Calls

57

Page 58: Meet a parallel, asynchronous PHP world

58

• Within PHP • Easy to use

Pros Cons

• Limited set of available commands

Page 59: Meet a parallel, asynchronous PHP world

VARIETY WANTED

Page 60: Meet a parallel, asynchronous PHP world

CHEFS TO THE RESCUE

Page 61: Meet a parallel, asynchronous PHP world

61

Page 62: Meet a parallel, asynchronous PHP world

Simple Message Queue

Page 63: Meet a parallel, asynchronous PHP world

Multiple Consumers / Workers

Page 64: Meet a parallel, asynchronous PHP world

More Decoupling…

Page 65: Meet a parallel, asynchronous PHP world

JACK OF ALL TRADES

Page 66: Meet a parallel, asynchronous PHP world

66

Useful if already within stack & simple system – please take a look: http://antirez.com/news/88

Page 67: Meet a parallel, asynchronous PHP world

67

Page 68: Meet a parallel, asynchronous PHP world

68

lPush

Page 69: Meet a parallel, asynchronous PHP world

69

brPop

Page 70: Meet a parallel, asynchronous PHP world

70

Page 71: Meet a parallel, asynchronous PHP world

71

Page 72: Meet a parallel, asynchronous PHP world

72

batch_basic_publish / basic_publish

Page 73: Meet a parallel, asynchronous PHP world

73

basic_consume

Page 74: Meet a parallel, asynchronous PHP world

74

Page 75: Meet a parallel, asynchronous PHP world

75

What do you want for lunch?

Page 76: Meet a parallel, asynchronous PHP world

76

What do you want for lunch?

Page 77: Meet a parallel, asynchronous PHP world

77

What do you want for lunch?

Page 78: Meet a parallel, asynchronous PHP world

78

Page 79: Meet a parallel, asynchronous PHP world

79

Page 80: Meet a parallel, asynchronous PHP world

80

Page 81: Meet a parallel, asynchronous PHP world

81

Page 82: Meet a parallel, asynchronous PHP world

82

?

Page 83: Meet a parallel, asynchronous PHP world

Solution #2: Message Queues

83

Page 84: Meet a parallel, asynchronous PHP world

84

• Scales well • Can be made

resilient • Decoupling

Pros Cons

• Extra software components

• Lack of Feedback to invoking process

Page 85: Meet a parallel, asynchronous PHP world

85

Page 86: Meet a parallel, asynchronous PHP world

86

http://gearman.org/

Page 87: Meet a parallel, asynchronous PHP world

87

addTaskBackground

Page 88: Meet a parallel, asynchronous PHP world

88

addFunction

Page 89: Meet a parallel, asynchronous PHP world

89

Page 90: Meet a parallel, asynchronous PHP world

90

addTaskBackground

Page 91: Meet a parallel, asynchronous PHP world

91

addTask

Page 92: Meet a parallel, asynchronous PHP world

92

Page 93: Meet a parallel, asynchronous PHP world

93

~20s

Page 94: Meet a parallel, asynchronous PHP world

94

OK, so just prepare …

Page 95: Meet a parallel, asynchronous PHP world

95

Page 96: Meet a parallel, asynchronous PHP world

96

Page 97: Meet a parallel, asynchronous PHP world

97

Page 98: Meet a parallel, asynchronous PHP world

98

Page 99: Meet a parallel, asynchronous PHP world

99

Page 100: Meet a parallel, asynchronous PHP world

100

Page 101: Meet a parallel, asynchronous PHP world

101

Page 102: Meet a parallel, asynchronous PHP world

102

Page 103: Meet a parallel, asynchronous PHP world

103

OK, so just prepare …

We need multiple (parallel) workers if we wish to improve performance

Page 104: Meet a parallel, asynchronous PHP world

Solution #3: Job Server

104

Page 105: Meet a parallel, asynchronous PHP world

105

• Scales well • Decoupling • Possibly

resilient • Return Values

Pros Cons

• Extra software components

• Extra processes

Page 106: Meet a parallel, asynchronous PHP world

PHP FAMILY BUSINESS

Page 107: Meet a parallel, asynchronous PHP world

107

What do you want for lunch?

What do you want for lunch?

What do you want for lunch?

Page 108: Meet a parallel, asynchronous PHP world

108

Page 109: Meet a parallel, asynchronous PHP world

109

Page 110: Meet a parallel, asynchronous PHP world

110

Page 111: Meet a parallel, asynchronous PHP world

111

Page 112: Meet a parallel, asynchronous PHP world

112

Background exec

Page 113: Meet a parallel, asynchronous PHP world

113

Page 114: Meet a parallel, asynchronous PHP world

114

What do you want for lunch?

Page 115: Meet a parallel, asynchronous PHP world

115

? ? ?

Same problem as before…

Page 116: Meet a parallel, asynchronous PHP world

Solution #4: Exec & Co.

116

Page 117: Meet a parallel, asynchronous PHP world

117

• Very easy to implement

Pros Cons

• No return values

• Extra processes

Page 118: Meet a parallel, asynchronous PHP world

PHP THREADS

Page 119: Meet a parallel, asynchronous PHP world

pthreads

"pthreads requires a build of PHP with ZTS (Zend Thread Safety) enabled"

119

Page 120: Meet a parallel, asynchronous PHP world

pthreads

"pthreads requires a build of PHP with ZTS (Zend Thread Safety) enabled"

120

Page 121: Meet a parallel, asynchronous PHP world

MUM GOT DAUGHTER(S)

Page 122: Meet a parallel, asynchronous PHP world

122

Page 123: Meet a parallel, asynchronous PHP world

123

pcntl_fork

Page 124: Meet a parallel, asynchronous PHP world

124

pcntl_waitpid

Page 125: Meet a parallel, asynchronous PHP world

125

Page 126: Meet a parallel, asynchronous PHP world

126

What do you want for lunch?

What do you want for lunch?

Page 127: Meet a parallel, asynchronous PHP world

127

Page 128: Meet a parallel, asynchronous PHP world

128

Page 129: Meet a parallel, asynchronous PHP world

129

What do you want for lunch?

Page 130: Meet a parallel, asynchronous PHP world

Solution #5: Forking processes

130

Page 131: Meet a parallel, asynchronous PHP world

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

Page 132: Meet a parallel, asynchronous PHP world

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

Page 133: Meet a parallel, asynchronous PHP world

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

Page 134: Meet a parallel, asynchronous PHP world

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 …

Page 135: Meet a parallel, asynchronous PHP world
Page 136: Meet a parallel, asynchronous PHP world

136

What do you want for lunch?

Page 137: Meet a parallel, asynchronous PHP world

137

Page 138: Meet a parallel, asynchronous PHP world

138

What do you want for lunch?

Page 139: Meet a parallel, asynchronous PHP world

139

Page 140: Meet a parallel, asynchronous PHP world

140

What do you want for lunch?

Page 141: Meet a parallel, asynchronous PHP world

141

Page 142: Meet a parallel, asynchronous PHP world

142

Page 143: Meet a parallel, asynchronous PHP world

143

Page 144: Meet a parallel, asynchronous PHP world

144

Page 145: Meet a parallel, asynchronous PHP world

145

Page 146: Meet a parallel, asynchronous PHP world

146

Page 147: Meet a parallel, asynchronous PHP world

147

Page 148: Meet a parallel, asynchronous PHP world

148

Page 149: Meet a parallel, asynchronous PHP world

149

Page 150: Meet a parallel, asynchronous PHP world

150

Page 151: Meet a parallel, asynchronous PHP world

EVENTS

Page 152: Meet a parallel, asynchronous PHP world
Page 153: Meet a parallel, asynchronous PHP world

DIY TIMER

Page 154: Meet a parallel, asynchronous PHP world

I/O Multiplexing

154

Page 155: Meet a parallel, asynchronous PHP world
Page 156: Meet a parallel, asynchronous PHP world

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

Page 157: Meet a parallel, asynchronous PHP world

157

Page 158: Meet a parallel, asynchronous PHP world

158

Page 159: Meet a parallel, asynchronous PHP world

159

Page 160: Meet a parallel, asynchronous PHP world

Solution #6: stream_select

160

Page 161: Meet a parallel, asynchronous PHP world

161

• Within PHP • Removes I/O

wait overhead

Pros Cons

• Cumbersome within large projects

• Performances could be better

Page 163: Meet a parallel, asynchronous PHP world

163

Page 164: Meet a parallel, asynchronous PHP world

164

Page 165: Meet a parallel, asynchronous PHP world

Solution #7: libevent

165

Page 166: Meet a parallel, asynchronous PHP world

166

• Within PHP • More

performant than select

Pros Cons

• Cumbersome within large projects

• Might behave differently within different OSs

Page 167: Meet a parallel, asynchronous PHP world

WHY A LOUSY OVEN?

Page 168: Meet a parallel, asynchronous PHP world

WHEN WE COULD USE…

Page 169: Meet a parallel, asynchronous PHP world

A REACTOR!

Page 170: Meet a parallel, asynchronous PHP world

Reactor Pattern

Page 171: Meet a parallel, asynchronous PHP world
Page 172: Meet a parallel, asynchronous PHP world

event loop

(event1, callback1)

(event2, callback2)

on(event1) (callback1)

on(event2) (callback2)

Page 173: Meet a parallel, asynchronous PHP world

Implements reactor pattern Formerly known as Node.PHP Non-blocking I/O Event Loop

http://reactphp.org

Page 174: Meet a parallel, asynchronous PHP world

React

libev libevent

inotify epoll pol event ports kqueue select

Page 175: Meet a parallel, asynchronous PHP world

175

Page 176: Meet a parallel, asynchronous PHP world

176

Out of loop: Ignored!

Page 177: Meet a parallel, asynchronous PHP world

177

Page 178: Meet a parallel, asynchronous PHP world

HTTP

Socket

Stream

Event Loop

Page 179: Meet a parallel, asynchronous PHP world

179

Page 180: Meet a parallel, asynchronous PHP world

180

Promise

Page 181: Meet a parallel, asynchronous PHP world

181

Page 182: Meet a parallel, asynchronous PHP world

182

Streams

Page 183: Meet a parallel, asynchronous PHP world

183

Page 184: Meet a parallel, asynchronous PHP world

184

Page 185: Meet a parallel, asynchronous PHP world

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"

Page 186: Meet a parallel, asynchronous PHP world

186

Libev uninstalled…

Page 187: Meet a parallel, asynchronous PHP world

187

Page 188: Meet a parallel, asynchronous PHP world

188

Page 189: Meet a parallel, asynchronous PHP world

189

Page 190: Meet a parallel, asynchronous PHP world

Solution #8: React

190

Page 191: Meet a parallel, asynchronous PHP world

191

• Wrapper around many event handlers

• Good abstractions

Pros Cons

• Still in 0.x • New

paradigm

Page 192: Meet a parallel, asynchronous PHP world

SUMMARIZING THINGS

Page 193: Meet a parallel, asynchronous PHP world

193

Parallel

Page 194: Meet a parallel, asynchronous PHP world

194

Asynchronous

Page 195: Meet a parallel, asynchronous PHP world

195

Page 196: Meet a parallel, asynchronous PHP world

WHAT'S NEXT?

Page 197: Meet a parallel, asynchronous PHP world

HACK ASYNC

http://docs.hhvm.com/manual/en/hack.async.php

Page 198: Meet a parallel, asynchronous PHP world

Node.JS Galore

• https://github.com/chobie/php-uv

198

Page 199: Meet a parallel, asynchronous PHP world

Node.JS Galore

• https://github.com/JosephMoniz/node.php 199

Page 200: Meet a parallel, asynchronous PHP world

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

Page 201: Meet a parallel, asynchronous PHP world

THANK YOU!

Page 202: Meet a parallel, asynchronous PHP world

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

Page 203: Meet a parallel, asynchronous PHP world

Stefano Maraspin @maraspin [email protected]