Skip to content
This repository was archived by the owner on Feb 6, 2026. It is now read-only.

feat(naxum, veritech): add the ability to Nak with a cooldown - #8286

Merged
nickgerace merged 1 commit into
mainfrom
feat/veritech/nak-with-backoff
Jan 16, 2026
Merged

nickgerace merged 1 commit into
mainfrom
feat/veritech/nak-with-backoff

Conversation

@sprutton1

@sprutton1 sprutton1 commented Jan 15, 2026 •

Copy link
Copy Markdown
Contributor

How does this PR change the system?

  • adds an OnFailure handler to naxum that allows for nacking with an exponential backoff
    • Configurable delay, max delay, and number of backoffs
    • Term and warn if exceeded
  • Made it so Veritech uses this handler for function execution requests and included necessary config
    • Made poolnoodle timeout faster when the pool is starved so we can more readily toss messages back in case another Veritech could pick it up
    • Also made a few more of the pool settings configurable to allow for more tuning in prod

Current state: we ask poolnoodle for a jail. If there are none available, we sleep for 100ms and try again, up to max_retries (120, so 12 seconds). If we time out, we throw an ExecutionPoolStarved error and Nak. This immediately gets retried and we go through this process again. This means we would wait up to 60s before giving up. I don't think Pinga would ever be aware of this failure.

New State: we ask poolnoodle for a jail. If there are none available, we sleep for 100ms and try again, up to max_retries (30, so 3 seconds). If we time out, we throw an ExecutionPoolStarved error and Nak with a configurable exponential delay. After the delay, the message gets redelivered.

Backoff Sequence

1s, 2s, 4s, 8s, 16s, 32s, 64s, 120s, 120s, Term

Timeline

Attempt Pool Wait Backoff Cumulative
1 3s 1s 4s
2 3s 2s 9s
3 3s 4s 16s
4 3s 8s 27s
5 3s 16s 46s
6 3s 32s 81s
7 3s 64s 148s
8 3s 120s 271s
9 3s 120s 394s
10 3s Term 397s

Total timeout: ~397 seconds (~6.5 minutes)

The big advantage of this approach is that under load we will slow down redelivering messages to give the Veritechs a chance to catch up. Messages being redelivered gives each instance a fair shot at trying again. Bailing out sooner at the poolnoodle level means we can boot messages out sooner and trigger the cooldown faster.

Screenshots:

2026-01-15T21:44:18.119507Z  WARN ThreadId(55) receive{...}: veritech_server::handlers: execution pool starved, will retry with backoff
2026-01-15T21:44:18.120423Z  WARN ThreadId(31) receive{...}: naxum::middleware::ack::on_failure: max delivery attempts reached, terminating message delivered=1 max_deliver=1 subject="veritech.requests.01KF1SVGQB2Q4STG9RJFH7GZK4.01KF1SVGQBST8AKQSWH43JE2DW.resolverfunction"

How was it tested?

Wrote a little load tester for veritech that swamps it with messages and saw the slow down occur.

  • Integration tests pass
  • Manual test: new functionality works in UI

Does it require a docs change?

  • No
  • Yes, and this PR includes it
  • Yes, and this PR does not include it (reasoning below)

In short: 🔗

@github-actions github-actions Bot added the A-veritech Area: Task execution backend service [Rust] label Jan 15, 2026
@github-actions

github-actions Bot commented Jan 15, 2026 •

Copy link
Copy Markdown

Dependency Review

✅ No vulnerabilities or OpenSSF Scorecard issues found.

Scanned Files

None

@sprutton1

Copy link
Copy Markdown
Contributor Author

/try

@github-actions

github-actions Bot commented Jan 15, 2026 •

Copy link
Copy Markdown

Okay, starting a try! I'll update this comment once it's running...
🚀 Try running here! 🚀

@sprutton1
sprutton1 force-pushed the feat/veritech/nak-with-backoff branch 2 times, most recently from c35360d to 1f27ea4 Compare January 15, 2026 22:25
subject = head.subject.as_str(),
"max delivery attempts reached, terminating message"
);
if let Err(err) = acker.ack_with(jetstream::AckKind::Term).await {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is the part I am not entirely sure about. I think today after we exhaust backoffs it just fails silently (minus poolnoodle yelling) and Pinga is unaware. I'll have to dig more.

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.

In the past, the servers have had their max_retries configured for the corresponding consumer of the stream. You might not even have to worry about terminating the message manually in that instance, but that would also require the server to be configured properly (because if you remove this and the server doesn't set max_retries... infinite backoff).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I am split on what to do here. If I don't terminate, it'll nak one last time and then the message seemingly just disappears? Or something? I suppose that more closely mirrors the current behavior. Open to ideas on what to do here.

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.

You're right... Hmm... I'll think on it. Is veritech the only one using this middleware? If so, I think we can try it without too much fiddling on termination procedures.
@fnichol thoughts?

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.

As I understand/remember it, once a message hits a max_deliver limit, it is no longer going to be delivered by the NATS server and the message itself remains in the stream (and any consumers skip over it like it's not there).

From NAT Jetstream Consumers:

MaxDeliver: The maximum number of times a specific message delivery will be attempted. Applies to any message that is re-sent due to acknowledgment policy (i.e., due to a negative acknowledgment or no acknowledgment sent by the client). The default is -1 (redeliver until acknowledged). Messages that have reached the maximum delivery count will stay in the stream.

Certainly it only looks like Veritech is using the middleware so we may have some room to try it out (or wire up a very simple example under https://github.com/systeminit/si/tree/main/lib/naxum/examples to confirm.

@sprutton1
sprutton1 requested a review from fnichol January 15, 2026 22:29
@sprutton1
sprutton1 marked this pull request as ready for review January 15, 2026 22:29
let (jetstream_message, extensions) = req.split();

// Extract message info BEFORE splitting (contains delivery count for backoff)
let info = Arc::new(match jetstream_message.info() {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

we really just need this so we can check delivered for calculating delays

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.

makes sense to me. we've actually done similar things at the server level, so doing it in naxum makes sense. I wonder if this should be an AckWithInfo instead and we offer both options: one where we extract info and the one that's on main. Thoughts?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I suppose if extracting and carrying this info around is expensive it's worth it to split it out, but I am not sure if this'll fan out painfully. I'm open to it if we think it's worthwhile

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.

It's more protection-oriented and without any real data. I'd say I'm okay either way. Just wanted to throw out the idea in case you didn't want to change the behavior of the other services. @fnichol may have more thoughts.

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.

Yeah, I'm hoping that'll be reasonably lightweight all things considered

@nickgerace nickgerace 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.

Great PR! I dig what I'm seeing 🚀

Comment thread lib/veritech-server/src/handlers.rs Resolved
Comment on lines +3 to +5
/// Information about a received message, extracted from the JetStream reply subject.
#[derive(Debug, Clone)]
pub struct Info {

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.

I love this

Comment on lines +119 to +124
warn!(
delivered = info.delivered,
max_deliver = max_deliver,
subject = head.subject.as_str(),
"max delivery attempts reached, terminating message"
);

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.

Wonderful!

subject = head.subject.as_str(),
"max delivery attempts reached, terminating message"
);
if let Err(err) = acker.ack_with(jetstream::AckKind::Term).await {

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.

In the past, the servers have had their max_retries configured for the corresponding consumer of the stream. You might not even have to worry about terminating the message manually in that instance, but that would also require the server to be configured properly (because if you remove this and the server doesn't set max_retries... infinite backoff).

let (jetstream_message, extensions) = req.split();

// Extract message info BEFORE splitting (contains delivery count for backoff)
let info = Arc::new(match jetstream_message.info() {

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.

makes sense to me. we've actually done similar things at the server level, so doing it in naxum makes sense. I wonder if this should be an AckWithInfo instead and we offer both options: one where we extract info and the one that's on main. Thoughts?

backoff_ms = %backoff.as_millis(),
"nacking message with backoff"
);
if let Err(err) = acker.ack_with(jetstream::AckKind::Nak(Some(backoff))).await {

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.

Does pinga handle this scenario gracefully? We've seen behavior historically that a nack'd message at the veritech requests level can cause pinga to believe the function has failed (cannot remember if that was flakey or consistent).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Before this change, we were just naking with None up to the number of retries. As far as I've seen in testing, Pinga is basically unaware this is happening.

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.

ahhh... so maybe now this will work gracefully... maybe we just never gave it a chance, so it rapidly hit the max retry count without a chance to succeed.

@nickgerace

Copy link
Copy Markdown
Contributor

Also, I ❤️ the data table too. Love seeing some actual data when doing work like this. Neat idea to put the backoff as a middleware in naxum.

@sprutton1
sprutton1 force-pushed the feat/veritech/nak-with-backoff branch from 1f27ea4 to 4bd7910 Compare January 15, 2026 22:59
@sprutton1

Copy link
Copy Markdown
Contributor Author

/try

@github-actions

github-actions Bot commented Jan 15, 2026 •

Copy link
Copy Markdown

Okay, starting a try! I'll update this comment once it's running...
🚀 Try running here! 🚀

@nickgerace
nickgerace enabled auto-merge January 16, 2026 23:21
@nickgerace
nickgerace added this pull request to the merge queue Jan 16, 2026

@fnichol fnichol 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.

This is looking great, and I love that a big part of the solution is adding a small, reusable middleware that can be added in.

.on_response(telemetry_nats::NatsOnResponse::new()),
)
.layer(AckLayer::new())
.layer(AckLayer::new().on_failure(BackoffOnFailure::new(consumer_max_deliver)))

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.

Gotta say, yay for composability!

subject = head.subject.as_str(),
"max delivery attempts reached, terminating message"
);
if let Err(err) = acker.ack_with(jetstream::AckKind::Term).await {

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.

As I understand/remember it, once a message hits a max_deliver limit, it is no longer going to be delivered by the NATS server and the message itself remains in the stream (and any consumers skip over it like it's not there).

From NAT Jetstream Consumers:

MaxDeliver: The maximum number of times a specific message delivery will be attempted. Applies to any message that is re-sent due to acknowledgment policy (i.e., due to a negative acknowledgment or no acknowledgment sent by the client). The default is -1 (redeliver until acknowledged). Messages that have reached the maximum delivery count will stay in the stream.

Certainly it only looks like Veritech is using the middleware so we may have some room to try it out (or wire up a very simple example under https://github.com/systeminit/si/tree/main/lib/naxum/examples to confirm.

let (jetstream_message, extensions) = req.split();

// Extract message info BEFORE splitting (contains delivery count for backoff)
let info = Arc::new(match jetstream_message.info() {

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.

Yeah, I'm hoping that'll be reasonably lightweight all things considered

Merged via the queue into main with commit f519303 Jan 16, 2026
4 checks passed
@nickgerace
nickgerace deleted the feat/veritech/nak-with-backoff branch January 16, 2026 23:42
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

A-veritech Area: Task execution backend service [Rust]

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants