Skip to content

fix(broadcast): store no expiry when persist has no ttl - #2685

Open
jvoegele wants to merge 2 commits into
mainfrom
jvoegele/real-1290-persist-expires-at-null
Open

jvoegele wants to merge 2 commits into
mainfrom
jvoegele/real-1290-persist-expires-at-null

Conversation

@jvoegele

@jvoegele jvoegele commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

What

A broadcast persisted without a ttl now stores expires_at = null instead of now + 72 hours. Replay never reads further back than 72 hours, so a message with no expires_at still replays for 72 hours, the same window as today.

An explicit ttl works as before.

Why

This is decision 5 of the configurable retention RFC (REAL-1290). The opt-in RFC specified null for this case, but the code stored a fixed timestamp. With a fixed timestamp, a later change to retention can't extend messages that are already stored.

The change doesn't need the retention setting itself, and it isn't urgent. It can land before the retention setting or with it. Stored rows live 72 to about 100 hours, so rows with the old fixed deadline age out within a few days, and no backfill is needed.

Behavior change

Broadcast from Database rows already store expires_at = null. Today they replay until the janitor drops their partition, up to about 100 hours. With the new bound on since, they replay for 72 hours. This matches the docs, which say messages are kept for 3 days.

No backfill. Rows stored with a timestamp keep it and expire on their own.

Nothing changes for clients. A persist without a ttl still replays for 72 hours, and the janitor still drops the row with its partition. No error codes or API docs change.

How it was tested

  • Messages.parse_persist/1 returns ttl: nil for true, %{}, and %{"ttl" => nil}.
  • Messages.expires_at/1 returns nil for nil.
  • Replay doesn't return a row with no expiry from just over 72 hours ago, and does return one from just under. A recent since still applies.
  • replay_query/4 at now just inside and just past 72 hours.
  • WebSocket, single broadcast, and HTTP persist without a ttl store expires_at: nil.
  • The e2e persistence suite expects ttl_seconds to be null for a persist without a ttl. It passes in CI.
  • The replay bound test creates the partitions it writes into, so it doesn't depend on partitions left over from an earlier run.

Review first

replay_query/4 in lib/realtime/messages.ex. The bound is computed in Elixir, not SQL, so the query shape doesn't change: it still has a plain inserted_at range and uses messages_replay_index. The existing "use the replay index" test checks the plan for that shape.

Risk and rollback

Rows written after this change have expires_at = null. If the change is reverted, those rows still replay for 72 hours only through the since bound, which the revert also removes. They would then replay until their partition drops, up to about 100 hours. That's today's behavior for Broadcast from Database rows, so a revert is safe.

Part of REAL-1290.

🤖 Generated with Claude Code

A broadcast persisted without a ttl stored expires_at = now + 72 hours,
so a later change to retention couldn't extend it. Store null instead,
as the opt-in RFC specified, and bound replay's since at 72 hours ago,
so those messages still replay for the same window as before.

The bound also applies to Broadcast from Database rows, which already
store null. They replayed until the janitor dropped their partition, up
to about 100 hours, and now replay for 72 hours.

RFC decision 5, REAL-1290.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown
Contributor

CRAP Score Report

Summary: files=279 functions=1686 scored=1686 worst_score=15944.76

** (Mix) CRAP threshold failed: max_score=30.00
High scores: 30
  lib/extensions/postgres_cdc_rls/subscription_manager.ex Extensions.PostgresCdcRls.SubscriptionManager.handle_info/2 score=37.84
  lib/realtime/adapters/postgres/oid_database.ex Realtime.Adapters.Postgres.OidDatabase.name_for_type_id/1 score=15944.76
  lib/realtime/application.ex Realtime.Application.setup_region_mapping/0 score=47.11
  lib/realtime/nodes.ex Realtime.Nodes.default_region_mapping/1 score=129.85
  lib/realtime/operations.ex Realtime.Operations.rebalance/0 score=42.00
  lib/realtime/operations.ex Realtime.Operations.kill_connections_to_tenant_id/2 score=90.00
  lib/realtime/tenants/connect.ex Realtime.Tenants.Connect.handle_info/2 score=42.56
  lib/realtime_web/channels/auth/jwt_verification.ex RealtimeWeb.JwtVerification.generate_signer/3 score=30.78
  lib/realtime_web/channels/realtime_channel.ex RealtimeWeb.RealtimeChannel.handle_info/2 score=53.95
  lib/realtime_web/dashboard/feature_flags.ex RealtimeWeb.Dashboard.FeatureFlags.handle_event/3 score=112.70
  lib/realtime_web/dashboard/node_info.ex RealtimeWeb.Dashboard.NodeInfo.fetch_node_data/2 score=42.00
  lib/realtime_web/dashboard/recon_trace.ex RealtimeWeb.Dashboard.ReconTrace.handle_event/3 score=552.00
  lib/realtime_web/dashboard/recon_trace.ex RealtimeWeb.Dashboard.ReconTrace.handle_info/2 score=210.00
  lib/realtime_web/dashboard/recon_trace.ex RealtimeWeb.Dashboard.ReconTrace.render_value/2 score=42.00
  lib/realtime_web/dashboard/recon_trace.ex RealtimeWeb.Dashboard.ReconTrace.load_module_functions/1 score=42.00
  lib/realtime_web/dashboard/recon_trace.ex RealtimeWeb.Dashboard.ReconTrace.parse_and_start/2 score=110.00
  lib/realtime_web/dashboard/recon_trace.ex RealtimeWeb.Dashboard.ReconTrace.format_value/1 score=552.00
  lib/realtime_web/dashboard/recon_trace.ex RealtimeWeb.Dashboard.ReconTrace.sort_entries/2 score=42.00
  lib/realtime_web/dashboard/sql_inspector.ex RealtimeWeb.Dashboard.SqlInspector.handle_event/3 score=72.00
  lib/realtime_web/dashboard/sql_inspector.ex RealtimeWeb.Dashboard.SqlInspector.execute_read_only/1 score=72.00
  lib/realtime_web/dashboard/sql_inspector.ex RealtimeWeb.Dashboard.SqlInspector.mask_sensitive_columns/1 score=42.00
  lib/realtime_web/dashboard/sql_inspector.ex RealtimeWeb.Dashboard.SqlInspector.compare_cells/2 score=42.00
  lib/realtime_web/dashboard/sql_inspector.ex RealtimeWeb.Dashboard.SqlInspector.format_cell/1 score=72.00
  lib/realtime_web/dashboard/tenant_migrations.ex RealtimeWeb.Dashboard.TenantMigrations.handle_info/2 score=156.00
  lib/realtime_web/live/components.ex RealtimeWeb.Components.input/1 score=90.00
  lib/realtime_web/live/inspector_live/conn_component.ex RealtimeWeb.InspectorLive.ConnComponent.handle_event/3 score=53.83
  lib/realtime_web/live/inspector_live/event_log_component.ex RealtimeWeb.InspectorLive.EventLogComponent.category_variant/1 score=35.00
  lib/realtime_web/live/inspector_live/event_log_component.ex RealtimeWeb.InspectorLive.EventLogComponent.event_label/1 score=51.43
  lib/realtime_web/live/inspector_live/index.ex RealtimeWeb.InspectorLive.Index.handle_event/3 score=30.84
  lib/realtime_web/live/status_live/index.ex RealtimeWeb.StatusLive.Index.handle_event/3 score=42.00

@coveralls

coveralls commented Nov 30, 2026 •

Copy link
Copy Markdown

Coverage Status

coverage: 93.38% (-0.03%) from 93.413% — jvoegele/real-1290-persist-expires-at-null into main

The test inserted a row from 73 hours ago, but the fixture only creates
partitions from yesterday on. It passed locally because older partitions
were left over, and would fail on a fresh database. Create the older
partitions in the test.

Also write the since bound as Enum.max with NaiveDateTime.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jvoegele
jvoegele marked this pull request as ready for review November 30, 2026 20:57
@jvoegele
jvoegele requested a balanced review from Copilot November 30, 2026 20:58

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues are recorded for the broadcast persistence and replay-window changes.

No open issues 🎉

What changed in this PR

This PR prepares broadcast persistence for configurable retention by storing no expiry when ttl is omitted, while preserving the current 72-hour replay window.

Changes:

  • Parse persistence without a TTL as ttl: nil and store expires_at: nil; explicit TTL behavior remains unchanged.
  • Bound replay queries to the last 72 hours, including for rows with no expiry.
  • Add and update tests for replay boundaries and no-TTL persistence across broadcast entry points.
File Description
test/​realtime/​tenants/​single_broadcast_test.exs Verifies single broadcasts persist with no expiry when TTL is unset.
test/​realtime/​messages_test.exs Tests nil TTL handling, expiry calculation, and replay bounds.
test/​realtime_web/​controllers/​broadcast_single_controller_test.exs Updates HTTP persistence expectations to no expiry.
test/​realtime_web/​channels/​realtime_channel/​broadcast_handler_test.exs Verifies WebSocket persistence stores no expiry without a TTL.
test/​e2e/​src/​suites/​broadcast-persistence.ts Updates nullable TTL types and end-to-end assertions.
lib/​realtime/​messages.ex Implements nil expiry defaults and the 72-hour replay bound.

Review effort: Balanced

@jvoegele
jvoegele requested a review from a team November 30, 2026 21:06

@leandrocp leandrocp left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

That's a good point to make the max ttl dynamic and bound to max retention. Just one comment to make the intention a bit more clear.

Comment thread lib/realtime/messages.ex
`ttl: nil` means they gave no ttl, so the message is kept for the retention.
"""
@type persist :: %{ttl: pos_integer()} | nil
@type persist :: %{ttl: pos_integer() | nil} | nil

@leandrocp leandrocp Nov 30, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we have a more clear type here? Something like :infinity or :max_retention? No preference, just trying to avoid the nil confusion.

Suggested change
@type persist :: %{ttl: pos_integer() | nil} | nil
@type persist :: %{ttl: pos_integer() | :infinity} | nil

persist: nil is already not very clear and adding ttl: nil would make persist even more confusing 😄

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.

4 participants