Chapter 5. Concurrent error-handling and fault tolerance with links, monitors, and processes

 

This chapter covers

  • Handling errors, Elixir style
  • Links, monitors, and trapping exits
  • Implementing a supervisor

Ever watched The Terminator, the movie about an assassin cyborg from the future (played by Arnold Schwarzenegger)? Even when the Terminator is shot multiple times, it just keeps coming back unfazed, over and over again. Once you’re acquainted with Elixir’s fault-tolerance features, you’ll be able to build programs that can handle errors gracefully and take corrective actions to fix the problems. (You won’t be able to build Skynet, though—at least, not yet.)

In sequential programs, there’s typically only one main process doing all the hard work. What happens if this process crashes? Usually, this means the entire program crashes. The normal approach is to program defensively, which means lacing the program with try, catch, and if err != nil.

The story is different when it comes to building concurrent programs. Because more than one process is running, it’s possible for another process to detect the crash and subsequently handle the error. Let that sink in, because it’s a liberating notion.

5.1. Links: ‘til death do us part

5.2. Monitors

5.3. Implementing a supervisor

5.4. A sample run (or, “Does it really work?”)

5.5. Summary

sitemap