Search code examples
akkadatabase-connectionslickakka-httphikaricp

Using a connection per query via Slick and Hikari


I have a web application that runs some database queries to fulfil some request. I am using aka http along with Slick(with HikariCP). I have used for comprehension to make sure these queries run in parallel. However, when I run it I see that only 1 connection is used to run all queries. What I want is the queries to run in parallel on the db i.e. use a separate connection.

eventFilter : DBIO[] = ??
vendorFilter : DBIO[] = ??

val flags = for {
  event <- eventFilter
  vendor <- vendorFilter
} yield (event, vendor)

Here eventFilter and vendorFilter are database actions that would be run in separate threads. However they are using the same connection to the database. I want to use separate connections so that the queries could actually run in parallel. I am using HikariCP that has 20 connections in idle state. I was hoping I could use them.

Is my understanding correct? Any idea as to how I can implement this. Any changes in Hikari config that could help bring this change. Also please point out any drawbacks related with this approach. Thanks.


Solution

  • Remember that the for comprehension is just syntactic sugar for applications of map and flatMap. The for comprehension desugars into:

    val flags = eventFilter.flatMap { event =>
      vendorFilter.map { vendor =>
        (event, vendor)
      }
    }
    

    From the documentation for flatMap:

    Use the result produced by the successful execution of this action to compute and then run the next action in sequence. The resulting action fails if either this action, the computation, or the computed action fails.

    So vendorFilter isn't executed until after eventFilter completes.

    If you want the two filters to execute in parallel, you'll want something like:

    val eventFut = db.run(eventFilter)
    val vendorFut = db.run(vendorFilter)
    
    eventFut.zip(vendorFut)