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


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="benwilson512" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/benwilson512/120/1457_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  benwilson512
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Author of Craft GraphQL APIs in Elixir with Absinthe</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>You can find more information here: <a href="https://hexdocs.pm/ecto/Ecto.Query.html#update/3-operators" class="inline-onebox" rel="nofollow">Ecto.Query — Ecto v3.14.0</a></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="177133" 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/ecto-postgres-database-simultaneous-update/31848/8">Post #7</a>
	                </div>
	            </div>
              <div id="likers-container-177133" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="177133"
                     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 #7"></div>
  </section>
</div>
    <div class="postbit" id="177131" data-post-id="177131">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="jerry" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  jerry
                    <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 so much for the clarification, <a class="mention" href="/u/hauleth" rel="nofollow">@hauleth</a>.<br>
I really appreciate it.</p>
<p>What does the below mean, please? especially, the <em>inc</em></p>
<aside class="quote no-group" data-username="benwilson512" data-post="2" data-topic="31848">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/benwilson512/48/1457_2.png" class="avatar"> benwilson512:</div>
<blockquote>
<p><code>update(inc: [balance: 5])</code></p>
</blockquote>
</aside> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="177131" 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/ecto-postgres-database-simultaneous-update/31848/7">Post #6</a>
	                </div>
	            </div>
              <div id="likers-container-177131" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="177131"
                     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 #6"></div>
  </section>
</div>
    <div class="postbit" id="177128" data-post-id="177128">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Yes, each query is transaction within itself, so within single query it is impossible to have race condition (assuming that there are no bugs in DB implementation). I would say that DB is probably better optimised for single <code>UPDATE</code> queries than locking mechanisms as second one can impose some additional restrictions as DB do not know what you want to do with that lock.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="177128" 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/ecto-postgres-database-simultaneous-update/31848/6">Post #5</a>
	                </div>
	            </div>
              <div id="likers-container-177128" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="177128"
                     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 #5"></div>
  </section>
</div>
    <div class="postbit" id="177124" data-post-id="177124">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="jerry" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  jerry
                    <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>Hello <a class="mention" href="/u/hauleth" rel="nofollow">@hauleth</a>,</p>
<p>Considering the nature of the query statement in Option 2, does Option 1 give the same level of integrity to the data being modified?</p>
<p>Thanks.</p>
<p>Jerry</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="177124" 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/ecto-postgres-database-simultaneous-update/31848/5">Post #4</a>
	                </div>
	            </div>
              <div id="likers-container-177124" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="177124"
                     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 #4"></div>
  </section>
</div>
    <div class="postbit" id="177120" data-post-id="177120">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="jerry" data-post="3" data-topic="31848">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/j/5daacb/48.png" class="avatar"> jerry:</div>
<blockquote>
<p>For each request, I need to reduce the account balance by the requested value before taking an action, hence the need to do read, modify, and write on the record.</p>
</blockquote>
</aside>
<p>If you need just to update, then I think the 1st option will be better as you do not need to do 12 trips (at least as it is TCP) and you can just do everything in 6.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="177120" 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/ecto-postgres-database-simultaneous-update/31848/4">Post #3</a>
	                </div>
	            </div>
              <div id="likers-container-177120" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="177120"
                     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 #3"></div>
  </section>
</div>
    <div class="postbit" id="177111" data-post-id="177111">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="jerry" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  jerry
                    <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>Many thanks <a class="mention" href="/u/benwilson512" rel="nofollow">@benwilson512</a>.</p>
<p>I think the option two will work for me.</p>
<p>It’s a high traffic application, using Elixir and ecto_sql with postgres.<br>
There is usually multiples of requests to the same account.</p>
<p>For each request, I need to reduce the account balance by the requested value before taking an action, hence the need to do read, modify, and write on the record.</p>
<p>Kindly let me try it out and feedback on the outcome.</p>
<p>Regards,</p>
<p>Jerry</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="177111" 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/ecto-postgres-database-simultaneous-update/31848/3">Post #2</a>
	                </div>
	            </div>
              <div id="likers-container-177111" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="177111"
                     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 #2"></div>
  </section>
</div>
    <div class="postbit" id="177100" data-post-id="177100">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="benwilson512" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/benwilson512/120/1457_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  benwilson512
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Author of Craft GraphQL APIs in Elixir with Absinthe</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="jerry" data-post="1" data-topic="31848">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/j/5daacb/48.png" class="avatar"> jerry:</div>
<blockquote>
<p>Whenever there are simultaneous/concurrent updates on an account record, the resulting balance is wrong.</p>
</blockquote>
</aside>
<p>Here are some options you can use to serialize access to rows. Regardless of which of these you use, I highly recommend <em>also</em> having a table that logs balance changes on each account, like a ledger. This allows you to audit the total in the future, and debug any issues.</p>
<h2><a name="p-177100-option-1-avoid-read-modify-write-patterns-1" class="anchor" href="#p-177100-option-1-avoid-read-modify-write-patterns-1" aria-label="Heading link" rel="nofollow"></a>Option 1: Avoid read, modify, write patterns.</h2>
<p>Instead of doing:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">account = Repo.get(Account, account_id)
account |&gt; Account.changeset(%{balance: account.balance + 5}) |&gt; Repo.update!
</code></pre>
<p>Do:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">Account |&gt; where(id: ^account_id) |&gt; update(inc: [balance: 5]) |&gt; Repo.update_all([])
</code></pre>
<p>In this approach, you change the balance in a single SQL query instead of two, avoiding race conditions. Postgres will ensure that the single update query runs atomicly.</p>
<h2><a name="p-177100-option-2-select-for-update-2" class="anchor" href="#p-177100-option-2-select-for-update-2" aria-label="Heading link" rel="nofollow"></a>Option 2: SELECT FOR UPDATE</h2>
<p>If you can’t avoid read, modify, write, then take a lock on the row you wish to update:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">Repo.transaction(fn -&gt;
  account = Account |&gt; lock("FOR UPDATE") |&gt; Repo.get!(account_id)
  account |&gt; Account.changeset(%{balance: account.balance + 5}) |&gt; Repo.update!
end)
</code></pre>
<p>This locks access to the account row for the duration of the transaction, allowing your process to safely reason about it.</p>
<p>If conflict is rare, you can also try doing <a href="https://hexdocs.pm/ecto/Ecto.Changeset.html#optimistic_lock/3" class="inline-onebox" rel="nofollow">Ecto.Changeset — Ecto v3.14.0</a> instead.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="177100" data-batch-url="/posts/batch_likers">
                        15
                      </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/ecto-postgres-database-simultaneous-update/31848/2">Post #1</a>
	                </div>
	            </div>
              <div id="likers-container-177100" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="177100"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-first-post cat-first-post" title="First 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>