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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I’m not sure I get the example. Why would you not just use Phoenix Channels?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="31052" data-batch-url="/posts/batch_likers">
                        0
                      </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/looking-for-clarity-around-using-agent/4750/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-31052" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="31052"
                     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 #11"></div>
  </section>
</div>
    <div class="postbit" id="31053" data-post-id="31053">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>We’re talking about barebones Elixir here. The topic started about Agents - but they really are best understood in the context of the concurrency primitives they are based on and the (more commonly used) counterpart GenServer.</p>
<aside class="quote no-group" data-username="wktdev" data-post="4" data-topic="4750">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/wktdev/48/17649_2.png" class="avatar"> wktdev:</div>
<blockquote>
<p>I think a perfect example to help someone like me to understand agents would be a minimalist terminal chat application that uses them.</p>
</blockquote>
</aside>
<p>The primary objective is Agents (and it’s relatives); the chat application was merely an example that was asked for in this context (admittedly far from a great one for Agents).</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="31053" data-batch-url="/posts/batch_likers">
                        1
                      </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/looking-for-clarity-around-using-agent/4750/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-31053" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="31053"
                     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 #12"></div>
  </section>
</div>
    <div class="postbit" id="31055" data-post-id="31055">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Got it. It does seem like a misuse of agents IMHO.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="31055" data-batch-url="/posts/batch_likers">
                        1
                      </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/looking-for-clarity-around-using-agent/4750/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-31055" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="31055"
                     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 #13"></div>
  </section>
</div>
    <div class="postbit" id="31139" data-post-id="31139">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I’m going to clarify how I use Agents based on some of the other posts that came after mine. I’ve been hearing a lot of noise about Agents being misused or that they provide not value. At least that’s my take away from various source I can’t remember.</p>
<p>I use Agents mostly as a key/value storage solution. In most cases I could have used ETS to accomplish the same. And from what I’ve read, the performance would probably be better. However, I probably use mostly out of laziness. I just find it hard to remember Erlang’s (IMHO cryptic) syntax. I did find that using an Agent came in handy once when I had to extend an existing Agent based module for a port reuse strategy.</p>
<p>In this case, I had a general Agent handling long lived state (but not requiring vm restart persistence) in a pure Elixir application (no database). I needed to later add a port  reuse strategy. This means, reading the data store, checking to see if a port was available for reuse. So this required reads and updates that that were concurrency safe.</p>
<p>I could have moved the whole design over to a GenServer, but instead, I just wrapped the code in an <code>Agent.get_and_update</code>. That was a few years ago. The implementation has been reused on another project since then. I have not has an issue with the design on hundreds of the products sold.</p>
<p>Personally, I would not use an Agent for logic any more complicated then that. If you need behaviour, I much prefer using either a GenServer or a GenFSM.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="31139" data-batch-url="/posts/batch_likers">
                        0
                      </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/looking-for-clarity-around-using-agent/4750/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-31139" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="31139"
                     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 #14"></div>
  </section>
</div>
    <div class="postbit" id="31145" data-post-id="31145">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="smpallen99" data-post="15" data-topic="4750">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/smpallen99/48/1157_2.png" class="avatar"> smpallen99:</div>
<blockquote>
<p>I’ve been hearing a lot of noise about Agents being misused or that they provide not value.</p>
</blockquote>
</aside>
<p>My personal issue with Agents is that they depend on “functions as the message payload” - I’m all for “functions being first class citizens” within a process but I get a bit queasy when functions start moving between processes especially as those processes could be on two different nodes.</p>
<ul>
<li>By accepting functions in messages the process is essentially giving up the autonomy to manage its own state - it’s completely at the mercy of whatever decides to send it a function, potentially <em>risking</em> its state that other processes are depending on.</li>
<li>If the sent function references any application modules - those dependencies better be well in sync on the nodes that the message/function is travelling between.</li>
</ul>
<p>I feel that the functions that manipulate process state should be collocated with the process (and the state), so I’d go with a GenServer even if it is a bit more work.</p>
<p>As “cool” as it is to be able to execute functions in an atomic fashion on a state that is potentially “conceptually shared” between multiple processes, I’d much rather have those functions be an integral part of the process. I guess that makes me a <a href="http://serviceorientation.com/serviceorientation/service_autonomy" rel="noopener nofollow ugc">process-autonomy</a> advocate.<br>
<img src="https://forum.elixirforum.com/uploads/default/original/2X/4/425cf8a2af1868cc834c0a12b6f0c9ef0e530de9.gif?v=15" title=":icon_biggrin:" class="emoji emoji-custom only-emoji" alt=":icon_biggrin:" 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="31145" 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/looking-for-clarity-around-using-agent/4750/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-31145" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="31145"
                     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 #15"></div>
  </section>
</div>
    <div class="postbit" id="31154" data-post-id="31154">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Some interesting points <a class="mention" href="/u/peerreynders" rel="nofollow">@peerreynders</a>. To be honest, all my Elixir projects over the last 3 years have all been single node solutions. Busy ones, but all single node, mainly enterprise telephony relates. I have played around with multi node, but nothing commercially. With that said, I have not given much thought to the distribution aspect of Agents.</p>
<p>Also, when I design a module with an Agent, I typically add the api to the module. So the consumers are calling an API with data. In only one instance did I expose an interface where the consumer passes their own <code>fn</code>. And I wasn’t very comfortable with the design.</p>
<p>I am, however, staring to see the pain points around using Agents, especially for the inexperienced. Its been an informative discussion for me.</p>
<p>As for Agent vs GenServer, I guess I save a few lines of code. Basically its 1 function / API call for Agent vs 2 for GenServer. And not even that if I used something like <code>exactor</code> which I’ve pretty well abandoned these days.</p>
<p>I’m almost convinced to abandon Agents in favour of GenServers <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> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="31154" 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/looking-for-clarity-around-using-agent/4750/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-31154" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="31154"
                     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>