# Dynamic Peers

Dynamic Peers allows you to add new instances of adapters to a running deployment without downtime to add extra [`add-peer`](../datasource/datasource-datasource-peers-configuration-part-1.md#add-peer) configuration to the peers they connect to.

**Available from:** Platform 8

## Overview

In this overview of Dynamic Peers, we’ll look at three different ways of managing peer connections:

* Statically defined peer connections
* Dynamic incoming peer connections **(Dynamic Peers)**
* Dynamic incoming and outgoing peer connections (Discovery)

### Statically defined peer connections

In a deployment with statically defined peer connections, connecting peers require corresponding [`add-peer`](../datasource/datasource-datasource-peers-configuration-part-1.md#add-peer) configuration items: one indicating the peer to connect to, and one indicating the peer to accept a connection from.

**Statically defined peer connections**

```plantuml
[Liberator1\n<color:gray>192.168.1.100</color>] as Liberator1
[PricingAdapter1]

PricingAdapter1 -right-> Liberator1

note bottom of Liberator1
datasrc-local-label Liberator1

<color:blue>~# Incoming peer connection</color>
add-peer
  remote-label PricingAdapter1
end-peer

add-data-service
    service-name PricingService
    add-source-group
        add-priority
            remote-label PricingAdapter1
        end-priority
    end-source-group
end-data-service
end note

note bottom of PricingAdapter1
datasrc-local-label PricingAdapter1

<color:blue>~# Outgoing peer connection</color>
add-peer
  addr 192.168.1.100
  port 14001
end-peer
end note
```

In the deployment above, scaling the number of instances of Liberator or PricingAdapter requires downtime to add extra [`add-peer`](../datasource/datasource-datasource-peers-configuration-part-1.md#add-peer) configuration to the peers.

### Dynamic incoming connections (Dynamic Peers)

Caplin Platform 8 introduces a new feature, **Dynamic Peers**, that provides a secure way to avoid the requirement for [`add-peer`](../datasource/datasource-datasource-peers-configuration-part-1.md#add-peer) definitions of _incoming_ peer connections. It does this with an extended DataSource peer handshake, in which the connecting peer lists the services it provides. If the accepting peer is a consumer of a service that the connecting peer provides, then the connection succeeds. For added security, the handshake can be secured with an optional [API key](../datasource/datasource-apikeys.md).

**Platform 8 Dynamic Peers**

```plantuml
[Liberator1\n<color:gray>192.168.1.100</color>] as Liberator1
[PricingAdapter1]

PricingAdapter1 -right-> Liberator1

note bottom of Liberator1
datasrc-local-label Liberator1

add-data-service
    service-name **PricingService**
    add-source-group
        add-priority
            remote-label-regex **PricingAdapter[0-9]+**
        end-priority
    end-source-group
end-data-service
end note

note bottom of PricingAdapter1
datasrc-local-label PricingAdapter1

<color:blue>~# Outgoing peer connection</color>
add-peer
  addr 192.168.1.100
  port 14001
end-peer

discovery-provide-service **PricingService**
end note
```

In the deployment above, the number of PricingAdapter instances can be scaled dynamically at runtime.

The number of Liberator instances cannot be scaled dynamically at runtime. Downtime is required to add extra [`add-peer`](../datasource/datasource-datasource-peers-configuration-part-1.md#add-peer) configuration to PricingAdapter instances.

The names of services provided by a peer are defined by [`discovery-provide-service`](../datasource/datasource-discovery-configuration.md#discovery-provide-service) and the `service-name` option of [`add-provided-data-service`](../datasource/datasource-data-services-configuration.md#add-provided-data-service) (see [Dynamic Services](datasource-dynamic-services.md)).

The names of services consumed by a peer are defined by `discovery-require-service` (for [Dynamic Services](datasource-dynamic-services.md) provided by connecting peers) and the `service-name` option of statically defined `add-data-service` blocks.

### Dynamic incoming and outgoing connections (Discovery)

Discovery removes the requirement for static [`add-peer`](../datasource/datasource-datasource-peers-configuration-part-1.md#add-peer) configuration for co-located peers, and all co-located peers can be dynamically scaled at runtime:

**Discovery deployment**

```plantuml
[Liberator1] as Liberator1
[PricingAdapter1]
[Discovery1\n<color:gray>192.168.1.200</color>] as Discovery1

PricingAdapter1 -right-> Liberator1
PricingAdapter1 -up- Discovery1
Liberator1 -up- Discovery1

note bottom of Liberator1
datasrc-local-label Liberator1

discovery-addr 192.168.1.200

add-data-service
    service-name **PricingService**
    add-source-group
        add-priority
            remote-label-regex **PricingAdapter[0-9]+**
        end-priority
    end-source-group
end-data-service
end note

note bottom of PricingAdapter1
datasrc-local-label PricingAdapter1

discovery-addr 192.168.1.200

discovery-provide-service **PricingService**
end note
```

When providers of services connect to Discovery they are given a dynamic list of peers interested in consuming their services, making it easy for providing peers to find and connect to their consumers. No [`add-peer`](../datasource/datasource-datasource-peers-configuration-part-1.md#add-peer) configuration is required; peers only need the location of the deployment’s Discovery server.

The services provided by a peer are defined by [`discovery-provide-service`](../datasource/datasource-discovery-configuration.md#discovery-provide-service).

The services consumed by a peer are defined by `discovery-require-service` and the `service-name` option of `add-data-service` blocks.

## Migrating to Dynamic Peers

Follow the steps in [Enabling Dynamic Peers](../datasource/datasource-enabling-dynamic-peers.md) in the DataSource documentation.

## Choosing Dynamic Peers or Discovery’s peer discovery

Dynamic Peers was introduced in Platform 8 to complement, not replace, Discovery’s peer discovery. Thus, the choice is not always either/or. In some use cases, it is advantageous to use both.

**When should I deploy Discovery?**

* If you need to dynamically scale Liberator and/or Transformer, then deploy Discovery.
* If you currently use IP-bound licensing for Caplin Platform products, and you need to deploy to an environment in which IP addresses are not fixed (such as  container-based deployment platforms), then deploy Discovery to take advantage of its centralised license management based on instance numbers.
* If your deployment uses a Transformer cluster, then consider deploying Discovery, which configures Transformer clustering automatically for you.
* If you want to take advantage of Discovery’s web interface that monitors bandwidth and latency across all connections in your deployment, then deploy Discovery.

**When might Dynamic Peers meet all my scaling requirements?**

* Dynamic Peers only offers dynamic scaling of adapters, but in our experience it is adapters, rather than Liberator and Transformer, that most often benefit from dynamic scaling. Thus Dynamic Peers can bring the advantages of dynamic scaling to many deployments without the requirement to migrate to Discovery.

**When do I need both Discovery and Dynamic Peers?**

* Discovery’s peer discovery only works for co-located peers. Peers located off site do not have access to Discovery and so would not ordinarily be dynamically scalable. Platform 8’s Dynamic Peers fills this feature gap.

  **Multi-site deployment**

  ```plantuml
  package "Data Centre 1" {
  [Liberator1\n<color:gray>192.168.1.100</color>] as Liberator1
  [PricingAdapter1]
  [Discovery1\n<color:gray>192.168.1.200</color>] as Discovery1
  }

  package "Data Centre 2" {
      [TradingAdapter1]
  }

  PricingAdapter1 -right-> Liberator1
  PricingAdapter1 -up- Discovery1
  Liberator1 -up- Discovery1
  Liberator1 - "lib1.example.com"
  TradingAdapter1 -down-> "lib1.example.com"

  note bottom of Liberator1
  datasrc-local-label Liberator1

  discovery-addr 192.168.1.200

  add-data-service
      service-name **PricingService**
      add-source-group
          add-priority
              remote-label-regex **PricingAdapter[0-9]+**
          end-priority
      end-source-group
  end-data-service

  add-data-service
      service-name **TradingService**
      add-source-group
          add-priority
              remote-label-regex **TradingAdapter[0-9]+**
          end-priority
      end-source-group
  end-data-service
  end note

  note bottom of PricingAdapter1
  datasrc-local-label PricingAdapter1

  discovery-addr 192.168.1.200

  discovery-provide-service **PricingService**
  end note

  note top of TradingAdapter1
  datasrc-local-label TradingAdapter1

  <color:blue>~# Outgoing peer connection</color>
  add-peer
    addr lib1.example.com
    port 14001
  end-peer

  discovery-provide-service **TradingService**
  end note
  ```

  In the deployment above, Platform 8’s Dynamic Peers allows the number of TradingAdapter instances to be scalable at runtime.
