title: Asynchronous batch insert/update to tables. Spring MVC, Java 8 (or later, Java 12 )
body: <p>I'm working on a business requirement that involves 3 tables:
  We have 3 database tables</p>
  
  <ul>
  <li>PRODUCTS ( 60 columns )</li>
  <li>CATEGORIZED_PRODUCTS ( 60 columns ): copy matched records from table PRODUCTS</li>
  <li>CATEGORIES</li>
  </ul>
  
  <p>Whenever user creates a new category, we have to query for list of products from table PRODUCTS that matched with that category, <em>Matched condition is defined by a list of filter columns from table PRODUCTS e.g: Color is yellow, weight is less than 10kg, etc...</em></p>
  
  <p>Then we will use primary keys from returned result from table PRODUCTS, and create a batch INSERT into table CATEGORIZED_PRODUCTS</p>
  
  <p>Basically, we have 3 SQL statement:</p>
  
  <p><strong>No. 1</strong>> Query for matched records from table PRODUCTS (This one is done by another API endpoint)</p>
  
  <p><strong>No. 2</strong>> Create new record in table Category</p>
  
  <p><strong>No. 3</strong>> Create records from table PRODUCTS into table CATEGORIZED_PRODUCTS</p>
  
  <p>The problem is, the API that query for matched PRODUCTS took 22 seconds to response with 25000 records and that API from another project. But we (i.e: multiple Microservices APIs) share the same Database.</p>
  
  <p>We cannot use Transaction (e.g @Transactional) here, because that would make end-user to wait for too long.</p>
  
  <p>We need to provide a smooth front-end behavior to end-user, so that, we don't want to let them wait. Then I can only think about using a back-end non-blocking asynchronous mechanism that Backend API returns response right after the creation of Category (i.e: at this point, we skip No. 2 &amp; 3 above )</p>
  
  <p>Then, using Asynchronous (with <strong>retry</strong> 3 times in case of failure) for No. 2 &amp; 3, then</p>
  
  <pre><code>Query for matched  from table PRODUCTS, then
  
  Create records which primary keys received from table PRODUCTS into table CATEGORIZED_PRODUCTS with 1 SQL statement.
  </code></pre>
  
  <p>We are using Spring Boot, and Spring JPA, Java 12, MySQL. Please suggest me some guideline to implement for using non-blocking asynchronous for this case.</p>
  
  <p>Using an independent batch job project is not an option, because at the moment, it is out of scope for the timespan (due to corporate management) and also for the infrastructure of the system. We are also NOT able to create stored procedure or function in database.</p>
  
  <p>PS: In case of "update" a CATEGORY, we have to delete records in table CATEGORIZED_PRODUCTS then, we process No. 2 &amp; 3 again.</p>
  
  <p>In case of "delete" a CATEGORY, we have to delete records in table CATEGORIZED_PRODUCTS then, we process No. 2 &amp; 3 again.</p>