Defensive Programming vs. Let It Crash

Defensive Programming vs. Let It Crash

Shared at the second meetup of Elixir Shanghai.

A talk highly inspired by Ferd's "The Zen of Erlang" post, but from a different angle.

What does Erlang bring to the table that could enable us to be less defensive and more confident about our code?

55726c206ec597ea0e33fa04ce6b3110?s=128

Lou Xun

July 30, 2016
Tweet

Transcript

  1. Defensive(Programming vs.$Let$It$Crash Lou$Xun$aquarhead@ela.build$/$Ela$Workshop

  2. 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?" Lou$Xun$aquarhead@ela.build$/$Ela$Workshop
  3. 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 Lou$Xun$aquarhead@ela.build$/$Ela$Workshop

  4. 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 Lou$Xun$aquarhead@ela.build$/$Ela$Workshop
  5. 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 Lou$Xun$aquarhead@ela.build$/$Ela$Workshop
  6. You$program$defensively because'you'cannot'simply Let$It$Crash Lou$Xun$aquarhead@ela.build$/$Ela$Workshop

  7. 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 Lou$Xun$aquarhead@ela.build$/$Ela$Workshop
  8. 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 Lou$Xun$aquarhead@ela.build$/$Ela$Workshop
  9. 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 Lou$Xun$aquarhead@ela.build$/$Ela$Workshop
  10. 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? Lou$Xun$aquarhead@ela.build$/$Ela$Workshop
  11. Supervision+Tree Lou$Xun$aquarhead@ela.build$/$Ela$Workshop

  12. 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 Lou$Xun$aquarhead@ela.build$/$Ela$Workshop
  13. 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 Lou$Xun$aquarhead@ela.build$/$Ela$Workshop
  14. 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 Lou$Xun$aquarhead@ela.build$/$Ela$Workshop
  15. Thanks h"p:/ /ferd.ca/the.zen.of.erlang.html Lou$Xun$aquarhead@ela.build$/$Ela$Workshop