Network Coordinates

Consul uses a network tomography system to compute network coordinates for nodes in the cluster. These coordinates allow the network round trip time to be estimated between any two nodes using a very simple calculation. This allows for many useful applications, such as finding the service node nearest a requesting node, or failing over to services in the next closest datacenter.

All of this is provided through the use of the Serf library. Serf’s network tomography is based on “Vivaldi: A Decentralized Network Coordinate System”, with some enhancements based on other research. There are more details about Serf’s network coordinates here.

Network Coordinates in Consul

Network coordinates manifest in several ways inside Consul:

  • The consul rtt command can be used to query for the network round trip time between any two nodes.

  • The Catalog endpoints and Health endpoints can sort the results of queries based on the network round trip time from a given node using a “?near=” parameter.

  • Prepared queries can automatically fail over services to other Consul datacenters based on network round trip times. See the Geo Failover for some examples.

  • The Coordinate endpoint exposes raw network coordinates for use in other applications.

Consul uses Serf to manage two different gossip pools, one for the LAN with members of a given datacenter, and one for the WAN which is made up of just the Consul servers in all datacenters. It’s important to note that network coordinates are not compatible between these two pools. LAN coordinates only make sense in calculations with other LAN coordinates, and WAN coordinates only make sense with other WAN coordinates.

Working with Coordinates

Computing the estimated network round trip time between any two nodes is simple once you have their coordinates. Here’s a sample coordinate, as returned from the Coordinate endpoint.

Sample coordinate from Coordinate endpoint

  1. ...
  2. "Coord": {
  3. "Adjustment": 0.1,
  4. "Error": 1.5,
  5. "Height": 0.02,
  6. "Vec": [0.34,0.68,0.003,0.01,0.05,0.1,0.34,0.06]
  7. }
  8. ...

All values are floating point numbers in units of seconds, except for the error term which isn’t used for distance calculations.

Here’s a complete example in Go showing how to compute the distance between two coordinates:

Computing distance between two coordinates with Go

  1. import (
  2. "math"
  3. "time"
  4. "github.com/hashicorp/serf/coordinate"
  5. )
  6. func dist(a *coordinate.Coordinate, b *coordinate.Coordinate) time.Duration {
  7. // Coordinates will always have the same dimensionality, so this is
  8. // just a sanity check.
  9. if len(a.Vec) != len(b.Vec) {
  10. panic("dimensions aren't compatible")
  11. }
  12. // Calculate the Euclidean distance plus the heights.
  13. sumsq := 0.0
  14. for i := 0; i < len(a.Vec); i++ {
  15. diff := a.Vec[i] - b.Vec[i]
  16. sumsq += diff * diff
  17. }
  18. rtt := math.Sqrt(sumsq) + a.Height + b.Height
  19. // Apply the adjustment components, guarding against negatives.
  20. adjusted := rtt + a.Adjustment + b.Adjustment
  21. if adjusted > 0.0 {
  22. rtt = adjusted
  23. }
  24. // Go's times are natively nanoseconds, so we convert from seconds.
  25. const secondsToNanoseconds = 1.0e9
  26. return time.Duration(rtt * secondsToNanoseconds)
  27. }