<p>Assuming there is a Flask web server that has two routes, deployed as a CloudRun service over GKE.</p>
<pre class="lang-py prettyprint-override"><code>@app.route('/cpu_intensive', methods=['POST'], endpoint='cpu_intensive')
def cpu_intensive():
    #TODO: some actions, cpu intensive

@app.route('/batch_request', methods=['POST'], endpoint='batch_request')
def batch_request():
    #TODO: invoke cpu_intensive
</code></pre>
<p>A &quot;batch_request&quot; is a batch of many same structured requests - each one is highly CPU intensive and handled by the function &quot;cpu_intensive&quot;. No reasonable machine can handle a large batch and thus it needs to be paralleled across multiple replicas.
The deployment is configured that every instance can handle only 1 request at a time, so when multiple requests arrive CloudRun will replicate the instance.
I would like to have a service with these two endpoints, one to accept &quot;batch_requests&quot; and only break them down to smaller requests and another endpoint to actually handle a single &quot;cpu_intensive&quot; request. What is the best way for &quot;batch_request&quot; break down the batch to smaller requests and invoke &quot;cpu_intensive&quot; so that CloudRun will scale the number of instances?</p>
<ul>
<li>make http request to localhost - doesn't work since the load balancer is not aware of these calls.</li>
<li>keep the deployment URL in a conf file and make a network call to it?</li>
</ul>
<p>Other suggestions?</p>

gidutz
<p><em>With more detail, it's now clearer!!</em></p>
<p>You have 2 responsibilities</p>
<ul>
<li>One to split -&gt; Many request can be handle in parallel, no compute intensive</li>
<li>One to process -&gt; Each request must be processed on a dedicated instance because of compute intensive process.</li>
</ul>
<p>If your split performs internal calls (with localhost for example) you will be only on the same instance, and you will parallelize nothing (just multi thread the same request on the same instance)</p>
<p>So, for this, you need 2 services:</p>
<ul>
<li>one to split, and it can accept several concurrent request</li>
<li>The second to process, and this time you need to set the concurrency param to 1 to be sure to accept only one request in the same time.</li>
</ul>
<p>To improve your design, and if the batch processing can be asynchronous (I mean, the split process don't need to know when the batch process is over), you can add PubSub or Cloud Task in the middle to decouple the 2 parts.</p>
<p>And if the processing requires more than 4 CPUs 4Gb of memory, or takes more than 1 hour, use Cloud Run on GKE and not Cloud Run managed.</p>
<p>Last word: Now, if you don't use PubSub, the best way is to set the Batch Process URL in Env Var of your Split Service to know it.</p>
