Slide 1

Slide 1 text

Defensive(Programming vs.$Let$It$Crash [email protected]$/$Ela$Workshop

Slide 2

Slide 2 text

A"cri&cal"bug"happened... • Resources)exhausted • Users)disconnect,)system)becomes)laggy • Cascading)failure • Loads)of)log,)errors)everywhere • "What)the)hell)is)the)root$cause?" [email protected]$/$Ela$Workshop

Slide 3

Slide 3 text

You$started$debugging... • Follow&the&stacktrace • Looks&like&it's&some&cleanup&code • Something&is&wrong&in&the&try&block&(without&log!) • And&you&failed&to&recover&it&in&the&catch&block [email protected]$/$Ela$Workshop

Slide 4

Slide 4 text

What%if%you%had... • The%exact%loca,on%where%things%start%to%fail • The%exact%"context"%at%there • local%variables,%global%variables • object%state • etc.. • Essen,al%informa,ons%are%very%easy%to%lose [email protected]$/$Ela$Workshop

Slide 5

Slide 5 text

You're'defensive,'because... • If$you$don't$catch,$a$raise$will$propagate$to$the$ top... • and$crash$everything • You$write$code$that$you$wish$never$get$executed$in$ produc;on • You're$hiding$a$broken$state$in$the$system [email protected]$/$Ela$Workshop

Slide 6

Slide 6 text

You$program$defensively because'you'cannot'simply Let$It$Crash [email protected]$/$Ela$Workshop

Slide 7

Slide 7 text

Erlang's)Process • Isolated • one+does+not+kill+all • Share+nothing • less+context+to+be+concerned • Lightweight • use+as+many+as+you+need • not+as+many+as+you+can [email protected]$/$Ela$Workshop

Slide 8

Slide 8 text

Other&ingredients • A#func(onal#language • much#less#state • Message#passing • async#opera(on • Monitor#&#Link • handle#process#down,#fail#as#a#unit • Preemp(ve#scheduling,#Distribu(on [email protected]$/$Ela$Workshop

Slide 9

Slide 9 text

Erlang/OTP • A#set#of#Erlang#libraries#and#design'principles • gen_server#etc.. • maintain#state • unified#interface • supervisor • sole#job#is#supervising#children • start,#stop,#monitor,#restart [email protected]$/$Ela$Workshop

Slide 10

Slide 10 text

State%&%Restart • State • Why%restart%works? • What%can%restart%fix? • Cannot%fix%everything%(of%course) • Clean%state • Isn't%it%the%same%as%restar?ng%my%daemon? [email protected]$/$Ela$Workshop

Slide 11

Slide 11 text

Supervision+Tree [email protected]$/$Ela$Workshop

Slide 12

Slide 12 text

Structured(supervision • Processes(close(to(root(are(robust,(solid,(rarely( change • Processes(down(to(leaves(can(be(fragile,(change(a( lot,(fail(regularly • When(a(process(fail,(supervisor(restarts(according( to(the(structure • Use(structure(to(recover(from(failure • Not(code [email protected]$/$Ela$Workshop

Slide 13

Slide 13 text

Let$It$Crash • Don't'be'afraid'of'failures,'excep6ons'or'crashes • They'are'tools'we'can'use • No'more'defensive'programming,'only'code'as'if' everything'goes'right • Or'if'you'absolutely'know'how'to'deal'with'a' certain'failure • Otherwise'just'let$it$crash [email protected]$/$Ela$Workshop

Slide 14

Slide 14 text

vs.$Defensive$Programming • Supervision,Tree,vs.,Nested,try..catch • Process,vs.,? • Self7healing,in,produc;on • Fail,early,,fail,fast,aids,debugging • Avoid,wri;ng,unused,error,handling,code • Produc;vity,,readability [email protected]$/$Ela$Workshop

Slide 15

Slide 15 text

Thanks h"p:/ /ferd.ca/the.zen.of.erlang.html [email protected]$/$Ela$Workshop