Swift in YTsaurus Flow

Swift is a data processing principle in YTsaurus Flow where the result of a computation isn’t saved in YTsaurus. Instead, the transformation function must be strictly deterministic: if you need the result again (for example, when restarting a job), it’s recomputed from the same input data.

Why you need it

In the classic approach (TTransformComputation), each epoch ends with a transactional write of results to YTsaurus. This ensures exactly-once delivery but puts a load on the cluster: for each input message, the system performs a lookup and writes to the deduplication table, plus one write for each output message.

Swift removes this limitation: if the function is deterministic, you don’t need to store its output — you can reproduce it when needed. This lets you:

  • Reduce the load on YTsaurus to zero or a minimum (only metadata).
  • Increase throughput for stateless transformations.

How exactly-once guarantees are maintained

Even though output data isn’t written to YTsaurus, exactly-once delivery to another Flow computation is preserved thanks to determinism:

  • If a job fails before delivering the result, Flow restarts it and gets the same output from the same input data.
  • The Message Distributor keeps delivering the message until it receives an acknowledgment (MarkPersisted) from the receiver, which prevents data loss.

So, exactly-once is ensured not by storing the output but by the idempotency of the computation and deduplication at the recipient.

TSwiftMapComputation does not support Sink. TSwiftOrderedSourceComputation supports direct delivery to an external sink. Deferred publications provide output-epoch finalization but do not by themselves upgrade at-least-once input to exactly-once.

Determinism requirement

The transformation function in a Swift computation must be deterministic: for the same input data, it must return the same output, including the order of messages.

The requirement also covers lineage — the binding of output messages to their parents: on recomputation, each output must get the same parents in the same order. A typical mistake is iterating over an unordered structure (such as a hash table or a set) when splitting the batch into groups: the order of groups changes from run to run, and the recomputation produces a different output. By default every output message of a Swift computation has exactly one parent; multiple parents are allowed only with allow_batching_with_relaxed_guarantees.

Warning

Breaking determinism when updating a pipeline without a drain can lead to duplicates or loss of intermediate messages: different parts of the system might process different versions of the output.

There can be exceptions to this rule, driven by the pipeline’s business logic specifics. But the developer of the business logic must clearly understand why they’re implementing them and what mechanisms ensure the pipeline’s overall result stays correct.

By default, Flow appends the derived message’s sequence number to its message ID. If two replays of a Swift computation emit different messages at the same position, those messages get the same message ID. This can cause data loss or duplicates; if the messages have different keys, it can also violate Flow’s internal invariants. In C++, Java, Python, and Go, output options can remove emission order from message identity by selecting a payload hash or user-defined suffix (see the C++ API description). This prevents that particular ID collision only when the semantic suffix is stable; it doesn’t make a nondeterministic computation deterministic.

Swift computation classes

Flow implements two base Swift classes:

TSwiftMapComputation

A deterministic Map without materializing results in YTsaurus.

  • Load on YTsaurus: ~0 writes per epoch (only system background processes).
  • Doesn’t support: Source or Sink.
  • Supports: timers and key-visitor streams — only for working with state, for example, for background cleanup (GC). Emitting output messages from timer or visit processing is prohibited: the output stream can’t depend on a timer or visit stream in streams_dependency. Since timer streams are added by default to the dependencies of each output, a spec with timers and outputs must explicitly define streams_dependency.
  • Requires: strict determinism and that each resulting message has exactly one parent — the input message.

For more details, see the Computation (C++) section.

TSwiftPassthroughComputation

A Passthrough computation that inherits from TSwiftMapComputation. It converts input messages to the output stream schema without custom logic. For more details, see Computation (C++).

TSwiftOrderedSourceComputation

The main class for reading ordered data from external sources.

  • Load on YTsaurus: ~1–2 writes per epoch (metadata for recovery; the messages themselves aren’t stored).
  • Supports: Sink and WatermarkStrategy for evaluating watermarks. Direct delivery to an external sink without deduplication is at-least-once.
  • Requires: exactly one Source that implements IOrderedSource.

For more details, see the Computation (C++) section.

TSwiftPassthroughOrderedSourceComputation

A Passthrough computation that inherits from TSwiftOrderedSourceComputation. It converts source messages to the output stream by mapping them to a new schema. For more details, see Computation (C++).

Comparison with TTransformComputation

Type Writes to YTsaurus per epoch Timer support State support Determinism requirement
TTransformComputation 2 per input message and 1 per output message Yes Yes No
TSwiftOrderedSourceComputation ~1–2 (metadata) No No Yes
TSwiftPassthroughOrderedSourceComputation ~1–2 (metadata) No No Yes
TSwiftMapComputation ~0 Yes (state only) Yes Yes
TSwiftPassthroughComputation ~0 No No Yes

See also