Slide 1

Slide 1 text

Expect the un-expected How to handle errors gracefully @BastianHofmann

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

12 million users

Slide 5

Slide 5 text

193 countries

Slide 6

Slide 6 text

~1800 request/s

Slide 7

Slide 7 text

lots of data

Slide 8

Slide 8 text

>100 million publications

Slide 9

Slide 9 text

~ 140 components

Slide 10

Slide 10 text

~ 400 repositories

Slide 11

Slide 11 text

~ 80 engineers

Slide 12

Slide 12 text

Growing Traffic

Slide 13

Slide 13 text

Active development

Slide 14

Slide 14 text

Continuous Delivery

Slide 15

Slide 15 text

Bugs

Slide 16

Slide 16 text

Performance regressions

Slide 17

Slide 17 text

Database overloads

Slide 18

Slide 18 text

Hardware failures

Slide 19

Slide 19 text

Distributed Systems

Slide 20

Slide 20 text

Microservices

Slide 21

Slide 21 text

Multiple technology stacks

Slide 22

Slide 22 text

$complexity++

Slide 23

Slide 23 text

Errors happen

Slide 24

Slide 24 text

How to detect them

Slide 25

Slide 25 text

How to handle them

Slide 26

Slide 26 text

Detecting if something breaks

Slide 27

Slide 27 text

Logging

Slide 28

Slide 28 text

Error Logs

Slide 29

Slide 29 text

callable set_exception_handler( callable $exception_handler );

Slide 30

Slide 30 text

bool error_log ( string $message [, int $message_type = 0 [, string $destination [, string $extra_headers ]]] )

Slide 31

Slide 31 text

ErrorLog /var/logs/apache/error.log

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

Monolog

Slide 34

Slide 34 text

Log additional info

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

Request Information

Slide 37

Slide 37 text

PHP has more than just Exceptions Throwables

Slide 38

Slide 38 text

Handling Notices, Warnings, Errors

Slide 39

Slide 39 text

callable set_error_handler( callable $error_handler [, int $error_types = E_ALL | E_STRICT ] );

Slide 40

Slide 40 text

set_error_handler( function($errno, $msg, $file, $line) { $e = new \Exception(); $error = [ 'type' => $errno, 'message' => $msg, 'file' => $file, 'line' => $line, 'trace' => $e->getTrace(), ]; error_log(json_encode($error)); return true; });

Slide 41

Slide 41 text

Or directly turn them into Exceptions

Slide 42

Slide 42 text

set_error_handler( function($errno, $msg, $file, $line) { switch ($errno) { case E_RECOVERABLE_ERROR: case E_USER_ERROR: throw new \ErrorException($msg, null, $errno, $file, $line); case E_WARNING: case E_USER_WARNING: case E_CORE_WARNING: case E_COMPILE_WARNING: throw new WarningException($msg, null, $errno, $file, $line); case E_NOTICE: case E_USER_NOTICE: throw new NoticeException($msg, null, $errno, $file, $line); case E_STRICT: throw new StrictException($msg, null, $errno, $file, $line); } return true; }); $a = []; try { $b = $a['doesNotExist']; } catch (NoticeException $e) { }

Slide 43

Slide 43 text

But what about fatal errors

Slide 44

Slide 44 text

Use error_get_last() in a Shutdown Handler http://php.net/manual/en/function.error- get-last.php

Slide 45

Slide 45 text

Log in a structured way

Slide 46

Slide 46 text

JSON http://www.ietf.org/rfc/rfc4627.txt

Slide 47

Slide 47 text

Logs from other services

Slide 48

Slide 48 text

web server http service http service http service http service user request log log log log log

Slide 49

Slide 49 text

Correlation / Tracing ID

Slide 50

Slide 50 text

web server http service http service http service http service create unique trace_id for request user request trace_id trace_id trace_id trace_id log log log log log

Slide 51

Slide 51 text

X-Trace-Id: bbr8ehb984tbab894

Slide 52

Slide 52 text

Getting to the logs

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

Put the logs in a central place

Slide 57

Slide 57 text

Make them easily full-text searchable

Slide 58

Slide 58 text

Make them aggregate-able

Slide 59

Slide 59 text

Logstash + Elasticsearch + Kibana

Slide 60

Slide 60 text

Full text search

Slide 61

Slide 61 text

Structured Messages

Slide 62

Slide 62 text

Logstash elasticsearch webserver webserver webserver AMQP log log log logstash logstash logstash

Slide 63

Slide 63 text

logstash http://logstash.net/

Slide 64

Slide 64 text

input filter output

Slide 65

Slide 65 text

Very rich plugin system

Slide 66

Slide 66 text

Logstash elasticsearch webserver webserver webserver AMQP log log log logstash logstash logstash

Slide 67

Slide 67 text

input { file { type => "error" path => [ "/var/logs/php/*.log" ] add_field => [ "severity", "error" ] } }

Slide 68

Slide 68 text

filter{ json { source => "message" } }

Slide 69

Slide 69 text

output { amqp { host => "amqp.host" exchange_type => "fanout" name => "logs" } }

Slide 70

Slide 70 text

Logstash elasticsearch webserver webserver webserver AMQP log log log logstash logstash logstash

Slide 71

Slide 71 text

input {
 rabbitmq {
 queue => "logs"
 host => "amqp.host"
 exchange => "ls_exchange"
 exclusive => false
 }
 }

Slide 72

Slide 72 text

output {
 elasticsearch {
 embedded => false
 bind_host => "localhost"
 bind_port => "9305"
 host => "localhost"
 cluster => "logs"
 }
 }

Slide 73

Slide 73 text

Kibana http://www.elasticsearch.org/overview/kibana/

Slide 74

Slide 74 text

No content

Slide 75

Slide 75 text

Always Log to file

Slide 76

Slide 76 text

Monitoring

Slide 77

Slide 77 text

Latency

Slide 78

Slide 78 text

Availability

Slide 79

Slide 79 text

Throughput

Slide 80

Slide 80 text

Overall

Slide 81

Slide 81 text

Granular

Slide 82

Slide 82 text

Graphite http://graphite.wikidot.com/

Slide 83

Slide 83 text

No content

Slide 84

Slide 84 text

StatsD https://github.com/etsy/statsd/

Slide 85

Slide 85 text

webserver webserver webserver statsd statsd statsd graphite

Slide 86

Slide 86 text

https://www.librato.com

Slide 87

Slide 87 text

http://www.soasta.com/

Slide 88

Slide 88 text

Dashboards

Slide 89

Slide 89 text

No content

Slide 90

Slide 90 text

No content

Slide 91

Slide 91 text

No content

Slide 92

Slide 92 text

Alerts

Slide 93

Slide 93 text

No content

Slide 94

Slide 94 text

Errors happen

Slide 95

Slide 95 text

Handling Exceptions gracefully

Slide 96

Slide 96 text

No content

Slide 97

Slide 97 text

Component based fronted

Slide 98

Slide 98 text

No content

Slide 99

Slide 99 text

No content

Slide 100

Slide 100 text

No content

Slide 101

Slide 101 text

No content

Slide 102

Slide 102 text

No content

Slide 103

Slide 103 text

No content

Slide 104

Slide 104 text

No content

Slide 105

Slide 105 text

Degrading Functionality

Slide 106

Slide 106 text

Profile Publications Publication Publication Publication AboutMe LeftColumn Image Menu Institution

Slide 107

Slide 107 text

Profile Publications Publication Publication Publication AboutMe LeftColumn Image Menu EXCEPTION Institution

Slide 108

Slide 108 text

Profile Publications Publication Publication Publication LeftColumn Image Menu Institution

Slide 109

Slide 109 text

Deployments

Slide 110

Slide 110 text

Errors happen

Slide 111

Slide 111 text

Safe deployments

Slide 112

Slide 112 text

Feature Flags/ Toggles

Slide 113

Slide 113 text

Release !== Deployment

Slide 114

Slide 114 text

public function hasAccess( $accountId, array $roleNames ) { return featureFlag()->isActive( FeatureFlag::TEST_ONE ); }

Slide 115

Slide 115 text

No content

Slide 116

Slide 116 text

No content

Slide 117

Slide 117 text

Partial rollout

Slide 118

Slide 118 text

Automation

Slide 119

Slide 119 text

Canary environments

Slide 120

Slide 120 text

Server Server Server Server

Slide 121

Slide 121 text

Server Server Server Server Test with low amount of traffic

Slide 122

Slide 122 text

Fast rollbacks

Slide 123

Slide 123 text

Oftentimes it’s not only one monolith

Slide 124

Slide 124 text

No content

Slide 125

Slide 125 text

Safe Service-to Service Communication

Slide 126

Slide 126 text

Service A Service B 200 OK

Slide 127

Slide 127 text

Distributed Systems are hard

Slide 128

Slide 128 text

Errors happen

Slide 129

Slide 129 text

Failures

Slide 130

Slide 130 text

Service A Service B 5xx

Slide 131

Slide 131 text

Timeouts

Slide 132

Slide 132 text

Service A Service B Timeout

Slide 133

Slide 133 text

Low timeout settings

Slide 134

Slide 134 text

Service A Service B Only wait for max. 50ms

Slide 135

Slide 135 text

Don’t call service instances directly

Slide 136

Slide 136 text

Use a Load Balancer

Slide 137

Slide 137 text

Service A Service B Service C Service C Load balancer Choose one running instance

Slide 138

Slide 138 text

Health checks

Slide 139

Slide 139 text

Service A Service B Service C Service C Load balancer Are you still alive?

Slide 140

Slide 140 text

Service A Service B Service C Service C Load balancer Are you still alive?

Slide 141

Slide 141 text

Service A Service B Service C Service C Load balancer

Slide 142

Slide 142 text

Circuit Breakers

Slide 143

Slide 143 text

Service A Service B 200 OK Circuit Breaker Status: closed Error rate: 0

Slide 144

Slide 144 text

Service A Service B Error Circuit Breaker Status: -> open Error rate: > threshold

Slide 145

Slide 145 text

Service A Service B Circuit Breaker Status: -> open Error rate: > threshold

Slide 146

Slide 146 text

Service A Service B Error Circuit Breaker Status: -> open Error rate: > threshold Test if still failing

Slide 147

Slide 147 text

Service A Service B 200 OK Circuit Breaker Status: -> close Error rate: 0 Test if still failing

Slide 148

Slide 148 text

How do I handle traffic spikes?

Slide 149

Slide 149 text

Service A Service B 200 OK Circuit Breaker

Slide 150

Slide 150 text

Service A Service B Circuit Breaker Service C Circuit Breaker Timeouts

Slide 151

Slide 151 text

Service A Service B Circuit Breaker Service C Circuit Breaker Timeouts

Slide 152

Slide 152 text

Throttling

Slide 153

Slide 153 text

Service A Service B Circuit Breaker Service C Circuit Breaker Only allow xx% of calls

Slide 154

Slide 154 text

No content

Slide 155

Slide 155 text

Priority

Slide 156

Slide 156 text

Service A Service B Circuit Breaker Service C Circuit Breaker 100% of calls 10% of calls

Slide 157

Slide 157 text

https://github.com/Netflix/Hystrix

Slide 158

Slide 158 text

https://github.com/odesk/phystrix

Slide 159

Slide 159 text

No content

Slide 160

Slide 160 text

Service A Service B Service C Circuit Breaker LinkerD

Slide 161

Slide 161 text

Test it

Slide 162

Slide 162 text

http://techblog.netflix.com/2014/09/introducing-chaos-engineering.html

Slide 163

Slide 163 text

Summing it up

Slide 164

Slide 164 text

Central Log Management

Slide 165

Slide 165 text

Monitor and Measure everything

Slide 166

Slide 166 text

Alerts

Slide 167

Slide 167 text

Graceful Exception Handling

Slide 168

Slide 168 text

Feature Flags

Slide 169

Slide 169 text

Partial Rollouts

Slide 170

Slide 170 text

Circuit Breakers

Slide 171

Slide 171 text

http://speakerdeck.com/u/bastianhofmann

Slide 172

Slide 172 text

http://twitter.com/BastianHofmann http://lanyrd.com/people/BastianHofmann http://speakerdeck.com/u/bastianhofmann [email protected]