The Little Elixir & OTP Guidebook
Benjamin Tan Wei Hao
  • MEAP began April 2015
  • Publication in September 2016 (estimated)
  • ISBN 9781633430112
  • 250 pages (estimated)
  • printed in black & white

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. Come rediscover the joy of programming with Elixir and remember how it feels like to be a beginner again.

Table of Contents detailed table of contents

1. Introduction

1.1. How is Elixir different from Erlang

1.1.1. Tooling

1.1.2. Ecosystem

1.2. Why Elixir and not X?

1.3. What is Elixir/OTP good for?

1.4. The Road Ahead

1.4.1. A Sneak Preview of OTP Behaviours

1.4.2. Distribution for Load-Balancing and Fault-Tolerance

1.4.3. Dialyzer and Type-Specifications

1.4.4. Property and Concurrency Testing

1.5. 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.7. Meet | >, the Pipe operator

2.8. Erlang Interoperability

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 Exactly?

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. Handling Synchronous Requests with handle_call /3

4.3.5. Accessing the Server State

4.3.6. Handling Asynchronous Requests with handle_cast /2

4.3.7. Stopping the Server and Cleaning Up

4.3.8. What Happens when Callbacks Return an Invalid Response?

4.3.9. Receiving Other Kinds of Messages

4.3.10. Process Registration

4.3.11. Reflecting on Chapter 3's Metex

4.4. Exercises

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. Just Enough ETS

6.3.7. Checking-out a Worker

6.3.8. Checking-in a Worker

6.3.9. 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. Slight Detour: Queues in Erlang

7.2.7. Back to Queuing Worker Processes

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 escript.build

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. Configuring Failover and Takeover

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. How does Dialyzer Work

10.2. Success Typings

10.3. Revealing Types in Elixir, Part I

10.4. Getting Started with Dialyzer

10.4.1. The PLT: The Persistent Lookup Table

10.4.2. Dialyxir

10.4.3. Building a PLT Table

10.5. Software Discrepancies that Dialyzer can Detect

10.5.1. Catching Type Errors

10.5.2. Wrong Use of Built-In Functions

10.5.3. Redundant Code

10.5.4. Type Errors in Guard Clauses

10.5.5. Tripping Up Dialyzer with Some Indirection

10.6. Type Specifications

10.6.1. Writing Type Specifications

10.7. Writing your own Types

10.7.1. Multiple Return Types and Bodiless Function Clauses

10.7.2. Revealing Types in Elixir, Part II

10.7.3. Back to Bug #5

10.8. Exercises

10.9. 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.3. Summary of Concuerror

11.4. Resources

11.5. Summary

Appendixes

Appendix A: Getting Erlang & Elixir

About the Technology

Elixir is a functional programming language built on the Erlang virtual machine. It combines the productivity and expressivity of Ruby with the concurrency and fault-tolerance of Erlang. Elixir makes full use of Erlang's powerful OTP library, which many developers consider the source of Erlang's greatness, so you can have mature, professional-quality functionality right out of the gate. Elixir's support for functional programming makes it a great choice for highly distributed event-driven applications like IoT systems.

What's inside

  • Learn Functional programming in Elixir
  • Concurrency and the Actor model
  • Experience the awesome power of the Erlang Virtual Machine and OTP
  • Build systems that are distributed and fault-tolerant
  • Learn to build applications with third-party libraries and the Phoenix web framework

About the reader

Written for readers comfortable with a standard programming language like Ruby, Java, or Python. Experience with Erlang or another functional programming language is helpful.

About the author

Benjamin Tan Wei Hao is a software engineer at Neo Innovation. Deathly afraid of being irrelevant, he is always trying to catch up on his ever-growing reading list. He has spoken at meetups and conferences, mostly about Ruby and Elixir. He blogs at benjamintan.io.


Manning Early Access Program (MEAP) Read chapters as they are written, get the finished eBook as soon as it’s ready, and receive the pBook long before it's in bookstores.
MEAP combo $39.99 pBook + eBook
MEAP eBook $31.99 pdf + ePub + kindle

FREE domestic shipping on three or more pBooks