Skip to content

Connections & Pooling

Brian Chavez edited this page Sep 14, 2016 · 86 revisions

The C# driver supports two types of connections.

  • A single connection to a RethinkDB server.
  • A set of pooled connections to a RethinkDB cluster.

The advantage of connection pooling is two-fold: redundancy and load balancing. When a connection pool is used, and a connection to a node is interrupted, subsequent queries get routed to different nodes in the cluster. Additionally, various pooling strategies can be used to distribute workloads among nodes in a cluster.

Failure Scenarios

While connection pools offer redundancy and load balancing, the driver will not retry a failed query. A failed query (as a result of a network communication error) can occur when connectivity to a node is lost:

  • Incident threads waiting to write a query to the network stream of a down node will fail. An exception is thrown.
  • Threads awaiting responses from a down node will be notified via an exception (faulted task). This applies to cursors and changefeeds.

In each failure case, when a node goes down, it is the responsibility of the developer to retry a query if needed. The connection pool should detect these failures immediately. However, as a best practice, when a faulted query occurs, please wait at least 1.5 seconds before retrying the query. This delay ensures a higher probability of success on the next retry and allows time for the supervisor to mark other dead nodes. When a node is marked dead, the dead node is skipped from selection until the node's connection can be re-established by the supervisor. In the case when all nodes are down, all queries fail.

Tip: Polly is a helpful .NET utility can help write Circuit Breaker and Wait/Retry code.

Async Connect

Each .Connect() method has an associated .ConnectAsync() for asynchronous establishment of a connection.

Connection Lifetime

Initializing a RethinkDB connection is expensive. A single Connection to a RethinkDB server or a ConnectionPool to a RethinkDB cluster should be created and connected at application start-up, one-time, for the duration of an application's lifetime. A single Connection and a ConnectionPool are both designed to be used by multiple concurrent threads. Additionally, reusing a single Connection and ConnectionPool after a query error is safe as long as the network connection is still functional. Applications using Inversion of Control (IoC) containers should configure a single Connection or a ConnectionPool as singleton lifetimes.


Single Connection (No Pooling)

To create one connection to a RethinkDB server (without connection pooling):

var conn = R.Connection()
            .Hostname(AppSettings.TestHost)
            .Port(AppSettings.TestPort)
            .Timeout(60)
            .Connect();
            
var result = R.Now().Run<DateTimeOffset>(conn);

Multiple threads can share the connection. The returned connection is ready to be used. Connection management is explicit. A failed connection must be manually .reconnect()ed in the event of a failed node.


Connection Pooling

Note: Currently, the Java driver does not support connection pooling. The following APIs are subject to change.

Note: The ConnectonPool does not allow hostnames as seeds for several reasons outlined here. Applications that require DNS hostname seeds must resolve hostnames to IPs before seeding the driver's ConnectionPool.

Round-Robin Strategy

To create a connection pool using a round-robin node selection strategy:

var conn = R.ConnectionPool()
            .Seed(new[] {"192.168.0.11:28015", "192.168.0.12:28015"})
            .PoolingStrategy(new RoundRobinHostPool())
            .Discover(true)
            .Connect();

var result = R.Now().Run<DateTimeOffset>(conn);

The connection can be used by multiple threads. The .Connect() method returns when at least one connection to a node has been successfully established.

The .Seed() method seeds the driver with IP addresses of well-known cluster nodes. If .Discover(true), the driver attempts to discover new nodes when nodes added to the RethinkDB cluster. The driver does this by setting up a change feed on a system table and listens for changes. Additionally, pre-existing nodes (not originally part of the seed list), are automatically discovered when .Discover(true).

The .PoolingStrategy(new RoundRobinHostPool()) provides a round-robin node selection strategy.

There are two construction arguments for RoundRobinHostPool that control reconnection intervals. When a host goes down the pool supervisor waits for a time span of retryDelayInitial before reconnecting to the node. If a reconnection attempt fails, the retry delay doubles. Doubling of the retry delay stops when the doubling of the retry delay is greater than retryDelayMax; after that, every subsequent retry is retryDelayMax.

  • retryDelayInitial - The initial retry delay when a host goes down. How long to wait before immediately retrying the connection. The default is 30 seconds.
  • retryDelayMax - The maximum retry delay. The default is 15 minutes.
Recommended Use

Round Robin selection strategy works well as a general algorithm for applications. Round Robin selection offers little overhead when selecting nodes to run queries. Developers should start with the Round Robin selection strategy until an alternative node selection strategy is needed.

Epsilon Greedy Strategy

Another connection pooling strategy is Epsilon Greedy. Epsilon Greedy is an algorithm that allows the connection pool to learn about "better" servers based on speed and how well they perform. This algorithm gives a weighted request rate to better-performing nodes in the cluster, while still distributing requests to all nodes (proportionate to their performance).

A good overview of Epsilon Greedy is available here.

To setup an epsilon greedy host pool:

var conn = R.ConnectionPool()
       .Seed(new[] { "192.168.0.11:28015" })
       .PoolingStrategy(
           new EpsilonGreedyHostPool(decayDuration, EpsilonCalculator.Linear())
        )
       .Discover(true)
       .Connect();
  • decayDuration - The amount of time to cycle through all EpsilonBuckets (0...120). This decay duration is divided by EpsilonBuckets (default: 5 min / 120 buckets = 2.5 seconds per bucket). IE: The average will be taken every decayDuration/EpsilonBuckets seconds. To use the default decayDuration pass null for this argument.
  • EpsilonCalculator - Given the weighted average among EpsilonBuckets slot measurements, calculate the node's EpsilonValue using EpsilonCalculator.Linear/Logarithmic/Polynomial(exponent).
Recommended Use

Make careful consideration of the decayDuration value before using the Epsilon Greedy selection strategy. For example, when the decayDuration is 5 minutes the algorithm is expecting the average sustained request rate to a RethinkDB cluster will be greater than, on average, 1 request per second per node. The rationale for this is simply the decayDuration. The decayDuration decays (zeroes) averages over a period of time. If the average request rate per node is below one, the application does not benefit from using the Epsilon Greedy selection strategy. When the average request rate is below one, the algorithm behaves similarly to a purely random selection with unnecessary computational overhead. The computational overhead is due to the calculation of the EpsilonValue for every node in the cluster.