Maintaining Composition Through Uncertainty

An introduction to some setbacks we may encounter along the way, along with the techniques used to fix them.

Encountering setbacks

When working with data, a couple of things could go wrong:

  • External services such as databases or network requests can unexpectedly slow down or fail.

  • Well-meaning users can make mistakes.

  • Malicious users can try to cause mischief with inputs shaped to attack our systems.

Though Elixir programmers depend on pipes, dealing with errors midstream in piped compositions is awkward and unreliable. As we build our boundary, we will need strategies for maintaining a composable architecture through this uncertainty.

Thinking differently

You may have noticed that we make heavy use of Elixir’s |> operator. Often, pipes rely on functions we expect to succeed. Since the boundary can’t rely on this kind of certainty, we need to adopt new techniques.

Because our services may struggle under load, we may need to use back pressure, a technique to slow requests to those services under duress. Since the data will come from untrusted sources, we must consider validations as we wrap our functional core in an API.

Adopting new techniques

Before we get back into the Mastery project we’re building to take complex quizzes, let’s address some of the techniques we might use to smooth out our boundary despite all that uncertainty. We’ll work with the following:

  • Errors as data

  • with

Both of these will let us compose with functions that might fail.


Get hands-on with 1200+ tech skills courses.