Skip to content

Comments

[IMPROVED] Remove no interest messages from head of stream#7766

Merged
neilalexander merged 1 commit intomainfrom
maurice/interest-thresh-cleanup
Jan 27, 2026
Merged

[IMPROVED] Remove no interest messages from head of stream#7766
neilalexander merged 1 commit intomainfrom
maurice/interest-thresh-cleanup

Conversation

@MauriceVanVeen
Copy link
Member

When using interest-based streams, messages that no longer have interest should be removed. For example when deleting a consumer that was the last one marking interest of subject A, then all messages matching subject A should be purged from the stream. If this goes above a 100k threshold, this linear scan will be expensive to perform so we don't block any other progress from happening. However, this would leave those messages around until a stream limit is reached.

This PR doesn't fully fix the issue, since the full scan is still expensive, but it does remove any messages at the "head" of the stream that have lost interest. For an active interest stream that's continuously consumed, these messages will be removed naturally without needing to block by going through them and removing immediately, as they'll naturally end up at the "head" of the stream and are eligible for removal.

Resolves #6747, #7754

Signed-off-by: Maurice van Veen github@mauricevanveen.com

@MauriceVanVeen MauriceVanVeen requested a review from a team as a code owner January 26, 2026 10:10
Copy link
Member

@neilalexander neilalexander left a comment

Choose a reason for hiding this comment

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

I'm concerned about the potential size that rmseqs can reach in memory and the potential slowness of calling RemoveMsg O(n) times.

In the first pass, it seems like we could just find the lowest asflr of known consumers and compact up to that? And then on a second pass, we can probably make better estimations from psim in the store without having to page every sequence into memory first.

@MauriceVanVeen MauriceVanVeen force-pushed the maurice/interest-thresh-cleanup branch 4 times, most recently from 517f093 to 6643dc0 Compare January 26, 2026 13:19
@MauriceVanVeen
Copy link
Member Author

it seems like we could just find the lowest asflr of known consumers and compact up to that?

Have updated the PR to do just that. However, the asflr could be below the first sequence permanently for a consumer that's idle/unused.

Therefore, using the o.chkflr which in essence means the same as the asflr, but gets moved up to be "the next message that needs acknowledgement above the ack floor". So this can be used to compact up to.

wallyqs pushed a commit to wallyqs/nats-server that referenced this pull request Jan 26, 2026
Update implementation to match the revised PR nats-io#7766 approach:

- Use store.Compact(asflr) instead of iterating through messages
- Calculate minimum chkflr across all consumers for the compact floor
- Update checkStateForInterestStream to properly calculate retryAsflr
  for filtered consumers using LoadNextMsg to find next matching message

This approach is more efficient:
- No iteration through individual messages
- No lock held during cleanup (Compact is atomic)
- Leverages existing chkflr tracking per consumer
- Handles filtered consumers correctly
@MauriceVanVeen MauriceVanVeen force-pushed the maurice/interest-thresh-cleanup branch from 6643dc0 to 03f065d Compare January 27, 2026 09:45
Signed-off-by: Maurice van Veen <github@mauricevanveen.com>
@MauriceVanVeen MauriceVanVeen force-pushed the maurice/interest-thresh-cleanup branch from 03f065d to 2a2df9d Compare January 27, 2026 10:55
Copy link
Member

@neilalexander neilalexander left a comment

Choose a reason for hiding this comment

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

LGTM

@neilalexander neilalexander merged commit eb86205 into main Jan 27, 2026
69 of 70 checks passed
@neilalexander neilalexander deleted the maurice/interest-thresh-cleanup branch January 27, 2026 11:18
This was referenced Jan 27, 2026
neilalexander added a commit that referenced this pull request Jan 27, 2026
Includes the following:

- #7772
- #7773
- #7769
- #7766

Signed-off-by: Neil Twigg <neil@nats.io>
neilalexander added a commit that referenced this pull request Jan 27, 2026
Includes the following:

- #7772
- #7773
- #7769
- #7766

Signed-off-by: Neil Twigg <neil@nats.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants