Skip to content

Batch queries to avoid exceeding RavenDB session request limit#5372

Open
dvdstelt wants to merge 1 commit intomasterfrom
batching-fix
Open

Batch queries to avoid exceeding RavenDB session request limit#5372
dvdstelt wants to merge 1 commit intomasterfrom
batching-fix

Conversation

@dvdstelt
Copy link
Member

@dvdstelt dvdstelt commented Mar 10, 2026

The method UpdateUserIndicatorOnEndpoints opens a single RavenDB session but issues a new database query inside the foreach loop for every endpoint that matches by EndpointId.Name rather than SanitizedName. RavenDB enforces a default limit of 30 requests per session as an early warning against chatty code.

Request budget consumed:

  • 1 request: initial query loading all matching endpoint documents
  • +1 request per else if branch hit: the sibling propagation query (sanitizedMatchingQuery)

So updating 30 endpoints that all match by raw name (not sanitized name) uses exactly 30 requests and throws on the 30th.

Why it happens: When a user updates many endpoints from the UI, the names in the request correspond to raw endpoint names (e.g. "public"."Endpoint" from a PostgreSQL broker). These match by EndpointId.Name, not SanitizedName, so every one of them hits the else if branch.

Fix (LicensingDataStore.cs): Instead of one query per iteration, the loop now just collects (sanitizedName -> userIndicator) pairs into a dictionary. After the loop, if any sibling propagation is needed, a single batched query fetches all sibling documents at once using .Where(d => d.SanitizedName.In(sanitizedNames)). The total session request count is capped at 2 regardless of the update list size.

Examples

When a queue name differs from its sanitized name (e.g. schema-prefixed names on PostgreSQL or SQL Server), each endpoint update issues an extra database query inside the session. RavenDB limits a session to 30 requests total, so failures depend on how many such endpoints are updated at once.

Succeeds

Simple queue names where EndpointId.Name == SanitizedName never issue extra queries, regardless of count:

EndpointId.Name SanitizedName Update key
Sales Sales Sales
Billing Billing Billing
Shipping Shipping Shipping

Schema-prefixed names also succeed as long as fewer than 29 are updated at once:

EndpointId.Name SanitizedName Update key Session requests
"public"."Sales" Sales "public"."Sales" 2
"public"."Sales", "public"."Billing" Sales, Billing both 3
28 schema-prefixed endpoints ... ... 29 — OK

Fails

EndpointId.Name SanitizedName Update key Session requests
29 schema-prefixed endpoints ... ... 30 — throws
50 [dbo].[x] endpoints ... ... 51 — throws

…n request limit

When updating user indicators on 30+ endpoints where names match by
EndpointId.Name rather than SanitizedName, the previous code issued one
extra DB query per endpoint inside the loop to propagate the indicator to
sibling documents sharing the same SanitizedName. This exceeded RavenDB's
default limit of 30 requests per session.

Replace the per-iteration inner queries with a single batched query after
the loop, capping total session requests at 2 regardless of input size.
@dvdstelt dvdstelt self-assigned this Mar 10, 2026
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.

2 participants