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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>You may also be interested in the <a href="https://blog.stenmans.org/theBeamBook/#_scheduling" rel="noopener nofollow ugc">BEAM Book</a> (linked the scheduling chapter directly) if you want to know more about the internals.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="237570" 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/clarification-on-sending-emails-asynchronously/44851/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-237570" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="237570"
                     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="237618" data-post-id="237618">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="josefrichter" data-post="8" data-topic="44851">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/josefrichter/48/19529_2.png" class="avatar"> josefrichter:</div>
<blockquote>
<p>And if one core = two threads, then each scheduler has 2 threads to work with, and can use the other one if one of them is blocked?</p>
</blockquote>
</aside>
<p>You can have 1000 Elixir processes stuck in an infinite loop (or a very long network call), on a 4-core / 8-thread CPU. The other Elixir processes will <em>still</em> run fine.</p>
<p>Understand that Erlang VM’s parallelism is not going to get deadlocked by a few rogue processes. That’s what makes it different and so desirable.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="237618" 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/clarification-on-sending-emails-asynchronously/44851/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-237618" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="237618"
                     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="237625" data-post-id="237625">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Yeah I was just watching Sasa’s talk linked above . It seems like the piece I was missing was the <strong>preemtive scheduling</strong> where one rogue process cannot really clog the cpu. It might be coming back over and over again in some cases, maxing out the CPU, but it can never take over and block the other processes. That’s fascinating!</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="237625" 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/clarification-on-sending-emails-asynchronously/44851/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-237625" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="237625"
                     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="237628" data-post-id="237628">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Yes. Without the preemptive scheduling of the runtime and the 99% transparent parallelism, Erlang / Elixir would remain just some curiosity languages and I personally would not use them.</p>
<p>As I said in other threads: don’t “love a language”. Do love the language’s runtime and (NOT in the case of Erlang and Elixir) compilation strictness (OCaml and Rust shine here).</p>
<p>The language syntax does not matter one bit. There are other pretty nice and solid frameworks out there – PHP’s Laravel is one very good example – but without a proper runtime and/or good compilation-level enforcement they still remain mostly curiosities, at least to serious programmers.</p>
<p><em>(And yes I am aware PHP and Ruby are used much more than Elixir. That’s a topic that’s beaten to death many times over but TL;DR no, that does not make them good. That makes them a safe choice for many employers and employees. At the end of the day we are in the whole thing for money. Programming is not just a hobby after all.)</em></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="237628" data-batch-url="/posts/batch_likers">
                        4
                      </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/clarification-on-sending-emails-asynchronously/44851/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-237628" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="237628"
                     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="237748" data-post-id="237748">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="ScriptyScott" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  ScriptyScott
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Thanks all for the great resources! I come from a background of mobile development with a bit of web dev peppered throughout my career. I always found it strange how certain asynchronous task (sending emails, async calls, etc…) would require specifically calling the async version of the method and usually some sort of additional queue support, often complicating stacks with things like Redis. I’ve skimmed through some of the links posted here and it looks like Elixir/Erlang handles threading in a manner that’s reminiscent of some of the mobile platforms I’m used to. I’m really glad I asked this question, thank you so much for the resources will be taking a more in-depth look over the next few days.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="237748" 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/clarification-on-sending-emails-asynchronously/44851/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-237748" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="237748"
                     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="237888" data-post-id="237888">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="softrage" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  softrage
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Note that more generally, if you don’t want to wait for a long-running process to terminate, i.e. during an HTTP request, you can also easily spawn a new process. To be clear and to reiterate some info in this thread, these processes are very cheap: they are part of the Erlang Runtime System (ERTS), and are <strong>not</strong> OS processes, and are unrelated to the threads/cores available on the system. You can spawn a new process using one of the <a href="https://elixir-lang.org/getting-started/processes.html" rel="nofollow">methods documented here</a>. A simple example would be:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">def some_route_handler(conn, _params) do
  spawn(fn -&gt; long_running_process() end)
  send_resp(conn, 202, "Accepted")
end
</code></pre>
<p>Even though a synchronous operation won’t block execution for other users, it will of course for the current user waiting for that process, but spawning another process in this way allows the current process to continue execution without waiting.</p>
<p>I would consider sending email as a long running process, because you never know how long this might take, and for many situations (i.e. sending a welcome email) there isn’t much I would want to do if the mail didn’t send. I might implement retry logic, but that could go in the spawned process as well. If it ultimately does fail, I probably don’t want to show a cryptic and somewhat distressing “Failed to send welcome email” message to my user, so there is no point in making them wait. If you <em>can</em> show a useful error message, and determine it’s worth the tradeoff of making users wait longer in exchange for being able to show that message when there is a failure, then you can make it synchronous.</p>
<p>I use a mailing library called Bamboo that encourages asynchronous behavior with its <code>deliver_later</code> function, but it looks like with Swoosh you just need to spawn a process yourself if you want that. Conversely, for synchronous behavior Bamboo provides <code>deliver_now</code>, while that’s the default with Swoosh. I’ve mostly gone the asynchronous route, but hopefully this gives you some information on how/when to implement synchronous/asynchronous behavior.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="237888" 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/clarification-on-sending-emails-asynchronously/44851/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-237888" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="237888"
                     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 #16"></div>
  </section>
</div>
    <div class="postbit" id="237890" data-post-id="237890">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Yep, both <code>Bamboo</code> and <code>Swoosh</code> are alright.</p>
<p>One more step forward would be to enqueue mail-sending jobs to <code>Oban</code> and do synchronous sending in the working process you’re given when the job’s time to execute has come.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="237890" 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/clarification-on-sending-emails-asynchronously/44851/18">Post #17</a>
	                </div>
	            </div>
              <div id="likers-container-237890" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="237890"
                     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 #17"></div>
  </section>
</div>
    <div class="postbit" id="237892" data-post-id="237892">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Is spawning a process like this outside of supervision tree ok in this case? I know in the past I actually spawned some Tasks under Task.Supervisor to send out emails. But that was my early days, not sure if that’s not overkill..</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="237892" 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/clarification-on-sending-emails-asynchronously/44851/19">Post #18</a>
	                </div>
	            </div>
              <div id="likers-container-237892" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="237892"
                     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 #18"></div>
  </section>
</div>
    <div class="postbit" id="237893" data-post-id="237893">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="softrage" data-post="17" data-topic="44851">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/s/74df32/48.png" class="avatar"> softrage:</div>
<blockquote>
<p>I would consider sending email as a long running process, because you never know how long this might take, and for many situations (i.e. sending a welcome email) there isn’t much I would want to do if the mail didn’t send.</p>
</blockquote>
</aside>
<p>For small to medium setup, the only 2 sane email sending methods are:</p>
<ul>
<li>use a reputable 3rd party services, such as Sendgrid or mailgun</li>
<li>install a SMTP server very close, like in the same data center or even on the same machine</li>
</ul>
<p>In either case, there are buffering on the other side already and the sending should be instantaneous. I see no point of spawning. In any event, retrying at the application server side will not help.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="237893" 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/clarification-on-sending-emails-asynchronously/44851/20">Post #19</a>
	                </div>
	            </div>
              <div id="likers-container-237893" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="237893"
                     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 #19"></div>
  </section>
</div>
    <div class="postbit" id="237895" data-post-id="237895">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="softrage" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  softrage
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I agree that these are sane methods. Here’s an unscientific experiment sending bad requests from an instance on GCP to Sendgrid/Mailgun to see response times:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">curl -d '{"bad":"data"}' -H "Content-Type: application/json" -X POST https://api.sendgrid.com/v3/marketing/test/send_email -s -o /dev/null -w "%{time_total}\n"
0.169216

curl -d '{"bad":"data"}' -H "Content-Type: application/json" -X POST https://api.mailgun.net/v3/mydomain.com -s -o /dev/null -w "%{time_total}\n"
0.272170
</code></pre>
<p>170ms/270ms extra in the normal case, with no HTML email body, is not quite free. When latency increases, or in the worse cases when a request fails for some reason (which they will on rare occasions, the case where retrying server side does help), or when the service has an outage (looking at you AWS!), then presumably the user is waiting much longer until Hackney times out to get a response. Since there’s no additional effort at least in the basic case, if I’m not planning to give an error to my user, I don’t see a reason not to spawn. It doesn’t make it more robust (unlike what <a class="mention" href="/u/dimitarvp" rel="nofollow">@dimitarvp</a> mentions), but if the email is going to successfully send or occasionally die either way, it might as well do it where my user doesn’t have to see <img src="https://forum.elixirforum.com/images/emoji/apple/upside_down_face.png?v=15" title=":upside_down_face:" class="emoji" alt=":upside_down_face:" 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="237895" 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/clarification-on-sending-emails-asynchronously/44851/21">Post #20</a>
	                </div>
	            </div>
              <div id="likers-container-237895" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="237895"
                     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 #20"></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/44851/load_more?page=3">Load more posts (7 remaining)</a>
</div></template></turbo-stream>