# Load balancing target endpoints without the creation of target servers

**URL:** <https://discuss.google.dev/t/load-balancing-target-endpoints-without-the-creation-of-target-servers/9541>\
**Category:** Apigee\
**Tags:** api-runtime\
**Created:** [September 7, 2016, 1:49am UTC](https://discuss.google.dev/t/load-balancing-target-endpoints-without-the-creation-of-target-servers/9541 "2016-09-07T01:49:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [September 7, 2016, 1:49am UTC](https://discuss.google.dev/t/load-balancing-target-endpoints-without-the-creation-of-target-servers/9541/1 "2016-09-07T01:49:22Z")

</div>

Hi,

I have dynamically configured the target endpoints to route to different hosts in different environments using javascript callout .

Is it possible to load balance these target servers without using the traditional approach of static configuration ? I want to load balance them dynamically .

I would like to load balance target servers without creating target servers externally as below.

```auto
<TargetEndpoint name="default">
    <HTTPTargetConnection>
        <LoadBalancer>
            <Server name="target1" />
        </LoadBalancer>
  </HTTPTargetConnection>
</TargetEndpoint>

```

Thanks

---

<div class="post-metadata">

**Author:** ![madhans](https://d3byx8b92xw3lq.cloudfront.net/user_avatar/discuss.google.dev/madhans/32/77322_2.png) [@madhans](https://discuss.google.dev/u/madhans)\
**Post date:** [September 7, 2016, 3:28am UTC](https://discuss.google.dev/t/load-balancing-target-endpoints-without-the-creation-of-target-servers/9541/2 "2016-09-07T03:28:26Z")

</div>

In short the target server is the out-of-box way of load balancing.

Can javascript be used to make the decision to identify a target and use route rules to pick target endpoint. So each target endpoint can represent a target server group that can participate in load balancing.

The decision can be captured in a flow variable called chosenTarget, so the routerules can look like the following. If this does not address your requirement, can you elaborate your requirement.

```auto
    <RouteRule name="server1">
        <TargetEndpoint>server1</TargetEndpoint>
        <Condition>(chosenTarget = "server1")</Condition>
    </RouteRule>
    <RouteRule name="server2">
        <TargetEndpoint>server2</TargetEndpoint>
        <Condition>(chosenTarget = "server2")</Condition>
    </RouteRule>

```

---

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [September 7, 2016, 3:48am UTC](https://discuss.google.dev/t/load-balancing-target-endpoints-without-the-creation-of-target-servers/9541/3 "2016-09-07T03:48:59Z")

</div>

The above snippet makes use of static configuration of target servers. But I have a configuration present in the javascript callout and I use ‘target.url’ to dynamically route to different targets.

Now, I need to load balance between those target servers. Here, I am not configuring target servers anywhere since all of them are within the javascript callout.

So I need to load balance the below target servers. Is there a way to do it without using the out of the box approach ?

Existing javascript callout that dynamically routesto target servers

```auto
var targetCandidates = [
      'http://api.lo5.at/info',
      'http://dc-echo.herokuapp.com/hello',
      'https://echo.getpostman.com/digest-auth'
    ];

/*need to load balance between the target servers */

context.setVariable('target.url', targetCandidates[chosen]);

```

Thanks
