---
title: "Proxy-Based Load Balancing"
url: https://docs.zeroc.com/ice/3.8/runtime/invocation/proxy-based-load-balancing
docs_index: https://docs.zeroc.com/llms.txt
---

> For an index of every page, see [llms.txt](https://docs.zeroc.com/llms.txt).

# Proxy-Based Load Balancing

Parts of this page are for some programming languages only, and name them. For one language's text alone, see [C++](https://docs.zeroc.com/ice/3.8/runtime/invocation/proxy-based-load-balancing/cpp.md), [C#](https://docs.zeroc.com/ice/3.8/runtime/invocation/proxy-based-load-balancing/csharp.md), [Java](https://docs.zeroc.com/ice/3.8/runtime/invocation/proxy-based-load-balancing/java.md), [JavaScript](https://docs.zeroc.com/ice/3.8/runtime/invocation/proxy-based-load-balancing/js.md), [MATLAB](https://docs.zeroc.com/ice/3.8/runtime/invocation/proxy-based-load-balancing/matlab.md), [PHP](https://docs.zeroc.com/ice/3.8/runtime/invocation/proxy-based-load-balancing/php.md), [Python](https://docs.zeroc.com/ice/3.8/runtime/invocation/proxy-based-load-balancing/python.md), [Ruby](https://docs.zeroc.com/ice/3.8/runtime/invocation/proxy-based-load-balancing/ruby.md), and [Swift](https://docs.zeroc.com/ice/3.8/runtime/invocation/proxy-based-load-balancing/swift.md).

A proxy to a [replicated object](https://docs.zeroc.com/ice/3.8/runtime/invocation/proxies-to-replicated-objects.md) can distribute its invocations over the replicas: a direct proxy holds the endpoints of the replicas, and an indirect proxy gets them from the [locator](https://docs.zeroc.com/ice/3.8/runtime/locators.md). [Locator Semantics for Clients](https://docs.zeroc.com/ice/3.8/runtime/locators/locator-semantics-for-clients.md) describes load balancing with a locator.

## Load Balancing with Connection Caching

By default, a proxy [caches its connection](https://docs.zeroc.com/ice/3.8/runtime/connection-management/connection-establishment.md#connection-reuse-and-caching) and uses the `Random` [endpoint selection type](https://docs.zeroc.com/ice/3.8/runtime/connection-management/connection-establishment.md#endpoint-selection). On the first invocation, Ice reuses an existing connection to any of the proxy's endpoints, including one opened by another proxy; otherwise, it tries the endpoints in random order until it establishes a connection to one of them. The proxy then sends all its invocations over this connection until the connection closes, so the proxies of a communicator that hold the same endpoints typically reach the same server.

With the `Ordered` endpoint selection type, Ice tries the endpoints in the order they appear in the proxy, and still reuses an existing connection to any of them first.

## Per-Request Load Balancing

When a proxy does not cache its connection, Ice selects an endpoint for each invocation, and uses an existing connection to this endpoint or connects to it. With the `Random` endpoint selection type, successive invocations can therefore reach different servers, though not necessarily in equal numbers:

> **C++**
>
> ```cpp
> GreeterPrx proxy{
>     communicator,
>     "greeter:tcp -h 10.0.0.1 -p 4061:tcp -h 10.0.0.2 -p 4061"};
> proxy = proxy.ice_connectionCached(false);
> ```

> **C#**
>
> ```csharp
> var proxy = GreeterPrxHelper.createProxy(
>     communicator,
>     "greeter:tcp -h 10.0.0.1 -p 4061:tcp -h 10.0.0.2 -p 4061");
> proxy = GreeterPrxHelper.uncheckedCast(proxy.ice_connectionCached(false));
> ```

> **Java**
>
> ```java
> var proxy = GreeterPrx.createProxy(
>     communicator,
>     "greeter:tcp -h 10.0.0.1 -p 4061:tcp -h 10.0.0.2 -p 4061");
> proxy = proxy.ice_connectionCached(false);
> ```

> **JavaScript**
>
> ```js
> let proxy = new VisitorCenter.GreeterPrx(
>     communicator,
>     "greeter:tcp -h 10.0.0.1 -p 4061:tcp -h 10.0.0.2 -p 4061");
> proxy = proxy.ice_connectionCached(false);
> ```

> **MATLAB**
>
> ```matlab
> proxy = GreeterPrx(communicator, 'greeter:tcp -h 10.0.0.1 -p 4061:tcp -h 10.0.0.2 -p 4061');
> proxy = proxy.ice_connectionCached(false);
> ```

> **PHP**
>
> ```php
> $proxy = GreeterPrxHelper::createProxy(
>     $communicator,
>     'greeter:tcp -h 10.0.0.1 -p 4061:tcp -h 10.0.0.2 -p 4061');
> $proxy = $proxy->ice_connectionCached(false);
> ```

> **Python**
>
> ```py
> proxy = VisitorCenter.GreeterPrx(
>     communicator,
>     "greeter:tcp -h 10.0.0.1 -p 4061:tcp -h 10.0.0.2 -p 4061")
> proxy = proxy.ice_connectionCached(False)
> ```

> **Ruby**
>
> ```ruby
> proxy = GreeterPrx.new(communicator, "greeter:tcp -h 10.0.0.1 -p 4061:tcp -h 10.0.0.2 -p 4061")
> proxy = proxy.ice_connectionCached(false)
> ```

> **Swift**
>
> ```swift
> var proxy = try makeProxy(
>     communicator: communicator,
>     proxyString: "greeter:tcp -h 10.0.0.1 -p 4061:tcp -h 10.0.0.2 -p 4061",
>     type: GreeterPrx.self)
> proxy = proxy.ice_connectionCached(false)
> ```

With the `Ordered` endpoint selection type, each invocation goes to the first endpoint, and to the next ones only when Ice cannot connect to the first. Ice does not remember a failed connection attempt: while the first server is unavailable, every invocation starts with an attempt to connect to it.

## See Also

- [Terminology](https://docs.zeroc.com/ice/3.8/basics/terminology.md)
- [Connection Establishment](https://docs.zeroc.com/ice/3.8/runtime/connection-management/connection-establishment.md)
- [Ice.Default.\*](https://docs.zeroc.com/ice/3.8/property-reference/ice-default-properties.md)
- [Locator Semantics for Clients](https://docs.zeroc.com/ice/3.8/runtime/locators/locator-semantics-for-clients.md)
- [Load Balancing](https://docs.zeroc.com/ice/3.8/services/icegrid/load-balancing.md)
