Skip to content

Client Encryption: Adds design for reducing encryption path contention.#5626

Open
jeet1995 wants to merge 11 commits intomainfrom
users/jeet1995/reduce-encryption-path-contention
Open

Client Encryption: Adds design for reducing encryption path contention.#5626
jeet1995 wants to merge 11 commits intomainfrom
users/jeet1995/reduce-encryption-path-contention

Conversation

@jeet1995
Copy link
Copy Markdown
Member

@jeet1995 jeet1995 commented Feb 20, 2026

Description

Parent Issue: #5641

Design-only PR — no code changes. Adds OpenSpec artifacts (proposal, design, specs, tasks) for reducing contention in the client-side encryption path.

Problem: OperationCanceledException under concurrent load due to a global SemaphoreSlim(1,1) in BuildProtectedDataEncryptionKeyAsync. The semaphore guards MDE's ProtectedDataEncryptionKey.GetOrCreate, which on cache miss triggers two synchronous Key Vault HTTP calls (Resolve + UnwrapKey). Every encrypted property in every document contends on this single-permit lock.

Proposed fix (3 capabilities, all gated behind opt-in env var):

  1. Async prefetch — call ResolveAsync() + UnwrapKeyAsync() outside the semaphore; sync UnwrapKey inside becomes a cache read (microseconds vs. 200ms–2.4s)
  2. Proactive background refresh — single deduplicated AKV call per key before TTL expiry; prevents thundering herd at the 2-hour cache boundary
  3. Resolved client cache — cache IKeyEncryptionKey per CMK URL so each refresh makes 1 AKV call instead of 2

No public API changes. No breaking changes. Env var AZURE_COSMOS_ENCRYPTION_OPTIMISTIC_DECRYPTION_ENABLED (off by default).

See openspec/changes/reduce-encryption-contention/ for full proposal, design decisions, specs, and implementation tasks.

Type of change

  • This change requires a documentation update

Copy link
Copy Markdown

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All good!

@bartelink
Copy link
Copy Markdown
Contributor

If this isn't dead, probably time to fill in the overview?

@jeet1995 jeet1995 changed the title Users/jeet1995/reduce encryption path contention Client Encryption: Adds design for reducing encryption path contention. Mar 5, 2026
@jeet1995
Copy link
Copy Markdown
Member Author

jeet1995 commented Mar 5, 2026

@bartelink - thanks! I've filled in the PR description (starting this out to document a design) - we'll see if this PR evolves to include code changes (so will keep this PR in a draft state).

Comment thread openspec/changes/reduce-encryption-contention/specs/async-dek-prefetch/spec.md Outdated
Comment thread openspec/changes/reduce-encryption-contention/specs/async-dek-prefetch/spec.md Outdated
Comment thread openspec/changes/reduce-encryption-contention/specs/env-var-feature-gate/spec.md Outdated
Comment thread openspec/changes/reduce-encryption-contention/design.md Outdated
Comment thread openspec/changes/reduce-encryption-contention/design.md Outdated
Comment thread openspec/changes/reduce-encryption-contention/design.md Outdated
Comment thread openspec/changes/reduce-encryption-contention/design.md Outdated
Comment thread openspec/changes/reduce-encryption-contention/design.md Outdated
Comment thread openspec/changes/reduce-encryption-contention/design.md Outdated
Comment thread openspec/changes/reduce-encryption-contention/design.md
@yash2710 yash2710 marked this pull request as ready for review April 8, 2026 18:14
@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).

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.

3 participants