| 1 | ## Address resolution |
| 2 | |
| 3 | Each node in the Cassandra cluster is uniquely identified by an IP address that the driver will use to establish |
| 4 | connections. |
| 5 | |
| 6 | * for contact points, these are provided as part of configuring the `Cluster` object; |
| 7 | * for other nodes, addresses will be discovered dynamically, either by inspecting `system.peers` on already connected |
| 8 | nodes, or via push notifications received from the [control host](../control_connection) when new nodes are discovered |
| 9 | by gossip. |
| 10 | |
| 11 | |
| 12 | ### Cassandra-side configuration |
| 13 | |
| 14 | The address that each Cassandra node shares with clients is the **broadcast RPC address**; it is controlled by various |
| 15 | properties in [cassandra.yaml]: |
| 16 | |
| 17 | * [rpc_address] or [rpc_interface] is the address that the Cassandra process *binds to*. You must set one or the other, |
| 18 | not both (for more details, see the inline comments in the default `cassandra.yaml` that came with your installation); |
| 19 | * [broadcast_rpc_address] \(introduced in Cassandra 2.1) is the address to share with clients, if it is different than |
| 20 | the previous one (the reason for having a separate property is if the bind address is not public to clients, because |
| 21 | there is a router in between). |
| 22 | |
| 23 | If `broadcast_rpc_address` is not set, it defaults to `rpc_address`/`rpc_interface`. If `rpc_address`/`rpc_interface` |
| 24 | is 0.0.0.0 (all interfaces), then `broadcast_rpc_address` *must* be set. |
| 25 | |
| 26 | If you're not sure which address a Cassandra node is broadcasting, launch cqlsh locally on the node, execute the |
| 27 | following query and take node of the result: |
| 28 | |
| 29 | ``` |
| 30 | cqlsh> select broadcast_address from system.local; |
| 31 | |
| 32 | broadcast_address |
| 33 | ------------------- |
| 34 | 172.1.2.3 |
| 35 | ``` |
| 36 | |
| 37 | Then connect to *another* node in the cluster and run the following query, injecting the previous result: |
| 38 | |
| 39 | ``` |
| 40 | cqlsh> select rpc_address from system.peers where peer = '172.1.2.3'; |
| 41 | |
| 42 | rpc_address |
| 43 | ------------- |
| 44 | 1.2.3.4 |
| 45 | ``` |
| 46 | |
| 47 | That last result is the broadcast RPC address. Ensure that it is accessible from the client machine where the driver |
| 48 | will run. |
| 49 | |
| 50 | |
| 51 | ### Driver-side address translation |
| 52 | |
| 53 | Sometimes it's not possible for Cassandra nodes to broadcast addresses that will work for each and every client; for |
| 54 | instance, they might broadcast private IPs because most clients are in the same network, but a particular client could |
| 55 | be on another network and go through a router. |
| 56 | |
| 57 | For such cases, you can register a driver-side component that will perform additional address translation. Write a class |
| 58 | that implements [AddressTranslator] and register an instance with your `Cluster`: |
| 59 | |
| 60 | ```java |
| 61 | public class MyAddressTranslator implements AddressTranslator { |
| 62 | public InetSocketAddress translate(InetSocketAddress address) { |
| 63 | ... // your custom translation logic |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | Cluster cluster = Cluster.builder() |
| 68 | .addContactPoint("1.2.3.4") |
| 69 | .withAddressTranslator(new MyAddressTranslator()) |
| 70 | .build(); |
| 71 | ``` |
| 72 | |
| 73 | Note: the contact points provided while creating the `Cluster` are not translated, only |
| 74 | addresses retrieved from or sent by Cassandra nodes are. |
| 75 | |
| 76 | ### EC2 multi-region |
| 77 | |
| 78 | If you deploy both Cassandra and client applications on Amazon EC2, and your cluster spans multiple regions, you'll have |
| 79 | to configure your Cassandra nodes to broadcast public RPC addresses. |
| 80 | |
| 81 | However, this is not always the most cost-effective: if a client and a node are in the same region, it would be cheaper |
| 82 | to connect over the private IP. Ideally, you'd want to pick the best address in each case. |
| 83 | |
| 84 | The driver provides an address translator that does just that: [EC2MultiRegionAddressTranslator]. |
| 85 | |
| 86 | ```java |
| 87 | Cluster cluster = Cluster.builder() |
| 88 | .addContactPoint("1.2.3.4") |
| 89 | .withAddressTranslator(new EC2MultiRegionAddressTranslator()) |
| 90 | .build(); |
| 91 | ``` |
| 92 | |
| 93 | With this configuration, you keep broadcasting public RPC addresses. But each time the driver connects to a new |
| 94 | Cassandra node: |
| 95 | |
| 96 | * if the node is *in the same EC2 region*, the public IP will be translated to the intra-region private IP; |
| 97 | * otherwise, it will not be translated. |
| 98 | |
| 99 | (To achieve this, `EC2MultiRegionAddressTranslator` performs a reverse DNS lookup of the origin address, to find the |
| 100 | domain name of the target instance. Then it performs a forward DNS lookup of the domain name; the EC2 DNS does the |
| 101 | private/public switch automatically based on location). |
| 102 | |
| 103 | |
| 104 | |
| 105 | [AddressTranslator]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/policies/AddressTranslator.html |
| 106 | [EC2MultiRegionAddressTranslator]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/policies/EC2MultiRegionAddressTranslator.html |
| 107 | |
| 108 | [cassandra.yaml]: https://docs.datastax.com/en/cassandra/3.x/cassandra/configuration/configCassandra_yaml.html |
| 109 | [rpc_address]: https://docs.datastax.com/en/cassandra/3.x/cassandra/configuration/configCassandra_yaml.html?scroll=configCassandra_yaml__rpc_address |
| 110 | [rpc_interface]: https://docs.datastax.com/en/cassandra/3.x/cassandra/configuration/configCassandra_yaml.html?scroll=configCassandra_yaml__rpc_interface |
| 111 | [broadcast_rpc_address]: https://docs.datastax.com/en/cassandra/3.x/cassandra/configuration/configCassandra_yaml.html?scroll=configCassandra_yaml__broadcast_rpc_address |