Elixir in Action
Format: PDF / Kindle (mobi) / ePub
Elixir in Action teaches you to apply the new Elixir programming language to practical problems associated with scalability, concurrency, fault tolerance, and high availability.
mostly for didactic purposes. In reality, casts have a drawback: you don’t know what happened with your request. This in turn means you may be giving false responses to end users, as illustrated in figure 11.3. Figure 11.3. Using casts reduces the certainty of your responses. Because you use a cast to add a to-do entry in your system, you have no way of knowing what happened with your request. So when you’re telling the end user that you succeeded, this is a guess rather than a truthful
and web server persistent state persisting data encoding and decoding data overview process bottleneck bypassing process handling requests concurrently limiting concurrency with pooling reading from database storing in database Phoenix web framework pid, 2nd pin operator ( ^ ) pipeline operator ( |> ), 2nd, 3rd, 4th Plug library poolboy pooling port identifier ports Profiler module profiling tools project environment pry Q question mark ( ? ), 2nd R raising errors
Similarly, you can define modules in the shell: Again, the bytecode isn’t saved to the disk in this case. Pure Erlang modules You’ve already seen how to call a function from a pure (non-Elixir) Erlang module. Let’s talk a bit about this syntax: In Erlang, modules also correspond to atoms. Somewhere on the disk is a file named code.beam that contains the compiled code of the :code module. Erlang uses simple filenames, which is the reason for this call syntax. But the rules are the same as
the result and call the function recursively. Next, let’s look at how to compute something in a loop and return the result. You’ve already seen this example when dealing with conditionals, but let’s repeat it. The following code implements a function that sums all the elements in a given list. Listing 3.8. Calculating the sum of the list (sum_list.ex) defmodule ListHelper do def sum(), do: 0 def sum([head | tail]) do head + sum(tail) end end This code looks very declarative: The
operations must be synchronized on individual items. Each item is stored in its own corresponding file, and you shouldn’t allow multiple concurrent read/writes of the same item. Thus this approach won’t work in this case. Handling requests concurrently Another option is to keep the database process and make it handle database operations concurrently. This is useful when requests depend on a common state but can be handled independently. The idea is illustrated in figure 7.3. Figure 7.3.