Skip to content

fix(subscriptions): back off failed catch-up reads instead of reading on every notification - #125

Merged
yordis merged 12 commits into
mainfrom
yordis/fix-catch-up-retry-backoff
Nov 24, 2026
Merged

yordis merged 12 commits into
mainfrom
yordis/fix-catch-up-retry-backoff

Conversation

@yordis

@yordis yordis commented Nov 24, 2026

Copy link
Copy Markdown
Member

@coderabbitai

coderabbitai Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Explain Change →

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 37 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 441bcb76-4484-4f3a-a13e-8691f4a5bb26
Commits

Reviewing files that changed from the base of the PR and between 9a7d398 and b81c80a.

Files selected for processing: 5

  • guides/Getting Started.md
  • lib/event_store/subscriptions/subscription_fsm.ex
  • lib/event_store/subscriptions/subscription_state.ex
  • test/subscriptions/subscription_catch_up_retry_test.exs
  • test/subscriptions/subscription_fsm_test.exs

No actionable comments were generated in the recent review.

Walkthrough

Catch-up reads now retry with exponential delays from 100 ms up to the configured interval. Successful reads and lock loss reset the retry attempt count. Notifications received during a pending retry record the latest event position without triggering an immediate read.

Changes

Catch-up retry behavior

Layer / File(s) Summary
Retry state and backoff
lib/event_store/subscriptions/subscription_state.ex, lib/event_store/subscriptions/subscription_fsm.ex, test/subscriptions/subscription_fsm_test.exs
The FSM tracks retry attempts and applies capped exponential delays to read errors. Successful and terminal read results, and lock loss, reset the counter. Tests cover delay growth, the cap, and resets.
Events during a pending retry
lib/event_store/subscriptions/subscription_fsm.ex, test/subscriptions/subscription_fsm_test.exs, test/subscriptions/subscription_catch_up_retry_test.exs, guides/Getting Started.md
Notifications record the latest event position without cancelling the pending retry. Tests cover event delivery after retry and acknowledgement through a backlog. The guide describes the retry behavior.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related issues

  • TrogonStack/commanded_eventstore issue 122 — The PR adds tests for retry delays and notification behavior, but does not cover every retry-path test gap listed in the issue.

Possibly related PRs

Suggested reviewers: vheathen

Merge Risk: Minimal · previous review 9a7d3

This change bounds catch-up read retries with exponential backoff and defers notification-triggered reads while a retry is pending. No concrete merge-blocking risk was identified.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: failed catch-up reads now back off exponentially instead of reading on every notification. The changeset adds bounded exponential retry (100 ms start, doubling, capped…
Description check Passed The description is related to the changeset. It explains the problem (a read on every notification after a failed catch-up read), the 100 ms start with doubling up to subscription_retry_interval, and …
Linked Issues check Passed Directly linked issues #119, #120, and #121 are open, so all three set coding requirements.

[#119] Notifications during a pending retry must not start a read. The head code for `SubscriptionFsm.notif…
Out of Scope Changes check Passed All changed files relate to the linked issues or their supporting behavior:
- subscription_fsm.ex: retry deferral for notifications and acks (#119, #120, #121), exponential backoff with reset on suc…
Full details: Title check

Explanation

The title names the main change: failed catch-up reads now back off exponentially instead of reading on every notification. The changeset adds bounded exponential retry (100 ms start, doubling, capped at subscription_retry_interval) in subscription_fsm.ex, a retry_attempt field, and tests for the new behavior. The title is specific and matches the primary objective in the linked issues.

Full details: Description check

Explanation

The description is related to the changeset. It explains the problem (a read on every notification after a failed catch-up read), the 100 ms start with doubling up to subscription_retry_interval, and the lock-loss reset. These match the code and documentation changes in the summary. It also lists the linked issues #119, #120, and #121.

Full details: Linked Issues check

Explanation

Directly linked issues #119, #120, and #121 are open, so all three set coding requirements.

[#119] Notifications during a pending retry must not start a read. The head code for SubscriptionFsm.notify_events/2 in the :catching_up path only calls track_last_received/2 and leaves the retry to read. catch_up/1 still cancels the retry and reads at once, as the issue allows. The PR summary and the test changes in subscription_fsm_test.exs cover this behavior.

[#120] An ack during a pending retry must not wait out the full interval. The head code for ack/3 in :catching_up with retry_timer_ref set records the ack without reading. The retry delay now starts at 100 ms and doubles up to retry_interval. For the issue scenario, where the cause clears about 100 ms after the failed read, the first retry reads the backlog within about 100 ms. The retry-timing and recovery tests in subscription_catch_up_retry_test.exs cover this.

[#121] An event appended during a failed read must not wait out the full interval once the cause is gone. Notifications now record the event, and the 100 ms first retry reads it. The added test appends an event during the failure and asserts delivery after schema restoration.

Residual limit: after several consecutive failures the delay grows toward retry_interval. During a long outage, the first read after recovery can therefore still wait up to that interval. The issue reports only short-failure cases, so this does not fail the linked requirements.

Evidence limit: the retry_catch_up scheduling and backoff code in subscription_fsm.ex was truncated in the read, so the backoff arithmetic rests on the PR summary and the tests, not on a direct read of that code.

Full details: Out of Scope Changes check

Explanation

All changed files relate to the linked issues or their supporting behavior:

  • subscription_fsm.ex: retry deferral for notifications and acks (#119, #120, #121), exponential backoff with reset on success and on lock loss (#120, #121), and the disconnect/2 cancel-and-reset that prevents a stale backoff after reconnect.
  • subscription_state.ex: adds the retry_attempt field that backoff requires.
  • subscription_fsm_test.exs and subscription_catch_up_retry_test.exs: tests for the new timing and recovery behavior. The break_schema/1 helper supports the failure tests.
  • guides/Getting Started.md: documents the changed :subscription_retry_interval behavior.

No change was found that lacks a connection to the linked issues. The subscription_fsm.ex body was truncated in part, so this verdict rests on the visible hunks and the PR summary for the omitted sections.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

A rabbit counts the retries slow,
One hundred milliseconds to go.
The timer waits; new notes are stored,
Then catch-up brings them to the board.
The backlog hops from five to thirty,
And every ack arrives quite sturdy.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 25f9cf83-ce04-4604-974c-ee656f6a685e
Commits

Reviewing files that changed from the base of the PR and between 9066366 and 9a7d398.

Files selected for processing: 5

  • guides/Getting Started.md
  • lib/event_store/subscriptions/subscription_fsm.ex
  • lib/event_store/subscriptions/subscription_state.ex
  • test/subscriptions/subscription_catch_up_retry_test.exs
  • test/subscriptions/subscription_fsm_test.exs

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

…up read

The old test asserted the first retry waits the whole retry interval; the
first retry now fires after 100ms and the warning states the delay used.

Signed-off-by: Yordis Prieto <yordis.prieto@gmail.com>
The first retry waits 100ms, capped at the retry interval, and the warning
states the delay used.

Signed-off-by: Yordis Prieto <yordis.prieto@gmail.com>
…failures

Signed-off-by: Yordis Prieto <yordis.prieto@gmail.com>
…ilures

The delay is capped at the retry interval.

Signed-off-by: Yordis Prieto <yordis.prieto@gmail.com>
Signed-off-by: Yordis Prieto <yordis.prieto@gmail.com>
…succeeds

Signed-off-by: Yordis Prieto <yordis.prieto@gmail.com>
…ending retry

Replaces the test that expected a notification to read at once while a
retry is pending.

Signed-off-by: Yordis Prieto <yordis.prieto@gmail.com>
…notified

A notification of new events while a failed read has a retry pending only
records the events. The retry reads them, so reads are bounded by the
backoff schedule instead of one per notification.

Signed-off-by: Yordis Prieto <yordis.prieto@gmail.com>
…subscription

Covers an event appended during a failed read and an ack-blocked backlog,
both with the default-sized retry interval.

Signed-off-by: Yordis Prieto <yordis.prieto@gmail.com>
Signed-off-by: Yordis Prieto <yordis.prieto@gmail.com>
Signed-off-by: Yordis Prieto <yordis.prieto@gmail.com>
…loses its lock

Signed-off-by: Yordis Prieto <yordis.prieto@gmail.com>
@yordis
yordis force-pushed the yordis/fix-catch-up-retry-backoff branch from 9a7d398 to b81c80a Compare November 24, 2026 05:30
@yordis

yordis commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

cursor review

@cursor

cursor Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
Changes core subscription FSM catch-up and notification behavior during DB failures, which affects event delivery latency and read load under outage conditions.

Overview
Failed subscription catch-up reads now retry with exponential backoff (100ms first, doubling up to :subscription_retry_interval) instead of waiting the full interval or re-reading on every event notification.

notify_events during a pending retry only updates last_received; the scheduled retry delivers those events, preventing busy streams from hammering the DB. Explicit catch-up requests still read immediately. retry_attempt resets after a successful read and when the subscription loses its lock on disconnect, so reconnects do not inherit a long backoff.

Documentation for :subscription_retry_interval is updated to match. Tests cover backoff timing, notification deferral, and faster recovery after short outages.

Reviewed by Cursor Bugbot for commit b81c80a. Bugbot is set up for automated code reviews on this repo. Configure here.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit b81c80a. Configure here.

@yordis
yordis merged commit 93111f0 into main Nov 24, 2026
7 checks passed
@yordis
yordis deleted the yordis/fix-catch-up-retry-backoff branch November 24, 2026 06:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment