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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="OvermindDL1" data-post="61" data-topic="4214">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/overminddl1/48/2677_2.png" class="avatar"> OvermindDL1:</div>
<blockquote>
<p>Huh, I had no issues with GenServer either when I hit erlang a decade ago, and I was heavy C++ for over a decade by then.  Call’s have reply messages.  Casts are fire-and-forget.  Etc… etc…  It all seemed pretty obvious?</p>
</blockquote>
</aside>
<p>For you? Sure, it seems it was the case. <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"> For everyone, I highly doubt.</p>
<aside class="quote no-group" data-username="OvermindDL1" data-post="61" data-topic="4214">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/overminddl1/48/2677_2.png" class="avatar"> OvermindDL1:</div>
<blockquote>
<p>If they can use the process dictionary at all then they should not be using agents and instead should be threading their state through their process calls, like they are ‘already’ doing with the Agents PID.</p>
</blockquote>
</aside>
<p>We are talking about misuses of Agents. So yes, they should not be using agents nor the process dictionary. I was wondering which one is less problematic.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="29620" 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/discussion-about-uses-for-agent-processes/4214/62">Post #61</a>
	                </div>
	            </div>
              <div id="likers-container-29620" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="29620"
                     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 #61"></div>
  </section>
</div>
    <div class="postbit" id="29623" data-post-id="29623">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I have never used an Agent either.</p>
<p>To me, “Agent” is a bit of a misnomer. “Agent” coveys to me a sense of <strong>doing</strong>, rather than <strong>holding</strong>.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="29623" 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/discussion-about-uses-for-agent-processes/4214/63">Post #62</a>
	                </div>
	            </div>
              <div id="likers-container-29623" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="29623"
                     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 #62"></div>
  </section>
</div>
    <div class="postbit" id="29629" data-post-id="29629">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="sasajuric" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sasajuric/120/991_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  sasajuric
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Author of Elixir In Action</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote group-livebook_core_team" data-username="josevalim" data-post="60" data-topic="4214">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/josevalim/48/1787_2.png" class="avatar"> josevalim:</div>
<blockquote>
<p>GenServer took me quite some time to master (especially in understand all of the trade-offs regarding call, cast, termination, etc).</p>
</blockquote>
</aside>
<p>Call/cast dilemma also needs to be understood with agents (because there’s <code>cast</code> function).</p>
<p>Thanks to Elixir, some <code>GenServer</code> aspects can be ignored initially (I bet you wished there was Elixir when you started learning Erlang <img src="https://forum.elixirforum.com/images/emoji/apple/stuck_out_tongue.png?v=15" title=":stuck_out_tongue:" class="emoji" alt=":stuck_out_tongue:" loading="lazy" width="20" height="20">). With <code>use GenServer</code> basic usage requires <code>start_link</code>, <code>call</code>, and <code>cast</code> (and respective callbacks). In fact, a simple explanation could just stick with a <code>call</code>, and that’s already in many ways on-par with agents.</p>
<aside class="quote group-livebook_core_team" data-username="josevalim" data-post="60" data-topic="4214">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/josevalim/48/1787_2.png" class="avatar"> josevalim:</div>
<blockquote>
<p>Here is something that just came to my mind: if we didn’t have agents, those folks would likely end-up just using the process dictionary, because that’s what happens in Erlang to a certain extend. And I am not quite sure if that is better or worse.</p>
</blockquote>
</aside>
<p>If we’re talking about misuses, I think that procdict is a lesser evil here because:</p>
<ol>
<li>It gives better runtime properties (no copying, scheduler overhead, potentially leaking processes)</li>
<li>procdict should be a stronger hint that you’re probably doing something wrong <img src="https://forum.elixirforum.com/images/emoji/apple/smiley.png?v=15" title=":smiley:" class="emoji" alt=":smiley:" loading="lazy" width="20" height="20"></li>
</ol> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="29629" 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/discussion-about-uses-for-agent-processes/4214/64">Post #63</a>
	                </div>
	            </div>
              <div id="likers-container-29629" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="29629"
                     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 #63"></div>
  </section>
</div>
    <div class="postbit" id="29634" data-post-id="29634">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="sasajuric" data-post="64" data-topic="4214">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sasajuric/48/991_2.png" class="avatar"> sasajuric:</div>
<blockquote>
<p>If we’re talking about misuses, I think that procdict is a lesser evil here because:</p>
<ol>
<li>It gives better runtime properties (no copying, scheduler overhead, potentially leaking processes)</li>
<li>procdict should be a stronger hint that you’re probably doing something wrong <img src="https://forum.elixirforum.com/images/emoji/apple/smiley.png?v=15" title=":smiley:" class="emoji" alt=":smiley:" loading="lazy" width="20" height="20"></li>
</ol>
</blockquote>
</aside>
<p>Good points, thanks! <img src="https://forum.elixirforum.com/images/emoji/apple/smiley.png?v=15" title=":smiley:" class="emoji" alt=":smiley:" 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="29634" 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/discussion-about-uses-for-agent-processes/4214/65">Post #64</a>
	                </div>
	            </div>
              <div id="likers-container-29634" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="29634"
                     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 #64"></div>
  </section>
</div>
    <div class="postbit" id="29661" data-post-id="29661">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="sasajuric" data-post="64" data-topic="4214">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sasajuric/48/991_2.png" class="avatar"> sasajuric:</div>
<blockquote>
<p>procdict should be a stronger hint that you’re probably doing something wrong <img src="https://forum.elixirforum.com/images/emoji/apple/smiley.png?v=15" title=":smiley:" class="emoji" alt=":smiley:" loading="lazy" width="20" height="20"></p>
</blockquote>
</aside>
<p>Just on this topic, are there any guidelines on when using the procdict is actually appropriate?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="29661" 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/discussion-about-uses-for-agent-processes/4214/66">Post #65</a>
	                </div>
	            </div>
              <div id="likers-container-29661" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="29661"
                     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 #65"></div>
  </section>
</div>
    <div class="postbit" id="29662" data-post-id="29662">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="mjadczak" data-post="66" data-topic="4214">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mjadczak/48/3537_2.png" class="avatar"> mjadczak:</div>
<blockquote>
<p>Just on this topic, are there any guidelines on when using the procdict is actually appropriate?</p>
</blockquote>
</aside>
<p>In my opinion, only for hidden ubiquitious state, like the random module uses it, and gettext uses it, it saves the callers from passing around and holding state into those, but that absolutely should be minimized as holding on to and passing state is superior in almost all cases except those trivial ones due to their ubiquity and ease of use.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="29662" 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/discussion-about-uses-for-agent-processes/4214/67">Post #66</a>
	                </div>
	            </div>
              <div id="likers-container-29662" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="29662"
                     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 #66"></div>
  </section>
</div>
    <div class="postbit" id="29687" data-post-id="29687">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="sasajuric" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sasajuric/120/991_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  sasajuric
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Author of Elixir In Action</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="mjadczak" data-post="66" data-topic="4214">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mjadczak/48/3537_2.png" class="avatar"> mjadczak:</div>
<blockquote>
<p>Just on this topic, are there any guidelines on when using the procdict is actually appropriate?</p>
</blockquote>
</aside>
<p>I agree with everything <a class="mention" href="/u/overminddl1" rel="nofollow">@OvermindDL1</a> said, but I’d like to expand on his comment about passing state vs procidct (or agent).</p>
<p>It has been hinted in this thread that agents might be helpful when it’s tedious to thread some piece of info (accumulator) through a deeper chain of nested functions. As I said, I do think that procdict is not as bad as agents for this, but a much better solution is IMO usually to pass the data around.</p>
<p>For example, let’s say you have a bunch of functions which maintain <code>foo</code> and <code>bar</code>, meaning they accept <code>foo</code> and <code>bar</code>, and return a tuple of <code>{foo, bar}</code>. Now, you want to add <code>baz</code>, and it feels like a huge amount of tedious work to add this piece of data, so it might be tempting to use agent or procdict.</p>
<p>The proper course of actions would be to bundle these fields into a map, and then you have functions which take a map, and return a transformed version of it. Adding additional pieces of data to this map is then simple. Such map with associated functions then captures some concept explicitly. If the logic around this data grows (you find you need more fields and/or functions working on the map), it’s likely the time to extract that code into a separate module (and maybe turn a map into a struct).</p>
<p>There are cases when passing the accumulator is extremely tedious, for example if most of the functions in the stack don’t care about the accumulator at all. A good example of that is RNG. If <code>:random</code> didn’t support procdict, you’d have to explicitly pass the RNG state through all the functions, just because you need a random number somewhere deep in the stack.</p>
<p>But in most cases, using procdict to accumulate something is a q&amp;d shortcut, rather than a proper solution. Using agent for this is even worse, for technical reasons I mentioned <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="29687" 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/discussion-about-uses-for-agent-processes/4214/68">Post #67</a>
	                </div>
	            </div>
              <div id="likers-container-29687" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="29687"
                     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 #67"></div>
  </section>
</div>
    <div class="postbit" id="29692" data-post-id="29692">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I guess the main reason why using procdicts is considered a dirty hack in most cases, is because it breaks the <a href="https://en.wikipedia.org/wiki/Referential_transparency" rel="noopener nofollow ugc">Referential Transparency</a> that functional programming enjoys. If code is not referentially transparent, it is hard to maintain, because there may be hidden dependencies that you overlook while refactoring, resulting in bugs that are hard to track down.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="29692" 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/discussion-about-uses-for-agent-processes/4214/69">Post #68</a>
	                </div>
	            </div>
              <div id="likers-container-29692" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="29692"
                     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 #68"></div>
  </section>
</div>
    <div class="postbit" id="30358" data-post-id="30358">
  <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>Interesting discussion… Hard to believe someone would use an Agent for in-process state management. I don’t think that’s the fault of the tool, but of the person’s experience with functional programming.</p>
<p>Personally, I use Agents, ETSs, GenServers and FSMs a lot in my solutions. I’m not that fond of ETS so I normally default to an Agent. However, ETS has some nice query functionality that I reach for if needed.</p>
<p>For simple long running state, Agents are nice. Less setup then GenServers. Agents are also handy for simple serialized “read and do something with the value” operations.</p>
<p>When I need state and more complex behaviour, I use a GenServer. Both approaches are faster than database access IMHO. Of course, I only use these for transient data and resort to the database when survivability is required.</p>
<p>I personally feel the addition of Agents to the language was a good thing. When they first came out, I didn’t really understand why I wouldn’t just use a GenServer. However, after learning them and a couple uses, I would miss them.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="30358" 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/discussion-about-uses-for-agent-processes/4214/70">Post #69</a>
	                </div>
	            </div>
              <div id="likers-container-30358" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="30358"
                     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 #69"></div>
  </section>
</div>
    <div class="postbit" id="101611" data-post-id="101611">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="robconery1" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/robconery1/120/12431_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  robconery1
                  </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="70" data-topic="4214">
<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>Interesting discussion… Hard to believe someone would use an Agent for in-process state management. I don’t think that’s the fault of the tool, but of the person’s experience with functional programming.</p>
</blockquote>
</aside>
<p>Well… guilty as charged. I feel as though I have a pretty good understanding of the language and how functional programming works in general, but just yesterday I was coding up some ShoppingCart code and thought “hmmm, should I use Redis or should I use an Agent”. ETS occurred to me as well, but I’m not really sharing any data between processes… <em>yet</em>. So I went with what I considered to be the more idiomatic, “simpler” solution: <code>Agent</code>.</p>
<p>I mean… I dunno it kind of fits the <a href="https://hexdocs.pm/elixir/Agent.html" rel="noopener nofollow ugc">docs</a>:</p>
<blockquote>
<p>Often in Elixir there is a need to share or store state that must be accessed from different processes or by the same process at different points in time.</p>
</blockquote>
<p>So: the docs suggest this is a reasonable approach, my intuition suggests the same. Does this mean I don’t understand functional programming? <strong>Nope</strong>. Let me explain why…</p>
<p>I ended up on this thread because something didn’t feel right about this decision. I didn’t know how to kill it aside from using a <code>cron</code> job - I need these carts to expire. So naturally I’m thinking Redis or Mnesia - but carts are kind of transient. I’m not interested in the <code>Cart</code> itself, and I’m only interested short-term in the “state” of the cart. A cart will either be used to execute an order in the near-term, or it won’t.</p>
<p>This is where the functional break happened in my mind: <em>I’m much more interested in the data produced than the cart’s behavior</em>. Logs can tell me much more than the current state of the cart and an arbitrary status flag.</p>
<p>Anyway: I start to wonder how I can use Elixir to terminate a cart after a set amount of time. I’m thinking about a watchdog process and a bunch of other “let’s see what sticks to the wall” kind of stuff and I come back to the same realization: <em>Redis does this better</em>. So I Google it and end up here and damn I’m happy I did because I was asking the very same questions: <em>what are Agents good for anyway?</em>.</p>
<p>I ended up here, read this great discussion and am very happy to know I’m not the only one having these thoughts. I’m also glad I’m not alone in <a href="https://github.com/robconery/trappist" rel="noopener nofollow ugc">appreciating</a> <code>:mnesia</code>.</p>
<p>Thanks again - great discussion.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="101611" 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/discussion-about-uses-for-agent-processes/4214/71">Post #70</a>
	                </div>
	            </div>
              <div id="likers-container-101611" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="101611"
                     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 #70"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <a class="load-more-button" data-turbo-stream="true" href="/topics/4214/load_more?page=8">Load more posts (6 remaining)</a>
</div></template></turbo-stream>