The Little Elixir & OTP Guidebook
Benjamin Tan Wei Hao
  • MEAP began April 2015
  • Publication in Fall 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. Syntax

1.1.2. Tooling

1.2. Why Elixir and not X?

1.3. What is Elixir/OTP good for?

1.4. Summary

2. A Whirlwind Tour

2.1. First Steps

2.1.1. Running an Elixir program in Interactive Elixir

2.1.2. Stopped an Elixir program

2.1.3. Getting Help

2.2. Data Types

2.2.1. Modules, Functions and Function Clauses

2.2.2. Numbers

2.2.3. Strings

2.2.4. Atoms

2.2.5. Tuples

2.2.6. Maps

2.3. Guards

2.4. Pattern Matching

2.5. Lists

2.6. Meet |>, the Pipe operator

2.7. Erlang Interoperability

2.8. Exercises

2.9. Summary

3. Processes 101

3.1. Actor Concurrency Model

3.2. Building a Weather application

3.2.1. The Naive 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 Message

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. The Completed Metex

4.3.11. Process Registration

4.3.12. 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 Reactions 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.2.8. 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.3. An Overview of Blitzy, the Load Tester

8.4. Let the Mayhem Begin!

8.4.1. Implementing the Worker Process

8.4.2. Running the Worker

8.5. Introducing Tasks

8.6. Onward to Distribution!

8.6.1. Location Transparency

8.6.2. An Elixir Node

8.6.3. Creating a Cluster

8.6.4. Connecting Nodes

8.6.5. Node Connections Are Transitive

8.7. Remotely Executing Functions

8.8. Making Blitzy Distributed

8.8.1. Creating a command line interface

8.8.2. Parsing input arguments with OptionParser

8.8.3. Connecting to the Nodes

8.8.4. Supervising Tasks with Tasks.Supervisor

8.8.5. Using a Task Supervisor

8.8.6. Creating the binary with mix escript.build

8.8.7. Running Blitzy!

8.9. Summary

9. Distribution and Fault Tolerance

9.1. Distribution for Fault Tolerance

9.1.1. An Overview of Chucky the Chuck Norris Facts Application

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. Summary

10. Building a Client Application

11. Distributed Elixir

Appendixes

Appendix A: Getting Erlang & Elixir

Appendix B: Where do we go from here?

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