perf(api-service): reduce DB round-trips for inbox mark-seen#10674
Draft
cursor[bot] wants to merge 1 commit intonextfrom
Draft
perf(api-service): reduce DB round-trips for inbox mark-seen#10674cursor[bot] wants to merge 1 commit intonextfrom
cursor[bot] wants to merge 1 commit intonextfrom
Conversation
- MessageRepository: larger status-update chunks and ordered bulkWrite pairing main update with firstSeenDate backfill (same write concern) - BaseRepository.bulkWrite: optional writeConcern/session passthrough - MarkNotificationsAsSeen: larger per-call ID batches to reduce prefetch finds Co-authored-by: Dima Grossman <dima@grossman.io>
✅ Deploy Preview for dashboard-v2-novu-staging canceled.
|
Contributor
|
Hey there and thank you for opening this pull request! 👋 We require pull request titles to follow specific formatting rules and it looks like your proposed title needs to be adjusted. Your PR title is: Requirements:
Expected format: Details: PR title must end with 'fixes TICKET-ID' (e.g., 'fixes NOV-123') or include ticket ID in branch name |
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.
Context
APM data for the production US API over the last day showed the inbox “mark notifications as seen” web transaction as the slowest among high-volume routes, with database time as the dominant contributor and external time present but secondary. Error rate for that route was negligible relative to other endpoints, so the change targets latency, not correctness.
Root cause
The handler ultimately goes through
MessageRepository.updateMessagesStatus, which:_ids for the filter.updateMany-equivalent for the status fields, then a second conditional update forfirstSeenDatewhen marking as seen.With the previous batch size, a single request that touched many messages issued many sequential database round-trips (two writes per batch). The inbox use case also split explicit ID updates into small batches, which multiplied the preliminary ID find work.
What we changed
MessageRepository: Use a larger ID chunk for status updates and, for the “mark as seen” path, send the paired updates as one orderedbulkWriteper chunk (still honoring the prior write concern). This preserves behavior (includingfirstSeenDatebackfill) while cutting round-trips.BaseRepository.bulkWrite: Allow optionalwriteConcern/sessionso repositories can pass through Mongo options without touching_modeldirectly.MarkNotificationsAsSeen: Increase the per-repository batch size when updating by notification IDs so fewer full find+update cycles run for large ID lists.Risk / review notes
bulkWritekeeps the main status update before thefirstSeenDateconditional update within each chunk.$inqueries vs fewer round-trips.Testing
pnpm exec tsc -p libs/dal/tsconfig.json --noEmitNODE_OPTIONS=--no-experimental-strip-types, which Node v20 rejects.