What is it good for? ■ Fault tolerance - maximum uptime ■ Large number of connections / IO ■ High level of concurrency ■ Distributed across machines
1. Built on Erlang
Erlang - 1986 It was originally designed by Ericsson to support distributed, fault-tolerant, soft real-time, highly available, non-stop applications. It supports hot swapping, thus code can be changed without stopping a system.
Erlang in the Wild WhatsApp RabbitMQ Messaging app RabbitMQ is an open Riak is a distributed acquired by Facebook source message broker NoSQL key-value data for $19 billion. Used software that store that offers high Erlang for handling 2 implements the availability, fault million connections a Advanced Message tolerance, operational server. 10 team Queuing Protocol simplicity, and members work on (AMQP). It is fault scalability. Erlang and they handle tolerant and both development and distributed. ops.
Erlang downsides? ■ Learning Curve ■ Syntax based on Prolog, not as approachable (to some)
2. Enter Elixir
About Elixir ■ Invented by José Valim, Rails committer ■ Syntax inspired by ○ Ruby ○ Clojure ■ Nicely bring together strengths of these languages while gaining benefits of Erlang VM
Immutable data structures Potential source of errors in Ruby:
Immutable data structures What’s wrong with this? ■ You don’t know what to expect of your data when you call a method ■ Especially painful for threading ○ Concurrent modification errors ○ Race conditions
Immutable data structures In Elixir all data structures are immutable!! ■ You can safely share with other processes ■ Don’t have to worry about data changing under the covers
Immutable data structures
Pattern Matching ‘=’ operator in action:
“ You Keep Using That Word, I Do Not Think It Means What You Think It Means
Pattern Matching The ‘=’ operator handles assignment and comparison at the same time. Can “destructure” complex types and assign variables automatically.
Pattern matching Fail when error code is not as expected: Pluck out variables from complex structures
Pattern matching in methods Multiple method definitions, match params: No need for guards inside methods
Pipe Operator ■ Functional Programming: Series of data transformations. ■ Pipe operator makes this cleaner: |>
Macros ■ Allow extension of the language in a clean way ■ Can control execution of passed in code ■ Gives you access to passed in AST
Lightweight processes / Message passing ■ Native support for lightweight processes ■ Can start tens of thousands of processes ■ Processes are isolated ■ Communicate via messages
Lightweight processes / Message passing
OTP / Supervision hierarchy ■ OTP= “Rails” of event driven architecture ■ Supervision hierarchies help you define fault tolerance ■ “Let it crash” - reboot with known state ■ Define supervisors for processes and the process / dependencies for restarting
Fault tolerance / supervision
Distribution ■ Distribution is free! ■ Can send and receive messages from other nodes ■ Can supervise other nodes
Growing Ecosystem Number of tools is growing Rails Phoenix ActiveRecord Ecto Rack Plug irb iex rake / bundler mix
Working on Exq library - Sidekiq for Elixir Feel free to help out!