Skip to content

[BUG] Batch tasks: flush timing, stale group deadlines, member order and undocumented failure behavior #5385

Open

Description

@Scalahansolo

Environment

  • SDK: TypeScript 1.45.0 and 1.46.0
  • Engine: self-hosted v0.115.1 and v0.116.2

We evaluated batch tasks for a per-group 10 s notification window. Most of the feature worked well: batches respected maxSize, groupMaxRuns held, and no item was lost or delivered twice in our tests. We found the following on engine v0.115.1 with TypeScript SDK 1.45.0, and again on v0.116.2 with SDK 1.46.0. The relevant code on main looked unchanged. We can share minimal scripts for each item.

1. A reused group key can flush its first item at once

The flush deadline is per group, and it is set when the group is created and after a flush (pkg/scheduling/v1/batch_scheduler.go). If a group has been idle for longer than maxInterval and its step's scheduler is still running, the deadline has already passed. The next item then flushes at the next tick, alone, and the items behind it wait for the following flush.

With maxInterval: 10s, on a step kept active by another pending item:

Case Trigger to handler
New group key 10.4–10.6 s
Reused key, idle 15–60 s 0.06–0.29 s
Reused key, next item 1 s after that flush 9.0–9.3 s

This matches the docs' wording, "since the last batch flush", but it means a burst on a reused key is split into a batch of one and the rest. For batching use cases we expected the interval to start at the first buffered item. Could that be the behavior, or an option?

2. Latency on an idle step depends on the lease poll

setBatchSchedulers removes a step's scheduler once it has no pending items. The next item needs a new scheduler, which starts on the 5 s poll in loopForLeases, and only then does its interval start. A step can therefore behave differently depending on unrelated pending items, and the same reused key waits 14.2–14.8 s when the step is idle versus under 1 s when it is active.

Time past maxInterval, trigger to handler, on v0.115.1:

Case p50 p90 Max
Normal task (control) 5–6 ms 9–10 ms 20 ms
Idle step 2.0–3.3 s 4.6–4.9 s 5.0 s
Idle step, maxSize: 1 3.1 s 4.6 s 4.9 s
Active step 0.27–0.34 s 0.42–0.57 s 0.92 s

The delay does not scale with the interval, and it also affects flushes triggered by maxSize.

3. Member order is lost

The handler's input is ordered by member UUID, not by trigger order. sendBatchStartFromPayload in internal/services/dispatcher/dispatcher.go builds a map[uuid.UUID] and json.Marshal sorts its keys. Across 60 groups of 6 items, with items sent one at a time and awaited at spacings from 0 ms to 500 ms, the earliest item came first 9 times, against 10 expected by chance. Which items share a batch stayed in order; only the order within a batch is lost.

Members carry no sequence or timestamp, and ctx.additionalMetadata() is {}. We'd like the order preserved, for example by sending members as a list, or a per-member trigger time. If order is not a goal, the docs could say so.

4. retries is ignored without a warning

With retries: 5, a throwing batch handler runs once and every member fails, while a normal control task with retries: 2 runs three times. The TypeScript types accept retries, and nothing is logged at declaration or registration. The limit appears to be in the SDK (workflow-proto.ts forces 0): when we patched the request to retries: 3, the engine reran the batch to attempt 4. Could the SDK reject or warn on this, and is batch retry support planned?

5. Possible unbounded group retention (from reading the code, not measured)

groupIsIdle returns false whenever flushDeadline is set, and it is always set once a group has an interval. On a step that stays busy, groups for high-cardinality keys may never be evicted.

6. Docs and types

  • The batch tasks page does not mention retries, member failure behavior, delivery guarantees, member order, or groupMaxRuns.
  • BatchTaskConfig says a batch flushes "once groupMaxRuns concurrent batches per group are exceeded". In our tests the limit blocks flushes instead, and items stay buffered.
  • Omitting groupMaxRuns means an engine default of 100, not unlimited. Omitting maxInterval means an engine default of 1000 ms, not no interval. Neither default is documented.

🤖 AI Disclosure
  • I acknowledge that an LLM was used in the creation of this Issue, in accordance with Hatchet's AI_POLICY.md.

  • Details: Claude Code (Claude Opus 5.5) wrote and ran the reproduction scripts, read the engine and SDK code for the diagnosis, and drafted this issue.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions