databaserustconnection-pooling

Give a database client to a webserver in rust


I'm currently working on a side project (in rust) where i have a to give a client database (postgres, redis, or meilisearch) to an actix route. The goal is to practice developing scalable systems and learning rust async on the way. The project is quite simple, it takes parses a post query to the webserver, and put it in the database according to some conditions and returns.

Since it's not thread safe or O.K to clone a client and you should not create a client for each request to the webserver i have done some research on the way to go.

I found that database connection pool were a solution, but nothing is implemented for meilisearch. Hence i searched but found nothing on how much pool were of any help, is there some "goto" resource on performance gained from engineering blog/company blogs that would have some feedbacks like potential costs/drawbacks?

If no connection pooling system exists, should i implement one on my own? Like extending r2d2 ? Or is it too much of a daunting task? I liked stackoverflow blog https://stackoverflow.blog/2020/10/14/improve-database-performance-with-connection-pooling/ and cockroachDB https://www.cockroachlabs.com/blog/what-is-connection-pooling/ but they are quite small.

In general are there other means of giving clients to a webserver in rust that follow good practices (even if not based on connection pooling) ?


Solution

  • I cannot give you an answer for your Meilisearch problem concerning the database connection pool. But generally speaking, using database connection pool or not depends on your needs:

    Regarding the second part of your question (i.e passing a database client to a Webserver in Rust), in your post you are saying that you're using Actix-Web for your application. Actix-Web has actix_web::App::app_data method that enables you to have access to your database connection from your routes without having to create a new client in each of your routes.

    I hope it helped!