Skip to content

fix(browser): cap persisted event queue size - #1410

Open
sunitaprajapati89 wants to merge 1 commit into
masterfrom
fix/persisted-queue-size-cap
Open

sunitaprajapati89 wants to merge 1 commit into
masterfrom
fix/persisted-queue-size-cap

Conversation

@sunitaprajapati89

Copy link
Copy Markdown

Summary

Fixes #1261.

Persisted queues for destinations that never become ready (e.g. blocked by an ad blocker) are never flushed. Because maxAttempts only increases on push/retry, buffered events stay at 1 attempt and are never dropped. On each pagehide they are written to localStorage, reloaded on the next visit, and written again, so the queue grows across visits until it exhausts the quota (5MB+ in the reported cases).

Changes

  • Cap each persisted queue at MAX_PERSISTED_ITEMS = 500, dropping the oldest items first. Items already in storage are now ordered before in-memory ones so the newest survive.
  • Prune the persisted seen map to the IDs of the items that were kept, so it can't grow unbounded either.
  • Add a regression test (550 events -> 500 persisted, oldest dropped, seen pruned) and a changeset.

The fix is scoped to the browser PersistedPriorityQueue; the shared PriorityQueue in @segment/analytics-core is untouched.

Notes

  • 500 is a hardcoded default and can be made configurable if preferred.
  • Not included: catching QuotaExceededError on writes (separate branch/PR); the two will touch the same lines in persisted.ts.
  • I haven't reproduced this against a real blocked destination; the test covers the cap itself.

Testing

  • Full browser jest suite: 71 suites passed (876 passed, 4 skipped)
  • tsc --noEmit, eslint and prettier clean for the changed files

🤖 Generated with Claude Code

Limit each persisted queue to 500 items (dropping the oldest) and prune
the persisted `seen` map to the retained items. Queues for destinations
that never become ready (e.g. blocked by an ad blocker) were never
flushed and grew across page loads until they exhausted the
localStorage quota.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@changeset-bot

changeset-bot Bot commented Nov 24, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: a03647d

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@segment/analytics-next Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@twilio-earthly-lunar-3

Copy link
Copy Markdown

🌙 Earthly Lunar

❌ 1 Failing
  • ❌ backstage-report.system-valid - catalog-info.yaml should define a spec.system that exists in Backstage
    • spec.system is not set in catalog-info.yaml. Add a system reference (e.g. payment-platform) that names a System registered in Backstage.

✅ 2 Passing

View all results in Grafana

@codecov

codecov Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.60%. Comparing base (d549330) to head (3c6e3d9).

⚠️ Current head 3c6e3d9 differs from pull request most recent head a03647d

Please upload reports for the commit a03647d to get more accurate results.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1410      +/-   ##
==========================================
+ Coverage   91.59%   91.60%   +0.01%     
==========================================
  Files         128      128              
  Lines        4162     4169       +7     
  Branches     1036     1037       +1     
==========================================
+ Hits         3812     3819       +7     
  Misses        350      350              
Flag Coverage Δ
browser 92.51% <100.00%> (+0.01%) ⬆️
core 90.07% <ø> (ø)
node 89.43% <ø> (ø)

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

This branch has not been deployed

No deployments
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.

Persisted queue for some integrations causes localStorage to fill up to its maximum amount

1 participant