Skip to content

Durable Emitter - Backpressure and other improvements - #2570

Open
tarcisiozf wants to merge 4 commits into
mainfrom
durable-emitter-backpressure
Open

tarcisiozf wants to merge 4 commits into
mainfrom
durable-emitter-backpressure

Conversation

@tarcisiozf

@tarcisiozf tarcisiozf commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

What changed

1. Emit back-pressure (opt-in)

  • New Config.BackpressureEnabled (default false) and Config.MaxQueueDepth (default DefaultMaxQueueDepth = 500k).
  • When enabled and the estimated backlog is at or above MaxQueueDepth, Emit returns ErrBacklogged without persisting the event. Rejected events are lost. That's the deliberate trade-off: shed load rather than take the shared DB down. That's also why the flag is off by default.
  • The backlog estimate comes from a new optional store interface, BacklogEstimator:
    • PgDurableEventStore reads pg_stat_user_tables.n_live_tup, a catalog lookup that stays cheap at any table size.
    • It covers rows written by every process sharing the table.
    • It is polled every BacklogPollInterval (default 5s) and seeded at start, so a restart into an existing backlog is covered.
    • Between polls the estimate is not adjusted, so MaxQueueDepth is a soft limit: rejects start and stop up to one BacklogPollInterval late.
  • Fails open: if the estimate query fails, the last value is kept. If the store doesn't implement BacklogEstimator, a warning is logged and back-pressure stays off.
  • New counter: durable_emitter.emit.backpressure_rejects.

2. Retransmit pauses while delivery deletes fail

  • After any failed delivery delete, the retransmit loop skips its ticks for DeleteFailureRetransmitBackoff (default 30s). It resumes once the window passes.
  • This stops the loop of re-reading and re-sending rows that can't be deleted.

3. Only the host scans the queue

  • Only the retransmit-owning (host) emitter runs the queue aggregate query; LOOP plugins no longer do.
  • While back-pressure is engaged, the host skips the scan too and reports depth from the estimate.

4. Per-process metric attribution

  • Every durable emitter metric now carries an emitter_instance attribute:
    • DurableEmitterMetricsConfig.Instance sets it explicitly.
    • Otherwise it defaults to host (retransmit enabled) or plugin.
    • LOOP servers set it to the plugin's logger name (e.g. cron, http-trigger).
  • Series from different processes no longer collide, so rate() and per-process dashboards are correct again.

5. LOOP configuration

New env vars, read by pkg/loop/server.go, so plugins use the same settings as the host:

  • CL_CHIP_INGRESS_DURABLE_EMITTER_BACKPRESSURE_ENABLED
  • CL_CHIP_INGRESS_DURABLE_EMITTER_MAX_QUEUE_DEPTH
  • CL_CHIP_INGRESS_DURABLE_EMITTER_BACKLOG_POLL_INTERVAL

Zero values fall back to the exported defaults: DefaultMaxQueueDepth, DefaultBacklogPollInterval and DefaultDeleteFailureRetransmitBackoff.

@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

✅ API Diff Results - github.com/smartcontractkit/chainlink-common

✅ Compatible Changes (17)

pkg/durableemitter (5)
  • BacklogEstimator — ➕ Added

  • DefaultBacklogPollInterval — ➕ Added

  • DefaultDeleteFailureRetransmitBackoff — ➕ Added

  • DefaultMaxQueueDepth — ➕ Added

  • ErrBacklogged — ➕ Added

pkg/durableemitter.(*PgDurableEventStore) (1)
  • EstimateBacklog — ➕ Added
pkg/durableemitter.Config (4)
  • BacklogPollInterval — ➕ Added

  • BackpressureEnabled — ➕ Added

  • DeleteFailureRetransmitBackoff — ➕ Added

  • MaxQueueDepth — ➕ Added

pkg/durableemitter.DurableEmitterMetricsConfig (1)
  • Instance — ➕ Added
pkg/loop.EnvConfig (3)
  • DurableEmitterBacklogPollInterval — ➕ Added

  • DurableEmitterBackpressureEnabled — ➕ Added

  • DurableEmitterMaxQueueDepth — ➕ Added

pkg/loop/telemetry.TelemetryConfig (3)
  • DurableEmitterBacklogPollInterval — ➕ Added

  • DurableEmitterBackpressureEnabled — ➕ Added

  • DurableEmitterMaxQueueDepth — ➕ Added


📄 View full apidiff report

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.

🟡 Changes recommended

Unresolved findings show that refresh and purge races can undercount the backlog and bypass its threshold, while successful deletes can prematurely cancel the retransmit pause.

2 open issues
What changed in this PR

This PR adds opt-in load shedding and recovery safeguards to the durable telemetry emitter, aiming to reduce database pressure during overload and distinguish metrics from host and LOOP processes.

Changes:

  • Adds configurable backlog-based rejection, PostgreSQL backlog estimates, and LOOP configuration forwarding.
  • Pauses retransmission after delete failures and limits queue scans to the host, skipping them while backlogged.
  • Adds emitter-instance metric labels and tests for backpressure, retransmit recovery, and queue observation.
File Description
pkg/​loop/​telemetry/​config.go Parses and exports backpressure settings.
pkg/​loop/​server.go Applies plugin settings and metric instance names.
pkg/​loop/​config_test.go Tests configuration parsing and environment export.
pkg/​durableemitter/​store.go Adds PostgreSQL statistics-based backlog estimation.
pkg/​durableemitter/​process_stats.go Labels process metrics by emitter instance.
pkg/​durableemitter/​process_stats_stub.go Labels fallback memory metrics by emitter instance.
pkg/​durableemitter/​observable_store.go Defines and instruments the optional backlog estimator.
pkg/​durableemitter/​durable_emitter.go Implements backpressure, delete-failure backoff, and selective queue scans.
pkg/​durableemitter/​durable_emitter_metrics.go Adds rejection metrics and shared instance attributes.
pkg/​durableemitter/​backpressure_test.go Tests rejection, recovery, metric attribution, and scan ownership.

Review effort: Balanced

Comment thread pkg/durableemitter/durable_emitter.go Resolved
Comment thread pkg/durableemitter/durable_emitter.go Resolved
@tarcisiozf
tarcisiozf force-pushed the durable-emitter-backpressure branch from 606a315 to 51e9fd3 Compare November 23, 2026 14:02
@tarcisiozf
tarcisiozf requested a balanced review from Copilot November 23, 2026 14:03
@tarcisiozf
tarcisiozf marked this pull request as ready for review November 23, 2026 14:03
@tarcisiozf
tarcisiozf requested a review from a team as a code owner November 23, 2026 14:03

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.

🟡 Changes recommended

Queue-depth accounting misses concurrent in-flight inserts, discards local deltas after stale polls, and ignores confirmed removals, allowing limit overruns or rejection of events when the queue is empty.

3 open issues
2 resolved since last review

Review effort: Balanced

Comment thread pkg/durableemitter/durable_emitter.go Resolved
Comment thread pkg/durableemitter/durable_emitter.go Resolved
Comment thread pkg/durableemitter/durable_emitter.go Resolved

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.

🟡 Changes recommended

Outstanding insert reservations can be dropped across polls, bypassing back-pressure, and LOOP plugin delete failures do not signal the host to pause retransmission.

1 open issue
1 previously missed issue

In code that hasn't changed since last review

Medium severity Pause retransmits on delivery failures from loop plugins

pkg/​durableemitter/​durable_emitter.go:973

This timestamp only records failures in the host's own emitter; LOOP plugins perform delivery deletes in separate instances and disable retransmit (pkg/loop/server.go:224). If plugin deletes time out, the host continues reading and republishing those rows until a host delete fails, so plugin failures do not trigger the intended immediate pause. Share the failure signal with the retransmit owner and test a plugin/host pair using the same store.

3 resolved since last review

Review effort: Balanced

Comment thread pkg/durableemitter/durable_emitter.go Resolved

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.

🟡 Changes recommended

Queue-depth rollover can misclassify an empty queue as full and reject new events, while cross-process delete-failure backoff and rejection-failure metrics also remain incomplete.

3 open issues
1 resolved since last review

Review effort: Balanced

Comment thread pkg/durableemitter/durable_emitter.go Resolved
Comment thread pkg/durableemitter/durable_emitter.go Resolved
Comment thread pkg/durableemitter/durable_emitter.go Resolved
@tarcisiozf
tarcisiozf force-pushed the durable-emitter-backpressure branch from afddbb6 to 73af7a8 Compare November 23, 2026 16:31
@tarcisiozf
tarcisiozf requested a balanced review from Copilot November 23, 2026 16:34
@tarcisiozf
tarcisiozf marked this pull request as draft November 23, 2026 16:34

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.

🟡 Changes recommended

The backlog estimate changes only after successful polls, so poll failures can allow unchecked queue growth or continued event rejection after draining because local insert, delete, and purge adjustments are missing.

1 open issue
3 resolved since last review

Review effort: Balanced

Comment thread pkg/durableemitter/durable_emitter.go Resolved
@tarcisiozf
tarcisiozf marked this pull request as ready for review November 23, 2026 16:49
// a delivery delete fails, as rows that cannot be deleted would only be re-sent.
// Only this process's deletes count, including those of retransmitted rows.
// Zero defaults to DefaultDeleteFailureRetransmitBackoff.
DeleteFailureRetransmitBackoff time.Duration

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.

Considering our options:

  • Pause delivery
  • Mark a row as delivered in-memory
  • Mark a row as attempted delivery in the db

Have you considered the in-memory approach? The pausing approach is not a strong guarantee


// Defaults for the back-pressure and retransmit backoff settings in Config.
const (
DefaultMaxQueueDepth = 500_000

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.

At 30k TPS, we hit this in >20 seconds. 🤔

}

if d.backlogEstimator != nil {
// Seed before accepting emits so a restart into an existing backlog is covered.

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.

by pre-empting the tick IFIUC we gain a readout a few seconds faster?

Comment thread pkg/durableemitter/durable_emitter.go Resolved
// emitters of processes sharing a node (host and LOOP plugins) export distinct
// series. Empty defaults to "host" for the retransmit-owning process and
// "plugin" otherwise.
Instance string

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.

double checking cardinality here - its linear wrt to node restarts but constant while a node is running?

@patrickhuie19

Copy link
Copy Markdown
Contributor

can we make the DE adaptable to slow queries? if we detect deletes are taking twice the p99 for typical delete latency (should make this configurable), can we backoff on attempting deletes?

Reviewing FRs in order of priority, as this informs what features we choose to create:

  1. not bringing down the nodes DB
  2. transmitting the messages at least once to ChIP

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.

5 participants