Skip to content

Hedging Detection API: Adds IsHedged() and GetHedgedRegions() to CosmosDiagnostics#5741

Closed
NaluTripician wants to merge 9 commits intomasterfrom
feature/hedging-detection-api
Closed

Hedging Detection API: Adds IsHedged() and GetHedgedRegions() to CosmosDiagnostics#5741
NaluTripician wants to merge 9 commits intomasterfrom
feature/hedging-detection-api

Conversation

@NaluTripician
Copy link
Copy Markdown
Contributor

@NaluTripician NaluTripician commented Apr 7, 2026

Proposal

Users of the Azure Cosmos DB .NET SDK who enable cross-region hedging via AvailabilityStrategy.CrossRegionHedgingStrategy() have no performant, first-class API to determine whether a given response was produced by a hedged request. Today the only way to detect hedging is to call Diagnostics.ToString() and parse the resulting JSON for \"Hedge Context\" — a string-parsing approach that is fragile, allocates heavily, and is inappropriate for hot paths.

What Changes

  • New virtual method IsHedged() on CosmosDiagnostics — returns bool indicating whether the availability strategy activated hedging. Default returns false (back-compat).
  • New virtual method GetHedgedRegions() on CosmosDiagnostics — returns IReadOnlyList<string> containing all regions that received hedge requests, in dispatch order. Default returns an empty list. This mirrors the \"Hedge Context\" datum already stored in diagnostics.
  • Internal boolean + list fields on CosmosTraceDiagnostics — set directly by CrossRegionHedgingAvailabilityStrategy, true O(1) with zero allocations.

Design Decisions

  1. Methods on CosmosDiagnostics (not ResponseMessage) — follows established pattern (GetClientElapsedTime, GetContactedRegions, etc.)
  2. Direct fields (not trace Data dictionary lookup) — avoids SetWalkingStateRecursively() overhead
  3. Virtual methods with defaults (not abstract) — non-breaking change
  4. All hedged regions (not just responding region) — gives users the full observability picture matching the diagnostics trace data

Impact

  • CosmosDiagnostics.cs — 2 new virtual methods
  • CosmosTraceDiagnostics.cs — internal fields + overrides
  • CrossRegionHedgingAvailabilityStrategy.cs — set flags on response

OpenSpec Artifacts

Full design, specs, and task breakdown in openspec/changes/hedging-detection-api/

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines:
Successfully started running 1 pipeline(s).

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines:
Successfully started running 1 pipeline(s).

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines:
Successfully started running 1 pipeline(s).

… all hedged regions

Replaces the single-region GetRespondingRegion() API with GetHedgedRegions()
that returns IReadOnlyList<string> of all regions that received hedge requests,
matching the data already tracked in the diagnostics Hedge Context datum.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines:
Successfully started running 1 pipeline(s).

@NaluTripician NaluTripician changed the title Hedging Detection API: Adds IsHedged() and GetRespondingRegion() to CosmosDiagnostics Hedging Detection API: Adds IsHedged() and GetHedgedRegions() to CosmosDiagnostics Apr 15, 2026
@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines:
Successfully started running 1 pipeline(s).

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines:
Successfully started running 1 pipeline(s).

@NaluTripician NaluTripician marked this pull request as ready for review April 16, 2026 17:16
@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines:
Successfully started running 1 pipeline(s).

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines:
Successfully started running 1 pipeline(s).

@NaluTripician
Copy link
Copy Markdown
Contributor Author

closing, new APIs being proposed in offline discussion

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.

1 participant