Skip to content

Add multi-client random routing strategy for CosmosDB load distribution - #6

Merged
hellices merged 2 commits into
mainfrom
copilot/add-cosmosdb-client-routing-strategy
Nov 8, 2025
Merged

hellices merged 2 commits into
mainfrom
copilot/add-cosmosdb-client-routing-strategy

Conversation

Copilot AI commented Nov 8, 2025 •

Copy link
Copy Markdown
Contributor

The existing preferredLocations approach uses sequential failover, concentrating load on the first region during normal operations. This prevents even utilization of replicas when handling high traffic.

Changes

Added Section 3 to cosmosdb/nodejs/client.md demonstrating multi-client random routing:

Multi-client initialization with rotated priorities:

const cosmosClients = regions.map((region, index) => {
    const preferredLocations = [
        region,
        ...regions.slice(index + 1),
        ...regions.slice(0, index)
    ];
    
    return new CosmosClient({
        endpoint,
        aadCredentials: credential,
        connectionPolicy: { preferredLocations }
    });
});

Random routing for even distribution:

function getRandomClient() {
    const randomIndex = Math.floor(Math.random() * cosmosClients.length);
    return cosmosClients[randomIndex];
}

async function queryWithRandomRouting(databaseId, containerId, querySpec) {
    const client = getRandomClient();
    const container = client.database(databaseId).container(containerId);
    return await container.items.query(querySpec).fetchAll();
}

Key points

  • Creates N clients for N regions, each prioritizing a different region first
  • Randomly selects client per request for statistical load distribution
  • Maintains SDK's automatic failover per client
  • Single-application implementation without deployment splitting
  • Trade-offs: increased connections and memory vs. even replica utilization

Existing content preserved unchanged.

Original prompt

This section details on the original issue you should resolve

<issue_title>Cosmosdb client 라우팅 전략 수립</issue_title>
<issue_description>cosmosdb/nodejs/client.md에 업데이트할 내용

Preferred locations를 사용하면 순차적으로 failover를 수행해서 부하를 몰리는 경향에는 대응이 안됨

Client를 preferredlocations 갯수만큼 생성한 다음 route 코드를 앞에 두어 랜덤하게 클라이언트를 사용할 수 있도록 함으로써 replicas를 고르게 사용할 수 있는 샘플을 아래쪽에 추가해줄 것. 기존 내용은 오류가 없을 시에는 수정하지 말고 최대한 유지하고 위 내용만 추가해줄 것</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: hellices <5661904+hellices@users.noreply.github.com>
Copilot AI changed the title [WIP] Add routing strategy for CosmosDB client Add multi-client random routing strategy for CosmosDB load distribution Nov 8, 2025
Copilot AI requested a review from hellices November 8, 2025 03:55
@hellices

hellices commented Nov 8, 2025

Copy link
Copy Markdown
Owner

Math.flood 로 했을 때 랜덤하게 잘 라우팅될까? @copilot

Copilot AI commented Nov 8, 2025

Copy link
Copy Markdown
Contributor Author

Math.flood 로 했을 때 랜덤하게 잘 라우팅될까? @copilot

네, Math.floor(Math.random() * cosmosClients.length)는 JavaScript에서 랜덤 인덱스를 생성하는 표준 방법입니다.

Math.random()은 0 이상 1 미만의 균등 분포 난수를 반환하므로, 클라이언트 수를 곱하고 Math.floor로 내림하면 각 클라이언트가 통계적으로 동일한 확률로 선택됩니다.

예: 3개의 클라이언트가 있을 때

  • Math.random() * 3 → 0~2.999...의 값
  • Math.floor()로 내림 → 0, 1, 2 중 하나를 균등하게 선택

많은 요청이 발생할수록 각 클라이언트로 약 33.3%씩 균등하게 분산됩니다.

Copilot AI requested a review from hellices November 8, 2025 03:59
@hellices
hellices marked this pull request as ready for review November 8, 2025 03:59
@hellices
hellices merged commit 14e7997 into main Nov 8, 2025
1 check passed
@hellices
hellices deleted the copilot/add-cosmosdb-client-routing-strategy branch July 31, 2026 14:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cosmosdb client 라우팅 전략 수립

2 participants