-
-
Notifications
You must be signed in to change notification settings - Fork 131
Connections & Pooling
The C# driver supports two types of connections. A single connection and a pool of connections.
A single connection is a basic TCP connection to a RethinkDB server. A pool of connections is a set of connections to a RehtinkDB cluster. The advantage of a pooled connection is redundancy and load balancing. When a connection to a node is shutdown, queries are routed to a different nodes that are readily available. Additionally, pooled connections can be used to distribute query workloads in a cluster.
Each .connect() method has an associated .connectAsync() for asynchronous establishment of a connection.
To create a connection (without pooling):
var conn = r.connection()
.hostname(AppSettings.TestHost)
.port(AppSettings.TestPort)
.timeout(60)
.connect();
int result = r.now().run<DateTimeOffset>(conn);
The connection can be shared by multiple threads. The returned connection is ready to be used.
Note: Currently, the Java driver does not support connection pooling. The following APIs are subject to change.
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();
int 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 cluster node has been successfully established.
The .seed() method seeds the driver with a set of well-known cluster nodes. If .discover(true), the driver attempts to discover new hosts when they are added to the RethinkDB cluster. It does this by setting up a change feed on a system table and listens for changes.
The .poolingStrategy(new RoundRobinHostPool()) establishes a round-robin node selection strategy when sending queries. When a needs to be sent to a RethinkDB cluster, nodes are selected in a round-robin fashion.
Another connection pooling strategy is Epsilon Greedy. Epsilon Greedy is an algorithm that allows the pool to not only to track failure state, but also to learn about "better" options in terms of speed, and to pick from available hosts based on how well they perform. This gives a weighted request rate to better performing hosts, while still distributing requests to all hosts (proportionate to their performance).
A good overview of Epsilon Greedy is here http://stevehanov.ca/blog/index.php?id=132
To setup an epsilon greedy host pool:
- Home
- Query Examples
- Logging
- Connections & Pooling
- Extra C# Features
- GOTCHA Goblins!
- LINQ to ReQL Provider
- Differences
- Java ReQL API Documentation