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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I was a Java web programmer. IMHO, GenServer processes are somewhat like “service objects” or “controller objects” or simply Servlet objects (I don’t know much about the C# ecosystem, sorry). These objects share the same properties:</p>
<ul>
<li>Effectively singleton</li>
<li>Has an infinite lifespan</li>
<li>Stateless</li>
</ul>
<p>Well, GenServers don’t have to be stateless, but if it can be stateless, then you might be better just use a simple module with a bunch of pure functions in it (maybe not that pure if you need to access a database).</p>
<p>I personally believe that those Java objects mentioned above do not even have to be objects. Classes with only a bunch of static methods should work fine, too. The only reason that those objects are objects is for the purpose of mocking in testing.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="311000" 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/is-an-elixir-genserver-so-different-from-an-object-of-a-class/60290/22">Post #21</a>
	                </div>
	            </div>
              <div id="likers-container-311000" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="311000"
                     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 #21"></div>
  </section>
</div>
    <div class="postbit" id="311003" data-post-id="311003">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="jhogberg" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/jhogberg/120/24176_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  jhogberg
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Erlang Core Team</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>While you’re certainly free to use them only for what you described, a <code>GenServer</code> can model a specific instance among countless others, hold state, and be short lived all at once. I think they’re a bit too generic (by design) to nail down for a specific purpose. <img src="https://forum.elixirforum.com/images/emoji/apple/slight_smile.png?v=15" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="311003" data-batch-url="/posts/batch_likers">
                        6
                      </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/is-an-elixir-genserver-so-different-from-an-object-of-a-class/60290/23">Post #22</a>
	                </div>
	            </div>
              <div id="likers-container-311003" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="311003"
                     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="311015" data-post-id="311015">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="rvirding" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/rvirding/120/1409_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  rvirding
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Creator of Erlang</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="sodapopcan" data-post="3" data-topic="60290">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sodapopcan/48/34668_2.png" class="avatar"> sodapopcan:</div>
<blockquote>
<p>The key thing is to let go of the idea that modules and processes are in any way related. While they do work together, modules are indeed just stateless bags of functions, and processes are used to run those functions and store state. While an object has its state encapsulated within its instance and only the methods defined on its class can operate directly on that state, processes can call functions from any module and have its state altered by any of them. So a process is like an instance, yes, but there isn’t a single module that controls its state. The gen server pattern does indeed associate a single module with a process, but there is nothing forcing that—you could put functions in that same module that are called by unrelated processes. All <code>use GenServer</code> does is metaprogram some functions into your module that are called by a process.</p>
</blockquote>
</aside>
<p>I think that this is something that must definitely be stressed. There is no way that processes and modules are related or have some inherent connection. Any process can use any module and any module can be used by any process. So as someone later pointed out I can have functions in mu GenServer module which can be called by other modules or used by other GenServers or processes. This is not something you should be very careful if you do it but there is nothing stopping from doing it.</p>
<p>And remember GenServers are just normal processes, there is nothing special about them. It is the same with modules.</p>
<p>Actually Erlang/Elixir is a very egalitarian system, all processes are created equal and all modules are equal. <img src="https://forum.elixirforum.com/images/emoji/apple/smile.png?v=15" title=":smile:" class="emoji" alt=":smile:" loading="lazy" width="20" height="20"></p>
<p><strong>EDIT:</strong> I can for example kill any process in the system I want. The fail safe and unstoppable way of always crashing the system is:</p>
<blockquote>
<p>iex(1)&gt; init = Process.whereis(:init)<br>
<a class="hashtag-cooked" href="/tag/pid/2279" data-type="tag" data-slug="pid" data-id="2279" data-style-type="icon" data-icon="tag" rel="nofollow"><span class="hashtag-icon-placeholder"><svg class="fa d-icon d-icon-square-full svg-icon svg-node"><use href="#square-full"></use></svg></span><span>pid</span></a>&lt;0.0.0&gt;<br>
iex(2)&gt; Process.exit(init, :kill)<br>
System process &lt;0.0.0&gt; terminated: killed</p>
<p>Crash dump is being written to: erl_crash.dump…done</p>
</blockquote>
<p>And no, there is no way of prohibiting this!</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="311015" data-batch-url="/posts/batch_likers">
                        12
                      </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/is-an-elixir-genserver-so-different-from-an-object-of-a-class/60290/24">Post #23</a>
	                </div>
	            </div>
              <div id="likers-container-311015" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="311015"
                     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 #23"></div>
  </section>
</div>
    <div class="postbit" id="311019" data-post-id="311019">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I think the comparison to classes is very apt, but there is quite a difference in application.</p>
<aside class="quote no-group" data-username="derek-zhou" data-post="20" data-topic="60290">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/derek-zhou/48/19943_2.png" class="avatar"> derek-zhou:</div>
<blockquote>
<p>A GenServer is much heavier weight than an Object in a typical OO language; so if you try to literately translate an OO program into Elixir</p>
</blockquote>
</aside>
<p>While in “OOP” languages almost everything is a class, I find that in Elixir I don’t write GenServers that often.</p>
<p>You said you are going to be working on an API. While there will be a lot of processes (one for each request, etc.), in your code most of the time when you want state, you’ll also want persistence, so will probably be using a database. Or you want concurrent lookups and to avoid a process bottleneck, so will use ETS.</p>
<p>In Java, you are immediately writing classes, at least one per source file. In Elixir I don’t think I’ve written 10 GenServers.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="311019" data-batch-url="/posts/batch_likers">
                        3
                      </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/is-an-elixir-genserver-so-different-from-an-object-of-a-class/60290/25">Post #24</a>
	                </div>
	            </div>
              <div id="likers-container-311019" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="311019"
                     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 #24"></div>
  </section>
</div>
    <div class="postbit" id="311022" data-post-id="311022">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="adamu" data-post="25" data-topic="60290">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/adamu/48/31482_2.png" class="avatar"> adamu:</div>
<blockquote>
<p>Or you want concurrent lookups and to avoid a process bottleneck, so will use ETS.</p>
</blockquote>
</aside>
<p>One thing I do in this regard is use a GenServer process to own the ETS table and to process writes which I actually want serialized and strictly managed (such writes are rare).  Reads of ETS, however, are not processed by the owning GenServer and are made via direct query to the ETS table.  Naturally, the GenServer and ETS reads are handled via a module defining API functions so users of such services are blissfully unaware that they’re ultimately talking to a GenServer, an ETS table, etc. but this shows a certain use of GenServers in the ETS using context.</p>
<p>A broader comment is that I’m always thinking of GenServers as really a runtime “service” mechanism more than a way to implement logic.  The GenServers I use (whether they’re defined by a library or I’ve written them) are not really carriers of much logic beyond mediating the processing of requests and dispatching those requests to logic defined elsewhere.  I’m also thinking about what failure modes might exist and how things restart (supervision trees, etc.)… again those are primarily worries of runtime mechanics and not business logic around processing data.</p>
<p>My perspective anyway.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="311022" data-batch-url="/posts/batch_likers">
                        3
                      </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/is-an-elixir-genserver-so-different-from-an-object-of-a-class/60290/26">Post #25</a>
	                </div>
	            </div>
              <div id="likers-container-311022" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="311022"
                     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 #25"></div>
  </section>
</div>
    <div class="postbit" id="311112" data-post-id="311112">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="rvirding" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/rvirding/120/1409_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  rvirding
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Creator of Erlang</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Two main reasons for accessing ETS tables through processes, GenServers or others, are transactions and restricting access. A reason is that ETS has very llimited built-in transactions, basically all you have is updating counters. If you need to do a sequence of operations on an ETS table you need to do through a single process, and limit access to that table for other processes. Maybe allow many processes to read the table but only one process to write it.</p>
<p>Another reason is if you need for the data in a table to be kept secret. In that case you only allow to owning process to access it and if that process crashes then the table is automatically deleted. This is one way of keeping secret data of keeping data secret inside a process.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="311112" data-batch-url="/posts/batch_likers">
                        7
                      </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/is-an-elixir-genserver-so-different-from-an-object-of-a-class/60290/27">Post #26</a>
	                </div>
	            </div>
              <div id="likers-container-311112" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="311112"
                     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>