Slide 1

Slide 1 text

HHVM at Etsy Harder, Better, Faster, Stronger Dan Miller Core Platform Engineer Etsy

Slide 2

Slide 2 text

@jazzdan http://bit.ly/hhvm_etsy

Slide 3

Slide 3 text

The World’s Handmade Marketplace

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

Time Deploys About 60 deploys per day

Slide 6

Slide 6 text

@jazzdan Overview • What is HHVM? • Why were we interested? • How did we migrate? What problems did we encounter? • What else can it do? • The Future

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

2009 2015 ‘14 ‘13 ‘12 ‘11 ‘10

Slide 9

Slide 9 text

2009 2015 ‘14 ‘13 ‘12 ‘11 ‘10 • Linux • Apache • MySQL • PHP

Slide 10

Slide 10 text

2009 2015 ‘14 ‘13 ‘12 ‘11 ‘10 • Linux • Apache • MySQL • PHP • Couldn’t keep up with traffic growth

Slide 11

Slide 11 text

2009 2015 ‘14 ‘13 ‘12 ‘11 ‘10

Slide 12

Slide 12 text

2009 2015 ‘14 ‘13 ‘12 ‘11 ‘10 • HipHop (HPHP) • Compile PHP to C++ • Deploy binary • Separate development environment

Slide 13

Slide 13 text

@jazzdan HipHop Virtual Machine (HHVM)

Slide 14

Slide 14 text

@jazzdan HHVM is not a source transformer (That was HPHPc)

Slide 15

Slide 15 text

Webserver PHP Backend Services FastCGI index.php Logger.php Tpl.php

Slide 16

Slide 16 text

Webserver HHVM Backend Services FastCGI index.php Logger.php Tpl.php

Slide 17

Slide 17 text

2009 2015 ‘14 ‘13 ‘12 ‘11 ‘10

Slide 18

Slide 18 text

@jazzdan HHVM is Open Source • Internal diffs developed in the open • Included in Linux distros • Over 2000 bugs opened and closed • Over 1000 pull requests accepted

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

@jazzdan HHVM is Open Source 1. google.com 2. facebook.com 3. youtube.com 4. yahoo.com 5. baidu.com 6. amazon.com 7. wikipedia.org 8. twitter.com 9. taobao.com 10. qq.com

Slide 23

Slide 23 text

@jazzdan HHVM is Open Source 1. google.com 2. facebook.com 3. youtube.com 4. yahoo.com 5. baidu.com 6. amazon.com 7. wikipedia.org 8. twitter.com 9. taobao.com 10. qq.com

Slide 24

Slide 24 text

@jazzdan HHVM is Compatible* with PHP • 60% of PHP unit tests fail • Missing Extensions • Different error message output • 20 of the top PHP projects on GitHub do pass • 97% of unit tests pass among top 50 projects on GitHub

Slide 25

Slide 25 text

@jazzdan HHVM is Compatible* with PHP • 99% of Etsy unit tests pass • (20 suite failures/1,798 test suites) Fail 20 Pass 1,798

Slide 26

Slide 26 text

@jazzdan HHVM is Faster

Slide 27

Slide 27 text

@jazzdan “Between 3x-6x Faster”

Slide 28

Slide 28 text

@jazzdan (for Facebook)

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

Why?

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

Model Controller API Business Logic A Business Logic B Business Logic A

Slide 33

Slide 33 text

Model API v3 Business Logic

Slide 34

Slide 34 text

@jazzdan “Bespoke” Endpoints • Specific to a view • Aggregate REST endpoints concurrently • Return bespoke response

Slide 35

Slide 35 text

@jazzdan Bespoke Endpoints etsy.com/listing/124740565 Bespoke Recent Shop Overview Related Seller Listing

Slide 36

Slide 36 text

@jazzdan Bespoke Endpoints etsy.com/listing/124740565 Bespoke Recent Shop Overview Related Seller Listing

Slide 37

Slide 37 text

@jazzdan Bespoke Endpoints etsy.com/listing/124740565 Bespoke Recent Shop Overview Related Seller Listing

Slide 38

Slide 38 text

@jazzdan Overview Related Listing Bespoke Endpoints etsy.com/listing/124740565 Bespoke Recent Shop Seller

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

@jazzdan curl_multi_*

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

@jazzdan Bespoke Endpoints etsy.com/listing/124740565 Bespoke Recent Shop Overview Related Seller Listing

Slide 44

Slide 44 text

API Traffic Web Traffic

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

@jazzdan Lower latency = more RPS/box

Slide 48

Slide 48 text

@jazzdan More RPS/box = Fewer boxes

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

@jazzdan Fewer boxes = less $/datacenter

Slide 51

Slide 51 text

@jazzdan Fewer boxes =

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

How?

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

$scp  -­‐r  rlerdorf.vms.etsy.com:/home/rlerdorf/hhvm-­‐root  .

Slide 57

Slide 57 text

@jazzdan Missing memcached constants?

Slide 58

Slide 58 text

@jazzdan /*Missing memcached constants*/

Slide 59

Slide 59 text

@jazzdan Missing geoip extension?

Slide 60

Slide 60 text

@jazzdan /*Missing geoip extension?*/

Slide 61

Slide 61 text

@jazzdan Missing msgpack extension?

Slide 62

Slide 62 text

@jazzdan Login doesn’t need to work for now

Slide 63

Slide 63 text

@jazzdan Etc

Slide 64

Slide 64 text

@jazzdan Ran some benchmarks

Slide 65

Slide 65 text

@jazzdan It’s faster

Slide 66

Slide 66 text

@jazzdan Way faster.

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

No content

Slide 69

Slide 69 text

Happiness and Performance Correlation Time Response Time Happiness

Slide 70

Slide 70 text

Happiness and Performance Correlation Time Response Time Happiness

Slide 71

Slide 71 text

@jazzdan Time to Compile HHVM

Slide 72

Slide 72 text

@jazzdan How hard can it be?

Slide 73

Slide 73 text

@jazzdan September 2nd, 2014: “Started compiling HHVM”

Slide 74

Slide 74 text

@jazzdan September 23rd, 2014: “strace’d cmake”

Slide 75

Slide 75 text

Happiness and Performance Correlation Time Response Time Happiness

Slide 76

Slide 76 text

Happiness and Performance Correlation Time Response Time Happiness

Slide 77

Slide 77 text

@jazzdan ( $git clean -fdx )

Slide 78

Slide 78 text

@jazzdan September 24th, 2014: “GOT IT TO COMPILE”

Slide 79

Slide 79 text

@jazzdan September 25th, 2014: “BUILT AN RPM”

Slide 80

Slide 80 text

Happiness and Performance Correlation Time Response Time Happiness

Slide 81

Slide 81 text

Happiness and Performance Correlation Time Response Time Happiness

Slide 82

Slide 82 text

libs=$(ldd  "${hhvm_binary}"  |  awk  '{print  $3}'  |  grep  -­‐v  '^$'

Slide 83

Slide 83 text

@jazzdan September 30th, 2014: “Fix the HHVM rpm we made last week that broke yum on every prod box”

Slide 84

Slide 84 text

6

Slide 85

Slide 85 text

@jazzdan Had to Upgrade • gcc • libmcrypt • gmp • mpfr • mpc • glog • jemalloc • tbb • libdwarf • libmemcached • libc • cmake • libcurl • more

Slide 86

Slide 86 text

The Test

Slide 87

Slide 87 text

No content

Slide 88

Slide 88 text

Idea Code Release Time

Slide 89

Slide 89 text

Idea Code Release Idea Code Release A/B Test

Slide 90

Slide 90 text

Idea Code Release Idea Code A/B Test Release “Oh crap…” <- Wasted effort (maybe)

Slide 91

Slide 91 text

Idea Code Release Idea Code A/B Test Release Idea Validate Prototype A/B Test Refinement A/B Test Release

Slide 92

Slide 92 text

Idea Validate Prototype A/B Test Refinement A/B Test Release <- Possibly quite crappy

Slide 93

Slide 93 text

Idea Validate Prototype A/B Test Refinement A/B Test Release <- Possibly quite crappy <- Make it less crappy here

Slide 94

Slide 94 text

Idea Validate Prototype A/B Test Refinement A/B Test Release Can HHVM run etsy.com?

Slide 95

Slide 95 text

Idea Validate Prototype A/B Test Refinement A/B Test Release Can HHVM run etsy.com? Is it faster?

Slide 96

Slide 96 text

Scoping

Slide 97

Slide 97 text

API Traffic Web Traffic

Slide 98

Slide 98 text

Run synthetic benchmarks

Slide 99

Slide 99 text

Response Time as Load Increases Response Time 0 1000 2000 3000 4000 Requests per Second 10 30 50 70 90 110 130 150 170 190 210 230 250 270 HHVM PHP 5.4

Slide 100

Slide 100 text

Happiness and Performance Correlation Time Response Time Happiness

Slide 101

Slide 101 text

Happiness and Performance Correlation Time Response Time Happiness

Slide 102

Slide 102 text

Run an experiment

Slide 103

Slide 103 text

Idea Validate Prototype A/B Test Refinement A/B Test Release Can HHVM run Etsy’s internal API? Is it faster? Time to fix the problems we skipped

Slide 104

Slide 104 text

@jazzdan How do we gain more confidence?

Slide 105

Slide 105 text

@jazzdan … and also validate our hypothesis?

Slide 106

Slide 106 text

Tee Traffic

Slide 107

Slide 107 text

Load Balancer API API-HHVM API-TEST

Slide 108

Slide 108 text

Load Balancer API API-HHVM API-TEST

Slide 109

Slide 109 text

Load Balancer API API-HHVM API-TEST

Slide 110

Slide 110 text

@jazzdan Infrastructure experiments are hard

Slide 111

Slide 111 text

@jazzdan Same hardware

Slide 112

Slide 112 text

@jazzdan Same traffic profile

Slide 113

Slide 113 text

@jazzdan Same hacks

Slide 114

Slide 114 text

@jazzdan Both Machines • Read Only MySQL Interface • Read Only memcached Interface • Read Only Redis interface • iptables blocking almost all the things • No log forwarding

Slide 115

Slide 115 text

Happiness and Performance Correlation Time Response Time Happiness

Slide 116

Slide 116 text

Happiness and Performance Correlation Time Response Time Happiness

Slide 117

Slide 117 text

HHVM CPU 14 hour Zend CPU 14 hour 140 rps peak 140 rps peak

Slide 118

Slide 118 text

@jazzdan “Between 3x-6x Faster”

Slide 119

Slide 119 text

#hhvm Repo Authoritative - Extra 20% • Produce bytecode SQLite database in advance • Build include map • Statically resolve file paths • Do non-type related optimizations at compile time

Slide 120

Slide 120 text

Time Deploys About 60 deploys per day

Slide 121

Slide 121 text

@jazzdan What about writing data?

Slide 122

Slide 122 text

Employee Only Traffic

Slide 123

Slide 123 text

@jazzdan memcached

Slide 124

Slide 124 text

Memcached operation failed (returned false) when decrementing KEY

Slide 125

Slide 125 text

Happiness and Performance Correlation Time Response Time Happiness

Slide 126

Slide 126 text

Happiness and Performance Correlation Time Response Time Happiness

Slide 127

Slide 127 text

No content

Slide 128

Slide 128 text

@jazzdan All get()s were returning false

Slide 129

Slide 129 text

Happiness and Performance Correlation Time Response Time Happiness

Slide 130

Slide 130 text

Happiness and Performance Correlation Time Response Time Happiness

Slide 131

Slide 131 text

No content

Slide 132

Slide 132 text

@jazzdan Takeaway:

Slide 133

Slide 133 text

@jazzdan HHVM is rock solid

Slide 134

Slide 134 text

@jazzdan Extensions sometimes have bugs

Slide 135

Slide 135 text

Slow Ramp Up

Slide 136

Slide 136 text

[23/janv./2015:22:40:32 +0000]

Slide 137

Slide 137 text

[23/ 1⽉月/2015:23:37:56]

Slide 138

Slide 138 text

request 1 setlocale(“a”) request 2 setlocale(“b”) strftime() strftime() Time

Slide 139

Slide 139 text

@jazzdan Solution: newlocale()/uselocale()

Slide 140

Slide 140 text

@jazzdan Takeaway:

Slide 141

Slide 141 text

@jazzdan HHVM is threaded

Slide 142

Slide 142 text

Release!

Slide 143

Slide 143 text

No content

Slide 144

Slide 144 text

HHVM Average Response Time 12 Hour Zend Average Response Time 12 Hour

Slide 145

Slide 145 text

HHVM p95 Response Time 12 Hour Zend p95 Response Time 12 Hour

Slide 146

Slide 146 text

HHVM vs PHP 5.5 on Etsy Internal API Median p95 p99 Response Time in Milliseconds 0 200 400 600 800 HHVM PHP

Slide 147

Slide 147 text

What else can HHVM do?

Slide 148

Slide 148 text

@jazzdan Flame Graphs

Slide 149

Slide 149 text

No content

Slide 150

Slide 150 text

@jazzdan HHVM Debugger

Slide 151

Slide 151 text

No content

Slide 152

Slide 152 text

@jazzdan pfff

Slide 153

Slide 153 text

@jazzdan sgrep

Slide 154

Slide 154 text

#hhvm sgrep: Problem Find all invocations of foo() where the second argument is 1, with any number of arguments after

Slide 155

Slide 155 text

#hhvm sgrep: Solution $ sgrep -e 'foo(X, 1, ...)' *.php

Slide 156

Slide 156 text

#hhvm sgrep: Problem Find all && where both operands are the same

Slide 157

Slide 157 text

#hhvm sgrep: Solution $ sgrep -e 'X && X' *.php

Slide 158

Slide 158 text

#hhvm sgrep: Solution $ sgrep -pvar X -e 'X && X' *.php

Slide 159

Slide 159 text

#hhvm sgrep: Problem Find all calls to foo() where the first argument is 1

Slide 160

Slide 160 text

#hhvm sgrep: Solution $ sgrep -e 'foo(1, ...)' *.php

Slide 161

Slide 161 text

#hhvm sgrep: Problem Find all method calls “addPreparable()” with any number of arguments

Slide 162

Slide 162 text

#hhvm sgrep: Solution $ sgrep -e 'X->addPreparable(...)'

Slide 163

Slide 163 text

@jazzdan spatch

Slide 164

Slide 164 text

#hhvm spatch: Problem Remove the second argument from all invocations of function foo()

Slide 165

Slide 165 text

#hhvm spatch: Solution //remove_second_arg_foo.spatch foo(X - ,Y )

Slide 166

Slide 166 text

#hhvm spatch: Solution $ spatch -f remove_second_arg_foo.spatch *.php

Slide 167

Slide 167 text

#hhvm spatch: Problem Rename a function with a variable number of arguments

Slide 168

Slide 168 text

#hhvm spatch: Solution - foo + bar (...)

Slide 169

Slide 169 text

@jazzdan perf(1)

Slide 170

Slide 170 text

No content

Slide 171

Slide 171 text

No content

Slide 172

Slide 172 text

@jazzdan perf(1) - 12.09% HPHP::f_sort + PHP::…getAllReplicantNames

Slide 173

Slide 173 text

Lessons Learned

Slide 174

Slide 174 text

@jazzdan If you’re running an old operating system…

Slide 175

Slide 175 text

@jazzdan …you’re gonna have a bad time.

Slide 176

Slide 176 text

@jazzdan Tee’ing traffic is a superpower

Slide 177

Slide 177 text

@jazzdan HHVM is Rock Solid

Slide 178

Slide 178 text

@jazzdan Extensions Sometimes Aren't

Slide 179

Slide 179 text

@jazzdan Threads are Hard

Slide 180

Slide 180 text

@jazzdan Tooling is Powerful

Slide 181

Slide 181 text

@jazzdan Lessons Learned • Do: • Run a newer Linux distribution • Ramp up slowly • Don’t: • Trust that extensions are 100% • Assume that processes are like threads

Slide 182

Slide 182 text

The Future

Slide 183

Slide 183 text

No content

Slide 184

Slide 184 text

No content

Slide 185

Slide 185 text

No content

Slide 186

Slide 186 text

No content

Slide 187

Slide 187 text

No content

Slide 188

Slide 188 text

No content

Slide 189

Slide 189 text

@jazzdan What does the future hold?

Slide 190

Slide 190 text

@jazzdan No one knows

Slide 191

Slide 191 text

@jazzdan But now we are better prepared

Slide 192

Slide 192 text

No content

Slide 193

Slide 193 text

@jazzdan Questions? ( https://joind.in/13384 )