<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	
	xmlns:georss="http://www.georss.org/georss"
	xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
	>

<channel>
	<title>otp « Plataformatec Blog</title>
	<atom:link href="/tag/otp/feed/" rel="self" type="application/rss+xml" />
	<link>/</link>
	<description>Plataformatec&#039;s place to talk about Ruby, Ruby on Rails, Elixir, and software engineering</description>
	<lastBuildDate>Thu, 12 Apr 2018 14:39:21 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.4.2</generator>
	<item>
		<title>Elixir, processes and this thing called OTP</title>
		<link>/2018/04/elixir-processes-and-this-thing-called-otp/</link>
					<comments>/2018/04/elixir-processes-and-this-thing-called-otp/#comments</comments>
		
		<dc:creator><![CDATA[Amanda Sposito]]></dc:creator>
		<pubDate>Wed, 11 Apr 2018 17:36:13 +0000</pubDate>
				<category><![CDATA[English]]></category>
		<category><![CDATA[elixir]]></category>
		<category><![CDATA[otp]]></category>
		<guid isPermaLink="false">/?p=7451</guid>

					<description><![CDATA[<p>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. And one of the things that comes up when learning about concurrency in Elixir is an acronym we hear a lot, called OTP. OTP The ... <a class="read-more-link" href="/2018/04/elixir-processes-and-this-thing-called-otp/">»</a></p>
<p>The post <a href="/2018/04/elixir-processes-and-this-thing-called-otp/">Elixir, processes and this thing called OTP</a> first appeared on <a href="/">Plataformatec Blog</a>.</p>]]></description>
										<content:encoded><![CDATA[<p><span style="font-weight: 400;">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. </span><span style="font-weight: 400;">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>
<h2><strong>OTP</strong></h2>
<p><span style="font-weight: 400;">The acronym stands for Open Telecom Platform, but it isn&#8217;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>
<p><span style="font-weight: 400;">Therefore, you may have heard the expression &#8220;OTP compliant&#8221;, which means that the application follows the system design principles established by Erlang/OTP.</span></p>
<p><span style="font-weight: 400;">One of the items in this set of principles is based on using process architecture to solve your application problems.</span></p>
<h2><strong>Processes</strong></h2>
<p><span style="font-weight: 400;">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&#8217;s cores, and because they are lighter, on average 2k, we can create thousands of them in our application.</span></p>
<p><span style="font-weight: 400;">They are isolated from each other and communicate through messages, thus helping us divide the workload and run concurrent things.</span></p>
<p><span style="font-weight: 400;">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&#8217;t have to worry about controlling its state using complex mechanisms to ensure this, things like <a href="https://en.wikipedia.org/wiki/Mutual_exclusion"><em>mutex</em></a> and <a href="https://en.wikipedia.org/wiki/Thread_(computing)"><em>threads</em></a> usage are no longer needed.</span></p>
<h2><strong>How do processes work in Elixir?</strong></h2>
<p><span style="font-weight: 400;">It is very common to associate processes and the message exchange between them with the <a href="https://en.wikipedia.org/wiki/Actor_model">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>
<p><span style="font-weight: 400;">Each process has a <em>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&#8217;t block processing waiting for a response.</span></p>
<p><span style="font-weight: 400;">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>
<p><img fetchpriority="high" decoding="async" class="size-large wp-image-7443 aligncenter" src="/wp-content/uploads/2018/04/sending-receiveing-messages-1024x765.png" alt="" width="1024" height="765" srcset="/wp-content/uploads/2018/04/sending-receiveing-messages-1024x765.png 1024w, /wp-content/uploads/2018/04/sending-receiveing-messages-300x224.png 300w, /wp-content/uploads/2018/04/sending-receiveing-messages-768x574.png 768w, /wp-content/uploads/2018/04/sending-receiveing-messages.png 1419w" sizes="(max-width: 1024px) 100vw, 1024px" /><span style="font-weight: 400;">It&#8217;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>
<p><span style="font-weight: 400;">When we have processes that monitor other processes, we name them <em>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>
<p><img decoding="async" class="aligncenter size-large wp-image-7444" src="/wp-content/uploads/2018/04/supervision-tree-1024x765.png" alt="" width="1024" height="765" srcset="/wp-content/uploads/2018/04/supervision-tree-1024x765.png 1024w, /wp-content/uploads/2018/04/supervision-tree-300x224.png 300w, /wp-content/uploads/2018/04/supervision-tree-768x574.png 768w, /wp-content/uploads/2018/04/supervision-tree.png 1415w" sizes="(max-width: 1024px) 100vw, 1024px" />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>
<h2><strong>How does this work for concurrency?</strong></h2>
<p><span style="font-weight: 400;">When BEAM starts, it also starts a thread named <em>Scheduler</em> that is responsible for running each process concurrently on the CPU.</span></p>
<p><span style="font-weight: 400;">To take full advantage of the hardware, BEAM starts a <em>Scheduler</em> for each available core, that is, a computer that has four cores will have four <em>schedulers</em>, each running several processes concurrently.</span></p>
<p><img decoding="async" class="aligncenter size-large wp-image-7446" src="/wp-content/uploads/2018/04/scheduler-1024x709.png" alt="" width="1024" height="709" srcset="/wp-content/uploads/2018/04/scheduler-1024x709.png 1024w, /wp-content/uploads/2018/04/scheduler-300x208.png 300w, /wp-content/uploads/2018/04/scheduler-768x532.png 768w, /wp-content/uploads/2018/04/scheduler.png 1432w" sizes="(max-width: 1024px) 100vw, 1024px" /></p>
<p><span style="font-weight: 400;">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&#8217;t have to worry about the implementation details of more primitive functions such as <em>spawn</em>, <em>send</em>, and <em>receive</em>.</span></p>
<p><span style="font-weight: 400;">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&#8217;t use these functions, instead, they use abstractions such as <em>Task, GenServer, and <em>Agent</em>. </em>But that is a topic for another post.</span></p>
<p>&nbsp;</p>
<p><em>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é Valim, and Bruce Tate. </em></p>
<p><span style="font-weight: 400;">Click the button below to get the first chapter of the book Adopting Elixir.</span></p>
<p><a style="background-color: #67af5b; border-radius: 3px; font-family: sans-serif; display: inline-block; padding: 5px 20px; margin-bottom: 50px; text-align: center; text-decoration: none; width: auto; color: #fff;" href="http://pages.plataformatec.com.br/chapter-three-adoption-stories" target="_blank" rel="noopener noreferrer">Download <strong>Adopting Elixir&#8217;s</strong> free chapter</a> <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p><p>The post <a href="/2018/04/elixir-processes-and-this-thing-called-otp/">Elixir, processes and this thing called OTP</a> first appeared on <a href="/">Plataformatec Blog</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>/2018/04/elixir-processes-and-this-thing-called-otp/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
	</channel>
</rss>
