简体   繁体   中英

Short or long lived connections for RethinkDB?

We have a project on Node.js that is based on restify and we are using RethinkDB as a database. The problem is that RethinkDB should be accessed from different parts of code (from route handlers, middlewares), but not for all requests. I am wondering what is the best way to connect to RethinkDB in this case?

I see next options:

  • have one long connection that is stored somewhere (approach we use now),

  • connect to RethinkDB on each HTTP request, which potentially some of the connections being never used,

  • connect in each part individually, with potentially several connections per HTTP request, but without useless connections.

I ask this question because I am not sure how well Rethink handle well short/long connections and how expensive they are. For instance MongoDB prefers long connections, but all examples in RethinkDB docs uses one connection per HTTP request.

I recommend a connection pool or one connection per query. Especially if you use feature like changefeeds, which is recommened to be on its own connection.

When you use a single connection for everything, you have to also handle re-connection when the connection timeout/broken. I think it's easier to just use a connection per query, or shared a connection on a request/response.

Just ensure to close your connection after using it, otherwise you will leak connections and new connection cannot be created.

Some driver goes further and doesn't require you to think of connection anymore such as: https://github.com/neumino/rethinkdbdash

Or Elixir RethinkDB: https://github.com/hamiltop/rethinkdb-elixir/issues/32 has an issue to create connection pool.

RethinkDB has an issue related connection pool: https://github.com/rethinkdb/rethinkdb/issues/281

That's probably what community is heading too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM