Skip to content

Explore a connection pool API in client drivers #281

Open

Description

@coffeemug

We should explore various connection pool designs here and see if implementing one might be beneficial.

Activity

  1. modified the milestones: , backlog on Mar 11, 2014
  2. nviennot commented on Mar 13, 2014

    @nviennot
    Contributor

    If we end up having a connection pool, I'd be happy if we didn't have to check in / check out connections from the pool. But connections are not stateless, cursors and asynchronous/noreply requests adds state to the connection. Chances are, we'll have to explicitly get a connection from the pool, and give it back. I'd rather have a thread safe connection. There should be only little benefit from having a connection pool, except perhaps better scheduling reducing the latency (e.g. one thread is in the process of doing a huge bulk insert, and another one wants to do a very simple read).

  3. mlucy commented on Mar 13, 2014

    @mlucy
    Member

    Connection pools are very useful when you want to issue a bunch of queries that don't need to see each other's writes. (For example, two big inserts into different tables.) Handling connection state might be a pain, but I think we could probably work it out in the official drivers. (I'm more worried about the maintenance burden on third-party drivers.)

  4. nviennot commented on Mar 13, 2014

    @nviennot
    Contributor

    I would argue that managing a connection pool is not a driver concern, but an application / ORM concern.

  5. neumino commented on Mar 13, 2014

    @neumino
    Member

    So all the node.js packages were using the gen-pool package where you have to manually acquire and release a connection. I think it's not really good for two reasons

    • obviously, it's not super friendly
    • it doesn't prevent users from doing errors (releasing a connection that could be re-used by another query)

    And I think I came up with something better for rethinkdbdash. When you call run on a query, it's going to:

    • acquire a connection available
    • run a query
    • then if the result is
      • a datum, error, or complete sequence, the connection is released
      • an incomplete sequence, the connection is NOT released and will be released only when the user close the cursor or retrieve all the data from the cursor

    If no connection is available, create one or wait for one to be released.

    The only drawback from this implementation is that if users don't fetch all the data from a cursor and don't close it, the connection will never be released.

  6. modified the milestones: , on Mar 26, 2014
  7. mlucy commented on Apr 1, 2014

    @mlucy
    Member

    How much demand has there been for this? Do we know the interface people basically want? Do they want a pool that they can treat abstractly as a single connection, or do they want a way to check out and return connections from a pool for temporary use?

  8. neumino commented on Apr 1, 2014

    @neumino
    Member

    People basically wants to call run() without passing a connection (and opening/closing it).
    Like this https://github.com/stingraze/Twitter-RethinkDB/blob/master/twitter-rethinkdb-search-insert.py#L3

    If we have a connection pool, we can also take out repl.

  9. nviennot commented on Apr 2, 2014

    @nviennot
    Contributor

    run() should always accept a connection if given one. If I want to connect to two different rethinkdb instances in the same thread (for example to do some data copying), I should be able to do so.
    And if I wish to have a connection pool for each of the databases, I should be able to do so.
    Having a connection pool is an orthogonal concept from which connection to use by default.

    I don't see what's wrong with repl, even if there are connection pools. Maybe the name could be better, for example default_connection=.

  10. danielmewes commented on Apr 2, 2014

    @danielmewes
    Member

    One option might be to make the argument that is passed to run() an abstract class, and to document it.
    Something like a ConnectionProvider type, with an abstract function getConnection() -> Connection and probably something like lock(Connection) and unlock(Connection) to implement what @neumino described (so connections are not closed if there are active cursors around). The exact interface might look slightly different depending on the client language.

    We could ship with a default SingleConnectionProvider, but people could easily implement their own connection pool mechanisms.

    For convenience, we could make run(conn) accept a Connection object as well, which would have the same behavior as connProvider = new SingleConnectionProvider(conn); query.run(connProvider).

  11. neumino commented on Apr 2, 2014

    @neumino
    Member

    Yep, in the case of rethinkdbdash, you can pass a connection to run.
    It's only when you don't pass a connection that run is going to try to get a connection from the pool.

    When I wrote rethinkdbdash I didn't think of multiple pools. I saw the pool as a way to not have to pass a connection to run, but I can see why it can be useful.

    I also currently don't let people manually get a connection from the pool and put it back.

  12. jcspencer commented on Apr 3, 2014

    @jcspencer

    @neumino Would it ever be possible for rethinkdbdash to become the official JS driver, and have the other official drivers follow in it's footsteps? Or would it be impossible due to licensing issues?

  13. neumino commented on Apr 3, 2014

    @neumino
    Member

    The main two differences with the two drivers are

    The first point is almost done and will be released soon.
    The second point is discussed here.

    So the official driver is getting close to rethinkdbdash.
    The differences then will be mostly about the environment (how you run the tests, etc.), and the code itself (rethinkdbdash is written in JavaScript, and the official driver in CoffeeScript).

    For now, rethinkdbdash cannot be the official driver because

    • it uses only node-protobuf and doesn't work with protobufjs
    • it doesnt support http connections

    #1868 will remove the need for protobuf and I plan to move rethinkdbdash to the JSON API.
    rethinkdbdash will have to support http connections at least to become the main driver.

    So it may happen at some point, but I don't know when.

  14. 83 remaining items

  15. mlucy commented on May 7, 2015

    @mlucy
    Member
    1. Yes. We push as much work as possible to the shards, but some work has to be done on the machine that accepted the connection, and you'll also be limited by network bandwidth on that machine if all your clients are connecting to it. (Also, if you have an unsharded table, you can drastically reduce intracluster traffic by connecting the only node hosting the table.)
    2. r.table('whatever').config() should tell you what nodes are acting as shards for the table.
  16. PeterKnego commented on Apr 10, 2016

    @PeterKnego

    HI, I created a connection pool implementation for Java driver. It wraps connections and provides them in a round-robin fashion, creating new connections as needed. It supports connecting to multiple cluster nodes.

    query.run() // use the global pool
    query.run(pool) // use the pool and automatically lease/return the connection when done
    query.run(connection) // the legacy way
    

    This is the basic test: https://github.com/PeterKnego/rethinkdb/blob/connection_pool/drivers/java/src/test/java/com/rethinkdb/FixedConnectionPoolTest.java

  17. coffeemug commented on Apr 11, 2016

    @coffeemug
    ContributorAuthor

    👍

  18. prashaantt commented on Sep 20, 2016

    @prashaantt

    I'm exploring RethinkDB and would like to settle on the best Node driver. It appears pooling is now the main differentiator between this and rethinkdbdash. Although this thread talks about pooling to be in the works for the official driver as well since 2014, there doesn't seem to be a recent update on it. Can somebody please help me choose, depending on what's on the current roadmap?

  19. internalfx commented on Sep 20, 2016

    @internalfx

    @prashaantt If you use rethinkdbdash, you will not be disappointed.

  20. thomasmodeneis commented on Nov 7, 2016

    @thomasmodeneis

    hey guys any update here ? connection pool and reconnection strategy is must have for javascript driver.

  21. chrisvariety commented on Nov 8, 2016

    @chrisvariety

    @thomasmodeneis #3771 would be the best solution for a JS connection pool.

    But it isn't as much of a "must have" as you'd think. For Postgresql, a connection pool is definitely a must have, but that's not true for Rethinkdb which automatically closes connections after 60 seconds if I recall correctly. (citation needed)

    We utilize Rethinkdb very heavily without a connection pool and everything works smoothly and is quite speedy, but if you're just starting off I'd recommend using rethinkdbdash right off the bat.

  22. raffi-minassian commented on Nov 8, 2016

    @raffi-minassian

    Like @chrisvariety , we too utilize Rethinkdb heavily (from node.js) without a connection pool and everything works extremely well.

    Also, we have some node.js based applications that pull data from a number of different RethinkDB databases depending on a number of properties on the incoming request. That kind of situation gets messy if you try to pool connections.

  23. thomasmodeneis commented on Nov 8, 2016

    @thomasmodeneis

    Hi @chrisvariety and @raffi-minassian
    You are saying that connection pool and reconnection strategy is not a must have, so how do you deal with this ?
    It happens everytime rethinkdb instance need to be restarted or crashes, and right now I have to restart the whole app.
    You are saying for me to use rethinkdbdash but I'm not sure I have the time to (once again) refactor all the api methods and implement it with rethinkdash having in mind that people are now focusing on thinky.

    I don't need any of the "goods" of rethinkdash for this particular api method. I just want this thing to reconnect, any ideas ?

    Cheers.

         info:  ReqlDriverError: Connection is closed.
         at ReqlDriverError.ReqlError [as constructor] (/app/node_modules/rethinkdb/errors.js:23:13)
         at new ReqlDriverError (/app/node_modules/rethinkdb/errors.js:68:50)
         at TcpConnection.Connection._start (/app/node_modules/rethinkdb/net.js:417:13)
         at /app/node_modules/rethinkdb/ast.js:146:29
         at tryCatcher (/app/node_modules/bluebird/js/main/util.js:26:23)
         at Promise._resolveFromResolver (/app/node_modules/bluebird/js/main/promise.js:483:31)
         at new Promise (/app/node_modules/bluebird/js/main/promise.js:71:37)
         at Limit.TermBase.run (/app/node_modules/rethinkdb/ast.js:135:12)
         at getOneBySecondaryIndex (/app/app/server/_api.js:96:10)
         ReqlDriverError: Connection is closed.
         at Command.callback (/app/app/server/server.js:617:9)
         at ReqlDriverError.ReqlError [as constructor] (/app/node_modules/rethinkdb/errors.js:23:13)
         at normal_reply (/app/node_modules/redis/index.js:714:21)
         at new ReqlDriverError (/app/node_modules/rethinkdb/errors.js:68:50)
         at RedisClient.return_reply (/app/node_modules/redis/index.js:816:9)
         at TcpConnection.Connection._start (/app/node_modules/rethinkdb/net.js:417:13)
         at JavascriptRedisParser.Parser.returnReply (/app/node_modules/redis/index.js:188:18)
         at /app/node_modules/rethinkdb/ast.js:146:29
         at JavascriptRedisParser.execute (/app/node_modules/redis-parser/lib/parser.js:415:12)
         at tryCatcher (/app/node_modules/bluebird/js/main/util.js:26:23)
         at Socket.<anonymous> (/app/node_modules/redis/index.js:267:27)
         at Promise._resolveFromResolver (/app/node_modules/bluebird/js/main/promise.js:483:31)
         at new Promise (/app/node_modules/bluebird/js/main/promise.js:71:37)
         at emitOne (events.js:90:13)
         at Limit.TermBase.run (/app/node_modules/rethinkdb/ast.js:135:12)
         at Socket.emit (events.js:182:7)
         at readableAddChunk (_stream_readable.js:153:18)
         at getOneBySecondaryIndex (/app/app/server/_api.js:96:10)
         at Socket.Readable.push (_stream_readable.js:111:10)
         at TCP.onread (net.js:534:20)
         at Command.callback (/app/app/server/server.js:617:9)
         at normal_reply (/app/node_modules/redis/index.js:714:21)
         at RedisClient.return_reply (/app/node_modules/redis/index.js:816:9)
         at JavascriptRedisParser.Parser.returnReply (/app/node_modules/redis/index.js:188:18)
         at JavascriptRedisParser.execute (/app/node_modules/redis-parser/lib/parser.js:415:12)
         at Socket.<anonymous> (/app/node_modules/redis/index.js:267:27)
         at emitOne (events.js:90:13)
         at Socket.emit (events.js:182:7)
         at readableAddChunk (_stream_readable.js:153:18)
         at Socket.Readable.push (_stream_readable.js:111:10)
         at TCP.onread (net.js:534:20)
    
  24. chrisvariety commented on Nov 8, 2016

    @chrisvariety

    @thomasmodeneis If you already have an existing app then no need to rewrite everything in Rethinkdbdash (though it should be mostly drop-in)

    You shouldn't have to restart your whole app after encountering an error like that (assuming Rethinkdb is up), just a retry should make the problem go away. Happy to look at your code if you paste it here.

  25. thomasmodeneis commented on Nov 8, 2016

    @thomasmodeneis

    Ye right I could code the retry myself, but there are dozens of api methods in use and I would have to spend time doing a reconnection strategy just for me. That does not seem constructive for the community.

    My question is rather going straight to the point of this ticket:
    "We should explore various connection pool designs here and see if implementing one might be beneficial." from Feb 2, 2013 and still today Nov 8 2016, the problem still unresolved.

    Correct me if I'm wrong, but this ticket is a must have in any database driver, and the fact that this is the official JS driver and lack of reconnection strategy after +3 years of discussion worries me a little bit.

    Cheers.

  26. raffi-minassian commented on Nov 10, 2016

    @raffi-minassian

    @thomasmodeneis I don't know the details around your layout but in the few apps that we have running against rethinkdb we either open a new connection when we need it, use it, then close it ... OR ... if keeping connections around we have some functions we pass them through before use to make sure they are still good and if not act accordingly, etc.

    I don't know your layout so either style may not be a good approach. Also, either approach would require you to refactor a bunch I guess. So in the end I guess I agree with you that having a connection pool would be nice as it could solve some cases like yours easily. But I also think that it should always be possible to use the driver while managing the connections manually. From what I understand about Rethinkdbdash it is all or nothing in that you have to use the built in pooling.

  27. marshall007 commented on Nov 11, 2016

    @marshall007
    Contributor

    @raffi-minassian you can initialize rethinkdbdash with pool: false and just pass your connection to .run() like you would in the official driver.

  28. raffi-minassian commented on Nov 11, 2016

    @raffi-minassian

    @marshall007 Sweet. I missed that ... should have looked more carefully....

  29. GeoffreyPlitt commented on Oct 23, 2017

    @GeoffreyPlitt

    Without rethinkdbdash, I found a trick: hard-crash on connection errors, and hopefully your server process is wrapped in while(true) or forever or pm2, etc.

    Basically:

    if(err.name==='ReqlDriverError') {
        console.log("Connection error. Crashing and restarting.")
        process.exit(1)
      }
    

    Hoping somebody has a better suggestion

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions