1 hour dive into
            Erlang/OTP


@jvalduvieco            @jordillonch
Problem domain
Lots of users
Lots of users
24x7x365
24x7x365
Lots of critical concurrent
       transactions
Lots of critical concurrent
       transactions
Hardware or software
      breaks
Hardware or software
      breaks
Lots of code changes
Lots of code changes
Unscalable
Unmaintainable   }   code
Unscalable
Unmaintainable   }   code
Maintenance/Debug
        in
production system
Maintenance/Debug
        in
production system
Does it sound to you?
The Erlang solution
Simplicity...
Minimize defensive
  programming
Typeless variables
Develop by contract
t-shirt function
      size
  If
 a
 function
 does
 not
 fit
 
on
 your
 t-shirt
 it
 is
 too
 long!
Single responsibility
      principle
No shared state
between entities
High Concurrency
High Concurrency
High concurrency
Light threads
Light threads
    hundreds of thousands of threads in one
      machine with a good cpu scheduler
Light threads
    hundreds of thousands of threads in one
      machine with a good cpu scheduler


    Lt

                                        Lt




               Lt
Message passing


     Lt

                  Lt




             Lt
Message passing
        A shared nothing architecture that
      communicates through message passing

     Lt

                                        Lt




                 Lt
Message passing
        A shared nothing architecture that
      communicates through message passing

     Lt

                                        Lt




                 Lt
Process Mailbox


     Lt

                  Lt




             Lt
Process Mailbox
     Every process has a mailbox with incoming
       messages, process take on convenience

     Lt

                                           Lt




                  Lt
No shared state


       Lt
   S
                     Lt
                          S



                Lt
            S
No shared state
    Every process its own internal state stored in
         a variable avoiding lock contention

       Lt
   S
                                             Lt
                                                  S



                    Lt
                S
Soft realtime
Soft realtime

You have no strict guarantees on
latency but language is designed
      to have low latency
High availability
High availability
 High availability
Supervised processes



       Pa




                       Pb
Supervised processes
     processes can be monitored by other
      processes, handling its termination


       Pa




                                  Pb
Fail early


                  Pa




             Pb
              S
Fail early
 Fail as soon as possible and let someone
handle bad data, someone will restart you

                                      Pa




                        Pb
                         S
Fail early
 Fail as soon as possible and let someone
handle bad data, someone will restart you

                                      Pa




                       Pb2
                          S
Hot code update




Pb
          Pa v1
              S

     Pc
Hot code update
     processes code and data can be replaced
             without loosing service




Pb
                   Pa v1
                       S

     Pc
Hot code update
     processes code and data can be replaced
             without loosing service




Pb
                   Pa v1
                      v2
                       S

     Pc
Distribution



Node            Node

P      P        P      P



P      P        P      P



P      P        P      P
Distribution
Processes run on nodes and can be located
            wherever they are


Node                        Node

P      P                     P      P



P      P                     P      P



P      P                     P      P
How does it look like?
Show me the code!
Demo 1
Hands on
The shell
Type on your console:

        erl
You should see:
Erlang R16B (erts-5.10.1) [source] [64-bit]
[smp:2:2] [async-threads:10] [hipe] [kernel-
poll:false] [dtrace]

Eshell V5.10.1   (abort with ^G)
1
Variables
Variables start
 Uppercase
Variables are immutable
Can contain any type
You can do things like...
1 Foo = 1.
1
2 Foo = 2.
** exception error: no match of right hand
side value 2
Foo
 is
 bounded
 to
 1
1 Foo = 1.
1
2 Foo = 2.
** exception error: no match of right hand
side value 2
Foo
 is
 bounded
 to
 1
1 Foo = 1.
1
          Foo
 ==
 2
 ?
2 Foo = 2.
** exception error: no match of right hand
side value 2
1 Foo = 1.
1
2 Bar = 2.
2
3 Foo = Bar.
** exception error: no match of right
hand side value 2
This is GREAT!
Now you have your
basic error checking
system implemented
Advanced
 types
[List]
[1,2,3,4,5,6]
You can do things like...
Iterate sequentially
1 MyList = [1,2,3,4].
[1,2,3,4]
2 [Head|Tail] = MyList.
[1,2,3,4]
3 Head.
1
4 Tail.
[2,3,4]
1 MyList = [1,2,3,4].
[1,2,3,4]
2 [Head|Tail] = MyList.
[1,2,3,4]
3 Head.
1
4 Tail.
[2,3,4]
1 MyList = [1,2,3,4].
[1,2,3,4]
2 [Head|Tail] = MyList.
[1,2,3,4]
3 Head.
1
4 Tail.
[2,3,4]
Do something to all or
 some items on a list
(list comprehensions)
1 MyList = [1, 2, 3, 4].
[1,2,3,4]
2 [X + 2 || X - MyList, X  2].
[5,6]
[Strings]
A list
You can do things like...
1 MyString = Erlang is not Ruby.
Erlang is not Ruby
1 MyString = Erlang is not Ruby.
Erlang is not Ruby
2 [Head2|Tail2] = MyString.
Erlang is not Ruby
3 Head2.
69 ASC
 =
 “E”
4 Tail2.
rlang is not Ruby
{Tuples}
{1,2,3}
Basic data container
random access
matcheable
You can do things like...
1 Mytuple = {1,2,3,4}.
{1,2,3,4}
2 A = 1.
1
3 B = 2.
2
4 {A,B,C,D} = {1,2,3,4}.
{1,2,3,4}
5 C.
3
6 D.
4
1 Mytuple = {1,2,3,4}.
{1,2,3,4}
2 A = 1.
1         A
 and
 B
 are
3 B = 2. bounded
 variables
2
4 {A,B,C,D} = {1,2,3,4}.
{1,2,3,4}
5 C.
3
6 D.
4
1 Mytuple = {1,2,3,4}.
{1,2,3,4}
2 A = 1.
1         A
 and
 B
 are
3 B = 2. bounded
 variables
2
4 {A,B,C,D} = {1,2,3,4}.
{1,2,3,4}
5 C. pattern
 matching
3                                           A==1?
6 D.                              B==2?
4
1 Mytuple = {1,2,3,4}.
{1,2,3,4}
2 A = 1.
1         A
 and
 B
 are
3 B = 2. bounded
 variables
2
4 {A,B,C,D} = {1,2,3,4}.                                                                                                  C
 and
 D
 are
 
{1,2,3,4}
                                                                                                                           unbounded
 
5 C. pattern
 matching
                                                                                                                           variables
3                                           A==1?
6 D.                              B==2?
4
functions
functions are types
single exit point
You can do things like...
foo_bar_func(Foo, Bar) -
 Result = Foo + Bar,
 Result.
This
 is
 a
 arity
 2
 function
 
                                              (2
 parameters)

foo_bar_func(Foo, Bar) -
 Result = Foo + Bar,
 Result.
This
 is
 a
 arity
 2
 function
 
                                                                        (2
 parameters)

foo_bar_func(Foo, Bar) -
 Result = Foo + Bar,
 Result.
                  means
 that
 more
 
                  code
 is
 to
 come
This
 is
 a
 arity
 2
 function
 
                                                                        (2
 parameters)

foo_bar_func(Foo, Bar) -
 Result = Foo + Bar,
 Result.
                  means
 that
 more
 
                  code
 is
 to
 come

                                                                                                                                                        means
 the
 function
 
                                                                                                                                                        code
 has
 ended
This
 is
 a
 arity
 2
 function
 
                                                                                                 (2
 parameters)

foo_bar_func(Foo, Bar) -
 Result = Foo + Bar,
 Result.
                                                                                                                    means

1 hour dive into erlang