<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="86002" data-post-id="86002">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="namelos" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/namelos/120/9296_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  namelos
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Currently I’m following the <em>“to spawn or not to spawn”</em> approach – use functional and module deal with thought concerns, and processes deal with runtime concerns.</p>
<p>And try to use registry with name or id, instead of direct linking or storing pids. It’s pretty similar to the service discovery pattern over configuring ip addresses.</p>
<p>I believe it’s the safest and cleanest way of doing things currently. If you are trying to play your business on BEAM, this is absolutely recommended approach.</p>
<p>But from time to time I’m also wondering use wrap processes to an object-oriented language. Though it’s not applausable across the community, it’s might suitable to make something interesting – for example an entity system with runtime concerns. Maybe like when you define an object use the wrapper, you’ll get: supervision tree, default recover behaviour, type, struct, methods, and possibly graphQL type resolver for free. Or you can write custom view or read model on this entity. And it’s might quite suitable for things like fast prototyping.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="86002" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/structuring-an-otp-application/14786/23">Post #22</a>
	                </div>
	            </div>
              <div id="likers-container-86002" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="86002"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #22"></div>
  </section>
</div>
    <div class="postbit" id="86061" data-post-id="86061">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="jwarlander" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/jwarlander/120/1155_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  jwarlander
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Processes can definitely serve a few different purposes when designing an application, and sometimes it’s fun to just play around a bit, trying out a totally different way of doing things. Like implementing an OO approach with processes.. <img src="https://forum.elixirforum.com/images/emoji/apple/wink.png?v=15" title=":wink:" class="emoji" alt=":wink:" loading="lazy" width="20" height="20"></p>
<p>However, for me it tends to come down to a few core points:</p>
<h2><a name="p-86061-concurrency-1" class="anchor" href="#p-86061-concurrency-1" aria-label="Heading link" rel="nofollow"></a>Concurrency</h2>
<p>This is of course the first thing one tends to learn about the BEAM – concurrency is implemented by processes exchanging messages, after all. There’s no point in launching 2 million processes just because you can, however; try to stay close to the <em>natural concurrency</em> of the problem you’re solving.</p>
<p>If devices, end users, etc are connecting to your application, then that’s usually a good start – each of those probably need at least one process, for as long as they’re interacting. Do you have recurring tasks, cleanup tasks, etc? They probably also need their own processes. But.. each semi-involved task of some kind that’s initiated by a user of the system? It might not be warranted a process of its own; perhaps every user has a single “background worker”, effectively serializing tasks per user in order to democratize resources somewhat, making sure a single user can’t bog down the entire system..</p>
<h2><a name="p-86061-fault-isolation-2" class="anchor" href="#p-86061-fault-isolation-2" aria-label="Heading link" rel="nofollow"></a>Fault Isolation</h2>
<p>This one didn’t really “click” for me until I’d started learning Erlang a long time ago, and after reading a couple of books and trying some examples, decided that I needed to build something “for real”.</p>
<p>As I was toying around with a small online game prototype, juggling things like TCP connections, command processing, long-running environmental effects, room-based communication and navigation etc, I finally realized that processes are a <em>very powerful tool for fault isolation</em> – especially in combination with proper supervision trees.</p>
<p>I was used to C/C++ mostly at the time, where errors usually are a bit of an all or nothing affair; either you anticipate and handle the error where it occurs, or your whole application comes crashing down. Naturally you’ve got ways to carefully navigate around that, but it’s not easy either way.</p>
<p>With the BEAM, instead, we can start to think about what parts of our systems that can safely fail, and how we can best handle that. User input processing and TCP communication? That all happens in user-specific processes, and if something fails, we just need to make sure that we’ve structured our processes so that everything that <em>needs</em> to be stopped / restarted is properly linked.</p>
<p>As a consequence, you can start thinking about separating very simple, reliable <em>core parts</em> of your application into their own supervision tree(s), so that even if everything else comes crashing down, those parts will survive, either to make sure everything else is properly restarted again, or if nothing else, to <em>safely</em> shut down, perhaps persisting critical data etc.</p>
<h2><a name="p-86061-serialization-3" class="anchor" href="#p-86061-serialization-3" aria-label="Heading link" rel="nofollow"></a>Serialization</h2>
<p>Another important concern is if you have some part of you application that can’t handle concurrency at all; this can then be wrapped in a process that manages whatever it needs to do, allowing other processes to send messages at will but always confident that you’re processing them one at a time.</p>
<p>Naturally this can turn into a performance bottleneck, so it requires some careful thinking. If you’re writing to a transaction log, for example, perhaps it’s tolerable to keep the last few transactions in memory, only flushing to disk every now and then, in order to achieve better throughput. Yes, you risk losing some transactions - but depending on what you’re doing that may be perfectly acceptable.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="86061" data-batch-url="/posts/batch_likers">
                        5
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/structuring-an-otp-application/14786/24">Post #23</a>
	                </div>
	            </div>
              <div id="likers-container-86061" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="86061"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>