I am not (anymore) very familiar with Pg, but I have not heard that any popular database could connect asynchronously. Therefore, you still need to maintain a database connection for the duration of the request. Therefore, you still need to block some of them where the stack is located.
Depending on your application, you may already be doing it in the best way.
But when you are dealing with some kind of polling application (where the same client sends a lot of requests in a short time), and it is more important to get an answer, even if it is empty, then you can write ruby โโFiber or a blown stream stream or process , which is durable and requests database queries and caches the results.
For example: the request comes from client A. The Goliath application processes the request to the database process using some unique identifier and responds to the request โwhile there is no dataโ. The database process completes the query and stores the results in a cache with an identifier. When the next request comes from the same client, Goliath sees that it is already waiting for the results of the request, removes the results from the cache, and responds to the client. At the same time, he plans the next query with the database process so that it is ready earlier. If the next request arrives before the last one is completed, a new request is not scheduled (without multiplying the requests).
Thus, your answers are fast and non-blocking, while maintaining fresh data from the ASAP database. Of course, they may not be slightly synchronized with the actual data, but again, depending on the application, this may not be a problem.
Laas
source share