{"id":7451,"date":"2018-04-11T14:36:13","date_gmt":"2018-04-11T17:36:13","guid":{"rendered":"http:\/\/blog.plataformatec.com.br\/?p=7451"},"modified":"2018-04-12T11:39:21","modified_gmt":"2018-04-12T14:39:21","slug":"elixir-processes-and-this-thing-called-otp","status":"publish","type":"post","link":"http:\/\/blog.plataformatec.com.br\/2018\/04\/elixir-processes-and-this-thing-called-otp\/","title":{"rendered":"Elixir, processes and this thing called OTP"},"content":{"rendered":"

One of the notable features provided by the Elixir language is the way it handles concurrency, how this is beneficial on a daily basis and adds value to the final software.\u00a0<\/span>And one of the things that comes up when learning about concurrency in Elixir is an acronym we hear a lot, called OTP.<\/span><\/p>\n

OTP<\/strong><\/h2>\n

The acronym stands for Open Telecom Platform, but it isn’t used just for telecom nowadays. In the book Designing for Scalability with Erlang\/OTP, written by authors Francesco Cesarini and Steve Vinoski, they define OTP as three key components that interact with each other: the first one is Erlang itself, the second is a set of libraries available with the virtual machine and the third is a set of system design principles.<\/span><\/p>\n

Therefore, you may have heard the expression “OTP compliant”, which means that the application follows the system design principles established by Erlang\/OTP.<\/span><\/p>\n

One of the items in this set of principles is based on using process architecture to solve your application problems.<\/span><\/p>\n

Processes<\/strong><\/h2>\n

When we talk about process in Elixir, we are referring to the Erlang virtual machine processes, not the operating system processes. BEAM (Erlang virtual machine) processes are much lighter and cheaper than operating system processes and run in all available machine’s cores, and because they are lighter, on average 2k, we can create thousands of them in our application.<\/span><\/p>\n

They are isolated from each other and communicate through messages, thus helping us divide the workload and run concurrent things.<\/span><\/p>\n

Elixir being a functional language, one of its features is immutability, which helps us to express explicit state. Hence, when we separate our code into independent tasks that can run concurrently, we don’t have to worry about controlling its state using complex mechanisms to ensure this, things like mutex<\/em><\/a> and threads<\/em><\/a> usage are no longer needed.<\/span><\/p>\n

How do processes work in Elixir?<\/strong><\/h2>\n

It is very common to associate processes and the message exchange between them with the actor model for concurrency<\/a>. This happens because each process in Elixir is independent and completely isolated from one another. A process can save state, but this is not shared, and the only way to share something among processes is by sending messages.<\/span><\/p>\n

Each process has a mailbox<\/em> that, as the name suggests, is responsible for receiving messages from other processes. It is also worth mentioning that when we send a message, everything happens asynchronously, so we don’t block processing waiting for a response.<\/span><\/p>\n

One way to think about this is to imagine processes like cell phones that use SMSs to exchange information, as each cell phone has a place to store those messages until they are handled, and this happens in an independent and isolated manner.<\/span><\/p>\n

\"\"It’s worth mentioning that we can also link a process to another. This is important because, based on this concept, we can identify faults and act to deal with them.<\/span><\/p>\n

When we have processes that monitor other processes, we name them Supervisor<\/em>. We can have several of these in our application and when we have more than one Supervisor monitoring processes we call this a Supervision Tree.<\/span><\/p>\n

\"\"This is very important as it enables us to provide fault tolerance, because when we have a problem with our code, the last thing we want is that it affects the end user. By monitoring processes, we can identify when something unexpected happens and work on it, terminating the process with an issue and restarting it, thus, the process returns to its initial state, giving us time to act and solve the issue that caused the error.<\/p>\n

How does this work for concurrency?<\/strong><\/h2>\n

When BEAM starts, it also starts a thread named Scheduler<\/em> that is responsible for running each process concurrently on the CPU.<\/span><\/p>\n

To take full advantage of the hardware, BEAM starts a Scheduler<\/em> for each available core, that is, a computer that has four cores will have four schedulers<\/em>, each running several processes concurrently.<\/span><\/p>\n

\"\"<\/p>\n

Processes are the foundation for the concurrency model we use in Elixir. Many of the features we need when using processes have some abstraction to help us, therefore, we don’t have to worry about the implementation details of more primitive functions such as spawn<\/em>, send<\/em>, and receive<\/em>.<\/span><\/p>\n

When we use these primitive functions, we need to worry about several additional error-prone details to achieve our goal and ensure our code is OTP compliant. Usually, developers don’t use these functions, instead, they use abstractions such as Task, GenServer, and Agent<\/em>. <\/em>But that is a topic for another post.<\/span><\/p>\n

 <\/p>\n

Learn more about how companies have successfully adopted Elixir, and what are the best practices they have. Dedicated to the ones who are adopting Elixir or planning to do it, Adopting Elixir is a book written by Ben Marx, Jos\u00e9 Valim, and Bruce Tate. <\/em><\/p>\n

Click the button below to get the first chapter of the book Adopting Elixir.<\/span><\/p>\n

Download Adopting Elixir’s<\/strong> free chapter<\/a> \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"

One of the notable features provided by the Elixir language is the way it handles concurrency, how this is beneficial on a daily basis and adds value to the final software.\u00a0And one of the things that comes up when learning about concurrency in Elixir is an acronym we hear a lot, called OTP. OTP The … \u00bb<\/a><\/p>\n","protected":false},"author":67,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[1],"tags":[143,288],"aioseo_notices":[],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/posts\/7451"}],"collection":[{"href":"http:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/users\/67"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/comments?post=7451"}],"version-history":[{"count":10,"href":"http:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/posts\/7451\/revisions"}],"predecessor-version":[{"id":7463,"href":"http:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/posts\/7451\/revisions\/7463"}],"wp:attachment":[{"href":"http:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/media?parent=7451"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/categories?post=7451"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/tags?post=7451"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}