<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="312580" data-post-id="312580">
  <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>Ah, I don’t mean that. Surely there must be more errors in the Elixir’s console / log?</p>
<p>But in any case I at my wits end here because I’ve done work similar to yours, at least 7 times in the last 7.5 years and was always able to fix even the occasional missed inserts.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="312580" 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/data-processing-in-parallel-hackathon-project/60424/22">Post #21</a>
	                </div>
	            </div>
              <div id="likers-container-312580" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="312580"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #21"></div>
  </section>
</div>
    <div class="postbit" id="312598" data-post-id="312598">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>You can try pool_size = 16</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="312598" 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/data-processing-in-parallel-hackathon-project/60424/23">Post #22</a>
	                </div>
	            </div>
              <div id="likers-container-312598" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="312598"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #22"></div>
  </section>
</div>
    <div class="postbit" id="312600" data-post-id="312600">
  <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>Just now noticed that <code>pool_size</code> and <code>max_concurrency</code> are the same. As already mentioned above, I advise you to always have <code>max_concurrency</code> be little less than <code>pool_size</code>.</p>
<p>I’d bump <code>pool_size</code> to 20 and then set <code>max_concurrency</code> to 18.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="312600" 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/data-processing-in-parallel-hackathon-project/60424/24">Post #23</a>
	                </div>
	            </div>
              <div id="likers-container-312600" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="312600"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #23"></div>
  </section>
</div>
    <div class="postbit" id="313142" data-post-id="313142">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Iex.new" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/Iex.new/120/18284_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Iex.new
                    <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>I wish you all a happy and healthy new year!</p>
<p>I have configured the pool size as advised to 20 and the max_concurrency to 18.<br>
This seems to have no effect as I still have the same issue as before.</p>
<p>config/config.exs</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">import Config


config :file_watch_example, :ecto_repos, [Store]
config :file_watch_example, Store,
  database: "store",
  username: "",
  password: "",
  hostname: "db",
  pool_size: 20,
  migration_timestamps: [type: :utc_datetime_usec],
  migration_lock: nil,
  queue_target: 10_000

config :logger, :console,
  level: :debug,
  format: "[$level] $message $metadata\n",
  metadata: [:error_code, :file]

config :logger,
  backends: [{LoggerFileBackend, :debug}]

config :logger, :debug,
  path: "/var/log/elixir_app/debug.log",
  level: :debug
</code></pre>
<p>lib/ingest_csv.ex</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule IngestCSV do
  alias NimbleCSV.RFC4180, as: YourCSV

  require Logger

  NimbleCSV.define(YourCSV, separator: ";")

  def load(path) do
    path
    |&gt; Path.expand()
    |&gt; File.stream!(read_ahead: 524_288)
    |&gt; YourCSV.parse_stream()
    |&gt; Stream.chunk_every(500)
    |&gt; Task.async_stream(fn rows -&gt;
      Enum.map(rows, fn [name, bar_code, price, currency] -&gt;
         FileWatchExample.Product.create_product(%{name: name, bar_code: bar_code, price: price, currency: currency})
      end)
    end, max_concurrency: 18, on_timeout: :kill_task, ordered: false)
    |&gt; Stream.run()
  end
end
</code></pre>
<p>grep -i error /var/log/elixir_app/debug.log | tail -3</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">07:43:22.538 [info] MyXQL.Connection (#PID&lt;0.230.0&gt;) disconnected: ** (DBConnection.ConnectionError) client #PID&lt;0.5886.0&gt; exited
07:43:22.541 [info] MyXQL.Connection (#PID&lt;0.226.0&gt;) disconnected: ** (DBConnection.ConnectionError) client #PID&lt;0.5889.0&gt; exited
07:43:22.542 [info] MyXQL.Connection (#PID&lt;0.240.0&gt;) disconnected: ** (DBConnection.ConnectionError) client #PID&lt;0.5890.0&gt; exited
</code></pre>
<p>The MyXQL.Connection Error is the only one I am facing currently:</p>
<pre data-code-wrap="bash"><code class="lang-bash">grep -i error /var/log/elixir_app/debug.log | grep -v MyXQL.Connection | wc -l
0
</code></pre>
<p>MySQL “show variables:” :</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">concurrent_insert          | AUTO
connect_timeout            | 10
max_connect_errors         | 100
max_connections            | 1000
max_insert_delayed_threads | 20
max_user_connections       | 0
</code></pre>
<p>The Elixir-App and MySQL run in Docker and no other containers are running when doing my tests.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="313142" 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/data-processing-in-parallel-hackathon-project/60424/25">Post #24</a>
	                </div>
	            </div>
              <div id="likers-container-313142" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="313142"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #24"></div>
  </section>
</div>
    <div class="postbit" id="313143" data-post-id="313143">
  <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>Do not insert 500 records in a loop. Maybe just remove the <code>Stream.chunk_every</code> and <code>Enum.map</code> calls and and just insert records one by one. I think the way you’re doing it now increases contention for DB connections.</p>
<p>Working with batches of records in your scenario only makes sense if you’re going to use <code>Repo.insert_all</code> (without validation). If you need to validate and thus insert records one by one then there’s no point in working with batches.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="313143" 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/data-processing-in-parallel-hackathon-project/60424/26">Post #25</a>
	                </div>
	            </div>
              <div id="likers-container-313143" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="313143"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #25"></div>
  </section>
</div>
    <div class="postbit" id="313144" data-post-id="313144">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Iex.new" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/Iex.new/120/18284_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Iex.new
                    <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>thank you, I will try that.<br>
For which use cases make it sense to do not do validation?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="313144" 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/data-processing-in-parallel-hackathon-project/60424/27">Post #26</a>
	                </div>
	            </div>
              <div id="likers-container-313144" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="313144"
                     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 #26"></div>
  </section>
</div>
    <div class="postbit" id="313145" data-post-id="313145">
  <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="Iex.new" data-post="27" data-topic="60424">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/iex.new/48/18284_2.png" class="avatar"> Iex.new:</div>
<blockquote>
<p>For which use cases make it sense to do not do validation?</p>
</blockquote>
</aside>
<p>In your current code.</p>
<p>It tries to do two things that conflict: (1) work with the records in batches – via the <code>Stream.chunk_every(500)</code> call, and (2) insert records one by one.</p>
<p>You don’t do that.</p>
<p>You either:</p>
<ol>
<li>Use <code>Stream.chunk_every(500)</code> <em>and</em> <code>Repo.insert_all(rows)</code> (which skips validation).</li>
</ol>
<p>OR</p>
<ol start="2">
<li>Do NOT use <code>Stream.chunk_every(500)</code> and do NOT use <code>Enum.map(rows, ... Repo.insert(one_record))</code>. Just do <code>Repo.insert(row)</code> directly.</li>
</ol>
<p>I’ll follow up with a bigger comment explaining why you can’t ingest all records. Looking at your complete code it’s now perfectly clear. Incoming.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="313145" 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/data-processing-in-parallel-hackathon-project/60424/28">Post #27</a>
	                </div>
	            </div>
              <div id="likers-container-313145" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="313145"
                     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 #27"></div>
  </section>
</div>
    <div class="postbit" id="313146" data-post-id="313146">
  <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>Now that you posted the entire code, here’s exactly what’s going on.</p>
<p>You are spawning 18 parallel processes and each of them is trying to insert 500 records <em>one by one</em> which means 9000 DB transactions. Unless you configure your pool with 9000 connections then an incomplete ingestion is to be expected.</p>
<p>The idea of balancing out your Repo’s pool size and the streaming processing’s <code>max_concurrency</code> parameter is to avoid exactly what your code is doing: never go above the Repo’s pool size.</p>
<p>But your code is doing it, in fact exceeding Repo’s capacity by 500x.</p>
<p>Change your code to do this:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule IngestCSV do
  alias NimbleCSV.RFC4180, as: YourCSV

  require Logger

  NimbleCSV.define(YourCSV, separator: ";")

  def load(path) do
    path
    |&gt; Path.expand()
    |&gt; File.stream!(read_ahead: 524_288)
    |&gt; YourCSV.parse_stream()
    # Remove `Stream.chunk_every`
    |&gt; Task.async_stream(fn [name, bar_code, price, currency] -&gt;
       # Remove `Enum.map`
       FileWatchExample.Product.create_product(%{name: name, bar_code: bar_code, price: price, currency: currency})
    end, max_concurrency: 18, on_timeout: :kill_task, ordered: false)
    |&gt; Stream.run()
  end
end
</code></pre>
<p>Now your DB pool’s size is 20 and you will never have more than 18 records being inserted in parallel, thus you should never have dropped ingestions.</p>
<p>If that really works then you can proceed to bump up the Repo’s pool size to e.g. 100 and put <code>max_concurrency</code> at 95 - 98 and that should work fine and accelerate your workload.</p>
<p>I believe you are being tripped up by <code>Task.async_stream</code>’s semantics. The function that is passed to it is going to be working in 18 <em>separate parallel independent processes</em>. BUT, having <code>Enum.map</code> inside of it is still serial i.e. <em>not</em> parallelized. So you do have 18 parallel independent tasks each trying to open 500 DB transactions (inserts) one after another. As you have found that that mostly works but not always – because it’s a very wrongly written parallel code.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="313146" 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/data-processing-in-parallel-hackathon-project/60424/29">Post #28</a>
	                </div>
	            </div>
              <div id="likers-container-313146" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="313146"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-solved cat-solved" title="Marked as solution"></div>
  </section>
</div>
    <div class="postbit" id="313207" data-post-id="313207">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I wonder if postgres can handle this abuse better.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="313207" 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/data-processing-in-parallel-hackathon-project/60424/30">Post #29</a>
	                </div>
	            </div>
              <div id="likers-container-313207" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="313207"
                     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 #29"></div>
  </section>
</div>
    <div class="postbit" id="313242" data-post-id="313242">
  <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>Likely yes but with that code the odds are against any DB.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="313242" 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/data-processing-in-parallel-hackathon-project/60424/31">Post #30</a>
	                </div>
	            </div>
              <div id="likers-container-313242" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="313242"
                     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 #30"></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/60424/load_more?page=4">Load more posts (17 remaining)</a>
</div></template></turbo-stream>