fix: batch notification queries to eliminate N+1 cascading#1679
Open
ionfwsrijan wants to merge 1 commit into
Open
fix: batch notification queries to eliminate N+1 cascading#1679ionfwsrijan wants to merge 1 commit into
ionfwsrijan wants to merge 1 commit into
Conversation
9c0ac17 to
b074126
Compare
Contributor
Author
|
@utksh1 Please review this. The failing check is pre-exisiting |
utksh1
requested changes
Jul 9, 2026
utksh1
left a comment
Owner
There was a problem hiding this comment.
The N+1 query batching is a solid improvement, but backend-unit CI is currently failing. Please run pytest locally, fix the failing tests, and push again.
b074126 to
6821f78
Compare
Contributor
Author
|
@utksh1 Please review this now |
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.
Description
The notification delivery pipeline performed a cascading N+1 query pattern: for each finding, it fetched all rules, and for each rule it checked delivery history and recorded it — resulting in 1 + N×(1 + 2M) DB queries. With 50 findings and 5 rules, this was 551 DB queries plus 250 outbound HTTP calls in a single synchronous chain.
Changes
backend/secuscan/notification_service.py:process_task_notifications()— Rewritten to batch all findings and all active rules upfront (2 queries). Evaluates (finding, rule) pairs in-memory. Dedup check uses a single batched query for all existing delivery history.backend/secuscan/notification_service.py:_deliver_rule_batch()— New helper that sends a single webhook per (rule, task) containing an array of findings instead of one webhook per (finding, rule). Reduces HTTP calls from N×M to M.backend/secuscan/executor.py:execute_task()— Notification dispatch is offloaded toasyncio.create_task()so slow webhook targets don't block task finalization.process_finding_notifications()anddeliver_via_rule()are preserved for backward compatibility.Impact
1 + N×(1 + 2M)to3 + M(findings + rules + dedup + M deliveries)event: finding.alert.batchwith afindingsarrayFiles Changed
backend/secuscan/notification_service.py— Major rewrite ofprocess_task_notifications, new_deliver_rule_batchhelperbackend/secuscan/executor.py— 1 line changed (async dispatch via create_task)Closes #1625