The Little Elixir & OTP Guidebook
Benjamin Tan Wei Hao
  • September 2016
  • ISBN 9781633430112
  • 296 pages
  • printed in black & white

Move seamlessly from learning the basics of Elixir to mastering the key concepts of OTP.

Roberto Infante, Devqf Ltd.

The Little Elixir & OTP Guidebook gets you started programming applications with Elixir and OTP. You begin with a quick overview of the Elixir language syntax, along with just enough functional programming to use it effectively. Then, you'll dive straight into OTP and learn how it helps you build scalable, fault-tolerant and distributed applications through several fun examples.

Table of Contents detailed table of contents

1. Introduction

1.1. Elixir

1.2. How is Elixir different from Erlang

1.2.1. Tooling

1.2.2. Ecosystem

1.3. Why Elixir and not X?

1.4. What is Elixir/OTP good for?

1.5. The Road Ahead

1.5.1. A Sneak Preview of OTP Behaviours

1.5.2. Distribution for Load-Balancing and Fault-Tolerance

1.5.3. Dialyzer and Type-Specifications

1.5.4. Property and Concurrency Testing

1.6. Summary

2. A Whirlwind Tour

2.1. Setting Up Your Environment

2.2. First Steps

2.2.1. Here Running an Elixir program in Interactive Elixir

2.2.2. Stopping an Elixir program

2.2.3. Getting Help

2.3. Data Types

2.3.1. Modules , Functions and Function Clauses

2.3.2. Numbers

2.3.3. Strings

2.3.4. Atoms

2.3.5. Tuples

2.3.6. Maps

2.4. Guards

2.5. Pattern Matching

2.5.1. = is used for assigning

2.5.2. = is also used for matching

2.5.3. Destructuring

2.6. Lists

2.6.1. Example: flattening a list

2.6.2. Ordering of function clauses

2.7. Meet |>, the Pipe operator

2.7.1. Example: filtering files in a directory by filename

2.8. Erlang Interoperability

2.8.1. Calling Erlang functions from Elixir

2.8.2. Calling the Erlang HTTP client in Elixir

2.8.3. One more thing…​

2.9. Exercises

2.10. Summary

3. Processes 101

3.1. Actor Concurrency Model

3.2. Building a Weather application

3.2.1. The Naïve Version

3.3. The Worker

3.4. Creating Processes for Concurrency

3.4.1. Receiving Messages

3.4.2. Sending Messages

3.5. Collecting and Manipulating Results with Another Actor

3.5.1. {:ok, result} ¬- The Happy Path Message - {:ok, result}

3.5.2. :exit ¬- The Poison Pill Message

3.5.3. Any Other Messages

3.5.4. The Bigger Picture

3.6. Exercises

3.7. Summary

4. Writing Server Applications with GenServer

4.1. What is OTP?

4.2. OTP Behaviours

4.2.1. The Different OTP Behaviours

4.3. Hands On OTP: Revisiting Metex

4.3.1. Creating a New Project

4.3.2. Making The Worker GenServer Compliant

4.3.3. Callbacks

4.3.4. Reflecting on chapter 3’s Metex

4.4. Exercise

4.5. Summary

5. Concurrent Error Handling and Fault Tolerance with Links, Monitors, and Processes

5.1.1. Linking Processes Together

5.1.2. Chain Reaction of Exit Signals

5.1.3. Setting up the Ring

5.1.4. Trapping Exits

5.1.5. Linking a terminated/non-existent process

5.1.7. Exit Messages

5.1.8. Ring, revisited

5.2. Monitors

5.2.1. Monitoring a Terminated/Non-Existent Process

5.3. Implementing a Supervisor

5.3.1. Supervisor API

5.3.2. Building Our Own Supervisor

5.3.4. Handling Crashes

5.3.5. Full Completed Source

5.4. A Sample Run (Or: Does It Really Work?)

5.5. Summary

6. Fault-tolerance with Supervisors

6.1. Implementing Pooly - a Worker Pool Application

6.1.1. The Plan

6.1.2. A Sample Run of Pooly

6.1.3. Diving into Pooly, Version 1: Laying the Groundwork

6.2. Implementing the Worker Supervisor

6.2.1. Initializing the Supervisor

6.2.2. Supervision Options

6.2.3. Restart Strategies

6.2.4. Max Restarts and Max Seconds

6.2.5. Defining Children

6.3. Implementing the Server: The Brains of the Operation

6.3.1. Pool Configuration

6.3.2. Validating the Pool Configuration

6.3.3. Starting the Worker Supervisor

6.3.4. Pre-populating the Worker Supervisor with Workers

6.3.5. Creating a New Worker Process

6.3.6. Checking-out a Worker

6.3.7. Checking-in a Worker

6.3.8. Getting the Status of the Pool

6.4. Implementing the Top Level Supervisor

6.5. Making Pooly an OTP Application

6.6. Bringing Pooly for a Spin

6.7. Exercises

6.8. Summary

7. Completing the Worker Pool Application

7.1. Version 3: Error Handling, Multiple Pools and Workers

7.1.1. Case 1: Crashes between the Server and Worker

7.1.2. Case 2: Crashes between the Server and Worker

7.1.3. Handling Multiple Pools

7.1.4. Adding the Application Behavior to Pooly

7.1.5. Adding the Top-level Supervisor

7.1.6. Adding the Supervisor of Pools

7.1.7. Making Pooly.Server Dumber

7.1.8. Adding the Pool Supervisor

7.1.9. Adding the Brains for the Pool

7.1.10. Adding the Worker Supervisor for the Pool

7.1.11. Taking it for a spin

7.2. Version 4: Implementing Overflowing and Queuing

7.2.1. Implementing Maximum Overflow

7.2.2. Handling Worker Check-ins

7.2.3. Handling Worker Exits

7.2.4. Updating Status with Overflow Information

7.2.5. Queuing Worker Processes

7.2.6. Taking it for a spin

7.3. Exercises

7.4. Summary

8. Distribution and Load Balancing

8.1. Why Distributed?

8.2. Distribution for Load Balancing

8.2.1. An Overview of Blitzy, the Load Tester

8.2.2. Let the Mayhem Begin!

8.2.3. Implementing the Worker Process

8.2.4. Running the Worker

8.3. Introducing Tasks

8.4. Onward to Distribution!

8.4.1. Location Transparency

8.4.2. An Elixir Node

8.4.3. Creating a Cluster

8.4.4. Connecting Nodes

8.4.5. Node Connections Are Transitive

8.5. Remotely Executing Functions

8.6. Making Blitzy Distributed

8.6.1. Creating a command line interface

8.6.2. Parsing input arguments with OptionParser

8.6.3. Connecting to the Nodes

8.6.4. Supervising Tasks with Tasks.Supervisor

8.6.5. Using a Task Supervisor

8.6.6. Creating the binary with mix

8.6.7. Running Blitzy!

8.7. Summary

9. Distribution and Fault Tolerance

9.1. Distribution for Fault Tolerance

9.2. Building Chucky

9.2.1. Implementing the Server

9.2.2. Implementing the Application Behavior

9.2.3. Application type arguments

9.3. An Overview of Failover and Takeover in Chucky

9.3.1. Step 1: determine the hostname(s) of the machine(s)

9.3.2. Step 2: create configuration files for each of the nodes

9.3.3. Step 3: fill the configuration files for each of the nodes

9.3.4. Step 4: compile Chucky on all the nodes

9.3.5. Step 5: start the distributed application

9.4. Failover and Takeover in Action

9.5. Connecting Nodes in a LAN, Cookies and Security

9.5.1. Find out the IP Addresses of both machines

9.5.2. Connecting both Nodes together

9.6. Great Summary

10. Dialyzer and Type Specifications

10.1. Introducing Dialyzer

10.2. Success Typings

10.3. Getting Started with Dialyzer

10.3.1. The Persistent Lookup Table

10.3.2. Dialyxir

10.3.3. Building a PLT Table

10.4. Software Discrepancies that Dialyzer can Detect

10.4.1. Catching Type Errors

10.4.2. Wrong Use of Built-In Functions

10.4.3. Redundant Code

10.4.4. Type Errors in Guard Clauses

10.4.5. Tripping Up Dialyzer with Some Indirection

10.5. Type Specifications

10.5.1. Writing Type Specifications

10.6. Writing your own Types

10.6.1. Multiple Return Types and Bodiless Function Clauses

10.6.2. Back to Bug #5

10.7. Exercises

10.8. Summary

11. Property-based and concurrency testing

11.1. Introduction to Property-Based Testing and QuickCheck

11.1.1. Installing QuickCheck

11.1.2. Using QuickCheck in Elixir

11.1.3. Patterns for Designing Properties

11.1.4. Generators

11.1.5. Built-in Generators

11.1.6. Creating Custom Generators

11.1.7. Recursive Generators

11.1.8. Summary of QuickCheck

11.2. Concurrency Testing with Concuerror

11.2.1. Installing Concuerror

11.2.2. Setting Up the Project

11.2.3. Types of Errors that Concuerror can Detect

11.2.4. Deadlocks

11.2.5. Reading Concuerror's Outputs

11.2.6. Concuerror Summary

11.3. Resources

11.4. Summary


Appendix A: Installing Erlang & Elixir

A.1. Getting Erlang

A.2. Method 1: Package Managers / Pre-built installers.

A.2.1. Mac OS X via Homebrew and Macports

A.2.2. Linux (Ubuntu and Fedora)

A.2.3. MS Windows

A.3. Method 2: Compiling from scratch (Linux/Unix only)

A.4. Verifying your Elixir installation

About the Technology

Elixir is an elegant programming language that combines the expressiveness of Ruby with the concurrency and fault-tolerance of Erlang. It makes full use of Erlang’s BEAM VM and OTP library, so you get two decades’ worth of maturity and reliability right out of the gate. Elixir’s support for functional programming makes it perfect for modern event-driven applications.

About the book

The Little Elixir & OTP Guidebook gets you started writing applications with Elixir and OTP. You’ll begin with the immediately comfortable Elixir language syntax, along with just enough functional programming to use it effectively. Then, you’ll dive straight into several lighthearted examples that teach you to take advantage of the incredible functionality built into the OTP library.

What's inside

  • Covers Elixir 1.2 and 1.3
  • Introduction to functional concurrency with actors
  • Experience the awesome power of Erlang and OTP

About the reader

Written for readers comfortable with a standard programming language like Ruby, Java, or Python. FP experience is helpful but not required.

About the author

Benjamin Tan Wei Hao is a software engineer at Pivotal Labs, Singapore. He is also an author, a speaker, and an early adopter of Elixir.

  • combo $39.99 pBook + eBook
  • eBook $31.99 pdf + ePub + kindle

FREE domestic shipping on three or more pBooks

Engaging. Practical. Informative. Thumbs up!

Dane Balia, Hetzner

If you’ve never touched Elixir or Erlang before, this book will open the door to a new universe for you.

Thomas Peklak, Emakina CEE

Offers techniques and insights difficult or impossible to find anywhere else.

Kosmas Chatzimichalis, Mach7x