Batch queries to avoid exceeding RavenDB session request limit#5372
Open
Batch queries to avoid exceeding RavenDB session request limit#5372
Conversation
…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.
SzymonPobiega
approved these changes
Mar 11, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The method
UpdateUserIndicatorOnEndpointsopens 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:
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 == SanitizedNamenever issue extra queries, regardless of count:SalesSalesSalesBillingBillingBillingShippingShippingShippingSchema-prefixed names also succeed as long as fewer than 29 are updated at once:
"public"."Sales"Sales"public"."Sales""public"."Sales","public"."Billing"Sales,BillingFails
[dbo].[x]endpoints