GitHub user purushah created a discussion: [Discussion] Connection group: 
failover across deployments of one model (Java)

Hi all. I'd like to propose a chat model connection that stands for several 
deployments of the same model, with failover and a cooldown. I want to agree on 
the shape here before opening a PR.

## Today

A chat model setup uses exactly one connection. When that endpoint is throttled 
or down, the call fails after its retries.

A `MODEL_ROUTER` with `fallback(true)` covers part of this with one setup per 
endpoint. But it keeps no memory between requests, so each request pays for the 
broken endpoint again. Routers also cannot be nested, so deployment failover 
and model routing have to share one router configuration. A connection group 
lets failover sit under model routing and quality escalation.

#1324 proposes the LiteLLM Router for this in Python. LiteLLM has no Java SDK, 
so I think Java needs a native version. The two fit together: the same idea, 
LiteLLM in Python, this in Java.

A gateway in front (the LiteLLM proxy, Portkey) also does this. But it is one 
more service to run and keep available. This proposal is for jobs that call the 
providers directly.

## How others do it

| | A failed backend is | For (default) |
|---|---|---|
| LiteLLM Router | skipped | 5 s |
| Envoy outlier detection | ejected after 5 consecutive errors | 30 s, longer 
with each ejection |
| LangChain4j community `FailoverStrategy` (experimental) | skipped after one 
failure | 1 min |

These are related approaches with different failure and retry rules. LiteLLM 
does not cool a deployment down for most 4xx errors, and it can share the 
cooldown state through Redis. Envoy counts 5xx, timeouts and connection 
failures, and its retries are a route setting, apart from ejection. LangChain4j 
counts any error and keeps the state in memory.

LiteLLM also has the two layers this proposal ends up with: its sub-routers 
(auto, complexity) pick a model group, and the Router then spreads the call 
over the deployments of that group.

## Proposal

```java
ResourceDescriptor group =
        ChatModelConnectionGroup.of("eastus", "westus", "sweden")
                .strategy(Strategy.ROUND_ROBIN)      // PRIORITY (declared 
order) is the default
                .model("sweden", "gpt-4o-swe")       // optional: this member's 
model or deployment name
                .cooldown(Duration.ofSeconds(30))    // the default
                .build();
env.addResource("gpt-group", ResourceType.CHAT_MODEL_CONNECTION, group);
```

Names are illustrative.

- **Shape.** The group is registered as a chat model connection. A setup points 
at it by name. Setups, routers, escalation and `ReActAgent` do not change.
- **Members.** They are expected to serve compatible versions of the same 
model. A member can have its own model name, which replaces the setup's `model` 
for that member. On Azure that is the deployment name. Native structured output 
is used only when every member supports it for the request.
- **One call.** Pick a member and call it. On a failure, call the next one. 
Each member is called at most once per group call. The group adds no backoff. 
Retries inside a member's SDK and the framework's `max-retries` stay as they 
are configured. A cancellation or an interrupt stops the call at once. A JVM 
`Error` is not failed over.
- **When all fail.** The last member's exception is thrown as it is, with the 
earlier ones attached as suppressed.
- **Cooldown.** A member that failed while another member answered the same 
call starts a 30 s cooldown. Members outside cooldown are tried first. Cooling 
members stay as a last resort, the one that recovers first in front. So a 
cooldown moves a member back, it does not remove it, and a call never fails 
only because all members are cooling.
- **Cost of that rule.** When every member fails the same call (a total outage, 
or a request that is too long), no cooldown starts, and the next call tries 
every member again. The group does not reduce traffic in a total outage, and it 
does not keep an invalid request away from any deployment.
- **Strategies.** `PRIORITY`, `ROUND_ROBIN`, `WEIGHTED`.
- **State.** Selection and cooldown state are in memory, thread-safe, and local 
to one group instance in one subtask. Nothing goes into Flink state, and 
recovery resets it. A model call that was recorded is replayed without calling 
the group. A call that was not recorded yet may run again and pick another 
member.
- **Latency.** Members are tried one after another, so one call can take the 
sum of the member timeouts. The docs will say to set them.
- **Validation.** At plan build: every member is a chat model connection of the 
same agent, and a group cannot contain a group.
- **Scope.** Java first, marked `@Experimental(inProgress = true)`, with the 
YAML type alias. No metrics in the first PR. The result metadata names the 
member that answered and the ones that failed.

## Questions

1. Is a connection that wraps connections the right shape? The alternative is a 
new strategy on `MODEL_ROUTER`, where failover and model routing share one 
configuration.
2. Is it fine that Java gets a native version and Python keeps #1324?
3. Most Java connections retry inside `chat()` (OpenAI and Anthropic twice by 
default). Failover is faster with member retries at 0. Should the group force 
that, or only document it and warn at plan build? I lean document.
4. The shared retry path does not classify provider errors by status yet. Some 
integrations do, and #1405 proposes not to retry client errors there. Is the 
rule "a failure counts only when another member answered" fine as a first step?
5. The name: `ChatModelConnectionGroup`. Better ideas are welcome.

References: [LiteLLM routing](https://docs.litellm.ai/docs/routing), [Envoy 
outlier 
detection](https://www.envoyproxy.io/docs/envoy/latest/intro/arch_overview/upstream/outlier),
 [LangChain4j 
`FailoverStrategy`](https://github.com/langchain4j/langchain4j-community/blob/main/models/langchain4j-community-model-router/src/main/java/dev/langchain4j/model/router/FailoverStrategy.java).


GitHub link: https://github.com/apache/flink-agents/discussions/1510

----
This is an automatically sent email for [email protected].
To unsubscribe, please send an email to: [email protected]

Reply via email to