Mastering Elixir

Book description

Leverage the power of Elixir programming language to solve practical problems associated with scalability, concurrency, fault tolerance, and high availability.

Key Features

  • Enhance your Elixir programming skills using its powerful tools and abstractions
  • Discover how to develop a full-fledged file server
  • Understand how to use Phoenix to create a web interface for your application.

Book Description

Running concurrent, fault-tolerant applications that scale is a very demanding responsibility. After learning the abstractions that Elixir gives us, developers are able to build such applications with inconceivable low effort. There is a big gap between playing around with Elixir and running it in production, serving live requests. This book will help you fll this gap by going into detail on several aspects of how Elixir works and showing concrete examples of how to apply the concepts learned to a fully fledged application. In this book, you will learn how to build a rock-solid application, beginning by using Mix to create a new project. Then you will learn how the use of Erlang's OTP, along with the Elixir abstractions that run on top of it (such as GenServer and GenStage), that allow you to build applications that are easy to parallelize and distribute. You will also master supervisors (and supervision trees), and comprehend how they are the basis for building fault-tolerant applications. Then you will use Phoenix to create a web interface for your application. Upon fnishing implementation, you will learn how to take your application to the cloud, using Kubernetes to automatically deploy, scale, and manage it. Last, but not least, you will keep your peace of mind by learning how to thoroughly test and then monitor your application.

What you will learn

  • Use Elixir tools, including IEx and Mix
  • Find out how an Elixir project is structured and how to create umbrella applications
  • Discover the power of supervision trees, the basis for fault-tolerance
  • Create a Domain-Specifc Language (DSL) that abstracts complexity
  • Create a blazing-fast web interface for your application with Phoenix
  • Set up an automatic deployment process for the cloud
  • Monitor your application and be warned if anything unexpected happens

Who this book is for

Mastering Elixir is for you if you have experience in Elixir programming and want to take it to the next level. This Elixir book shows you how to build, deploy, and maintain robust applications, allowing you to go from tinkering with Elixir on side projects to using it in a live environment. However, no prior knowledge of Elixir is required to enjoy the complex topics covered in the book.

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Mastering Elixir
  3. Dedication
  4. Packt Upsell
    1. Why subscribe?
    2. PacktPub.com
  5. Contributors
    1. About the authors
    2. About the reviewer
    3. Packt is searching for authors like you
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Conventions used
    4. Get in touch
      1. Reviews
  7. Preparing for the Journey Ahead
    1. Why functional programming?
    2. Elixir and Erlang
    3. Elixir's data types
      1. Integers
      2. Floats
      3. Atoms
      4. Booleans
      5. Tuples
      6. Lists
      7. Maps
      8. Binaries
      9. Strings and charlists
      10. Other types
        1. Keyword lists
        2. Ranges
        3. MapSets
    4. Pattern matching
      1. Pattern matching on tuples
      2. Pattern matching on lists
      3. Pattern matching on maps
      4. Pattern matching on binaries and strings
    5. Functions and Modules
      1. Anonymous functions
      2. Modules and Named Functions
      3. Module attributes, directives, and uses
    6. Working with collections
      1. Looping through recursion
      2. Eager processing with the Enum module
        1. Comprehensions
      3. Lazy processing with the stream module
    7. Control flow
      1. if and unless
      2. cond
      3. case
      4. with
      5. Exceptions
    8. Typespecs and behaviours
      1. Typespecs
        1. Dialyzer
      2. Behaviours
    9. Protocols
      1. Structs
      2. Bringing structs and protocols together
    10. Tooling and ecosystems
      1. IEx
      2. Mix
      3. ExUnit
      4. Erlang interoperability
    11. Summary
  8. Innards of an Elixir Project
    1. Elixir applications
    2. Elixir project structure
      1. Project dependencies
    3. Umbrella projects
    4. ElixirDrip – our showcase application
      1. Using behaviours
      2. Viewing cross references with xref
    5. Adopting a consistent coding style
    6. Summary
  9. Processes – The Bedrock of Concurrency and Fault Tolerance
    1. Inside the BEAM
    2. Working with processes
      1. Creating processes
      2. Message passing between processes
      3. Building a cache worker
    3. Detecting errors by linking and monitoring processes
      1. Using links
      2. Using monitors
    4. Recovering from errors with supervisors and supervision trees
      1. Defining and starting a supervisor
        1. Streamlined child specs
      2. Starting children at runtime using dynamic supervisors
      3. Minimizing error effects with supervision trees
    5. Summary
  10. Powered by Erlang/OTP
    1. GenServer
      1. GenServer at your service
      2. Implementing a CacheWorker with GenServer
      3. Agent
    2. Task
      1. Parallel tasks
    3. Using (D)ETS
      1. Disk-based ETS
    4. Registry
    5. Summary
  11. Demand-Driven Processing
    1. GenStage
      1. The upload pipeline
        1. The notifier stage
        2. The RemoteStorage stage
        3. The Encryption stage
        4. The Starter stage
      2. The download pipeline
        1. The Notifier stage
        2. The Encryption stage
        3. The RemoteStorage stage
        4. The Starter stage
        5. Final remarks
    2. Flow
      1. Sample data
      2. Lazy queries
      3. Disk-usage rank query
    3. Summary
  12. Metaprogramming – Code That Writes Itself
    1. The abstract syntax tree
    2. Tinkering with macros
      1. Hygienic accesses
      2. Famous (un)quotes
        1. Macros – a silver bullet?
    3. Extending modules
      1. Using the use and __using__/1 macros
    4. Creating a domain-specific language
      1. Registering module attributes
      2. Collecting a list of pipeline steps
      3. Generating worker specs and subscription options
        1. Producer stage
        2. ProducerConsumer and Consumer stages
        3. Collecting the pipeline worker specs
        4. Defining the supervisor functions
      4. Streamlining GenStage modules
        1. Simpler pipeline producers
        2. Simpler pipeline (producer) consumers
    5. Macros cheat sheet
    6. Summary
  13. Persisting Data Using Ecto
    1. Connecting to the database
    2. Schemas and migrations
      1. Schemas
        1. Custom field types
        2. Users or media owners?
        3. Schema or not – that is the question
      2. Migrations
      3. Relationships
    3. Changesets
      1. Media
      2. Users
      3. Media ownership
      4. Constraints
    4. Queries
      1. Media folder queries
      2. Loading schemas and their associations
      3. Queries with raw SQL
      4. Finding top users with aggregates
    5. Summary
  14. Phoenix – A Flying Web Framework
    1. The Plug specification
      1. Creating a module plug
      2. Creating a function plug
    2. Routing requests
    3. Handling requests in a controller
    4. Rendering views
      1. Layouts
    5. Authenticating users
    6. Implementing a JSON API
      1. Authenticating users in the API
    7. Interactive applications using channels
      1. Preparing the server
      2. Joining channels on the client
      3. Exchanging events
      4. Authenticating socket connections
      5. Tracking users statuses using Presence
    8. Summary
  15. Finding Zen through Testing
    1. Unit testing
      1. Testing functions without side-effects
      2. Testing functions with side-effects
        1. Creating mocks with Mox
        2. Testing interactions with the repository separately
      3. Testing the documentation
    2. Integration testing
    3. Testing Phoenix Channels
    4. Testing macros
    5. Property-based testing
    6. Summary
  16. Deploying to the Cloud
    1. Releasing with Distillery
      1. Configuring the release
        1. Interpolating environment variables
      2. Creating the release
      3. Creating a custom release task
    2. Containerizing our application
      1. Creating a development container
      2. Orchestrating more than one container
      3. Composing the deployment containers
    3. Deploying to Kubernetes
      1. Configuring the cloud database
      2. Creating a namespace
      3. Creating secrets
      4. Publishing the production image
      5. Deploying your first pod
        1. Creating a Kubernetes job
      6. Exposing your pods to the world
    4. Continuous deployment with Travis CI
    5. Connecting the deployed Elixir nodes
      1. Testing the connected nodes
    6. Summary
  17. Keeping an Eye on Your Processes
    1. Collecting metrics
      1. Exposing Prometheus metrics
      2. Creating custom Prometheus metrics
      3. Local Prometheus server
      4. Deploying Prometheus in Kubernetes
      5. Calculating percentiles
        1. Setting Grafana variables
    2. A window to your nodes
      1. Connecting to a containerized node
      2. Connecting to an Erlang node running in Kubernetes
        1. Using a remote shell
    3. Inspecting application behavior
      1. Profiling with the fprof profiler
      2. Tracing with the :dbg module
    4. Summary
  18. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Mastering Elixir
  • Author(s): Andre Albuquerque, Daniel Caixinha
  • Release date: July 2018
  • Publisher(s): Packt Publishing
  • ISBN: 9781788472678