---
metadata:
  - name: generator
    content: Diplodoc Platform v5.50.6
alternate:
  - https://ytsaurus.tech/docs/en/flow/concepts/companion.md
  - https://ytsaurus.tech/docs/ru/flow/concepts/companion.md
---
> **Documentation Index:** Fetch the complete configuration index at https://ytsaurus.tech/docs/en/llms.txt

<!-- source: en/_includes/flow/concepts/companion.md -->
# Companion in YTsaurus Flow

In Flow, you can run user code in a separate process. This process is called a companion process.

## Companion use cases
### Currently used

- Supporting computations in languages other than [C++](https://ytsaurus.tech/docs/en/flow/cpp/getting-started.md), such as [Python](https://ytsaurus.tech/docs/en/flow/python/getting-started.md), [Java and Kotlin](https://ytsaurus.tech/docs/en/flow/java/getting-started.md), and [Go](https://ytsaurus.tech/docs/en/flow/go/getting-started.md).
- Running user C++ code in a separate process through the [C++ companion](#cpp-companion).

### Planned

- Hot updating user code without stopping the [pipeline](https://ytsaurus.tech/docs/en/flow/concepts/glossary.md#pipeline).

## Workflow {#schema}

When you use a companion, the [Computation](https://ytsaurus.tech/docs/en/flow/concepts/glossary.md#stream-and-computation) consists of two parts: a specialized Computation on the [Worker](https://ytsaurus.tech/docs/en/flow/concepts/glossary.md#worker) side and a lightweight Computation on the companion side.

{% note info %}

You develop all business logic in the chosen programming language on the companion side, while the pipeline structure is still configured via the [spec](https://ytsaurus.tech/docs/en/flow/concepts/spec.md). In this workflow, the worker becomes an infrastructure binary that doesn’t depend on the pipeline logic. So, when you use Python, Go, Java, or Kotlin, you don’t need to write any C++ user code.

{% endnote %}

The Computation on the Worker side collects a batch of messages, enriches it with all the information needed for processing (states, parameters, watermark values, and so on), and sends it to the companion via gRPC locally, within a single host.

The batch is formed without regard to [keys](https://ytsaurus.tech/docs/en/flow/concepts/glossary.md#key) — one request may contain messages with different keys; this is how the worker collects batches for all computations. There is no per-key grouping in the companion protocol: if the business logic needs per-key processing, it is done in the companion code — see the [Python](https://ytsaurus.tech/docs/en/flow/python/computation.md#batch-function) example.

The companion returns its output in groups; each group carries [lineage](https://ytsaurus.tech/docs/en/flow/concepts/lineage.md) — the list of ids of the input messages of the batch its output was derived from (lineage is unrelated to keys). For when lineage must be set explicitly and what exactly to pass, see [When to set lineage explicitly](https://ytsaurus.tech/docs/en/flow/concepts/lineage.md#explicit-lineage).

In the future, you’ll also be able to use Unix sockets.

<svg version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1208.445638020834 574.5789930555557" width="2416.891276041668" height="1149.1579861111113"><metadata/><defs></defs><rect x="0" y="0" width="1208.445638020834" height="574.5789930555557" fill="#ffffff"/><g stroke-linecap="round" transform="translate(643.9894748263896 184.18894546751199) rotate(0 277.2280815972222 144.85329861111086)"><path d="M32 0 C148.34 0, 264.68 0, 522.46 0 C543.79 0, 554.46 10.67, 554.46 32 C554.46 87.13, 554.46 142.26, 554.46 257.71 C554.46 279.04, 543.79 289.71, 522.46 289.71 C380.41 289.71, 238.37 289.71, 32 289.71 C10.67 289.71, 0 279.04, 0 257.71 C0 167.76, 0 77.8, 0 32 C0 10.67, 10.67 0, 32 0" stroke="none" stroke-width="0" fill="#99e9f2"/><path d="M32 0 C219.48 0, 406.96 0, 522.46 0 M32 0 C217.7 0, 403.4 0, 522.46 0 M522.46 0 C543.79 0, 554.46 10.67, 554.46 32 M522.46 0 C543.79 0, 554.46 10.67, 554.46 32 M554.46 32 C554.46 78.15, 554.46 124.3, 554.46 257.71 M554.46 32 C554.46 96.05, 554.46 160.09, 554.46 257.71 M554.46 257.71 C554.46 279.04, 543.79 289.71, 522.46 289.71 M554.46 257.71 C554.46 279.04, 543.79 289.71, 522.46 289.71 M522.46 289.71 C367.16 289.71, 211.87 289.71, 32 289.71 M522.46 289.71 C347.73 289.71, 173.01 289.71, 32 289.71 M32 289.71 C10.67 289.71, 0 279.04, 0 257.71 M32 289.71 C10.67 289.71, 0 279.04, 0 257.71 M0 257.71 C0 174.39, 0 91.07, 0 32 M0 257.71 C0 194.91, 0 132.12, 0 32 M0 32 C0 10.67, 10.67 0, 32 0 M0 32 C0 10.67, 10.67 0, 32 0" stroke="#1e1e1e" stroke-width="1" fill="none"/></g><g transform="translate(829.8376278347446 189.18894546751199) rotate(0 91.37992858886719 12.5)"><text x="91.37992858886719" y="17.619999999999997" font-family="Excalifont, Xiaolai, sans-serif, Segoe UI Emoji" font-size="20px" fill="#1e1e1e" text-anchor="middle" style="white-space: pre;" direction="ltr" dominant-baseline="alphabetic">Companion Process</text></g><g stroke-linecap="round" transform="translate(10 186.09862118675665) rotate(0 298.95963541666697 144.513671875)"><path d="M32 0 C176.89 0, 321.79 0, 565.92 0 C587.25 0, 597.92 10.67, 597.92 32 C597.92 77.11, 597.92 122.22, 597.92 257.03 C597.92 278.36, 587.25 289.03, 565.92 289.03 C455.48 289.03, 345.04 289.03, 32 289.03 C10.67 289.03, 0 278.36, 0 257.03 C0 211.48, 0 165.93, 0 32 C0 10.67, 10.67 0, 32 0" stroke="none" stroke-width="0" fill="#a5d8ff"/><path d="M32 0 C203.29 0, 374.57 0, 565.92 0 M32 0 C199.45 0, 366.9 0, 565.92 0 M565.92 0 C587.25 0, 597.92 10.67, 597.92 32 M565.92 0 C587.25 0, 597.92 10.67, 597.92 32 M597.92 32 C597.92 102.12, 597.92 172.23, 597.92 257.03 M597.92 32 C597.92 115.32, 597.92 198.64, 597.92 257.03 M597.92 257.03 C597.92 278.36, 587.25 289.03, 565.92 289.03 M597.92 257.03 C597.92 278.36, 587.25 289.03, 565.92 289.03 M565.92 289.03 C415.32 289.03, 264.71 289.03, 32 289.03 M565.92 289.03 C377.57 289.03, 189.21 289.03, 32 289.03 M32 289.03 C10.67 289.03, 0 278.36, 0 257.03 M32 289.03 C10.67 289.03, 0 278.36, 0 257.03 M0 257.03 C0 189.47, 0 121.9, 0 32 M0 257.03 C0 179.53, 0 102.03, 0 32 M0 32 C0 10.67, 10.67 0, 32 0 M0 32 C0 10.67, 10.67 0, 32 0" stroke="#1e1e1e" stroke-width="1" fill="none"/></g><g transform="translate(179.07972208658884 191.09862118675665) rotate(0 129.87991333007812 12.5)"><text x="129.87991333007812" y="17.619999999999997" font-family="Excalifont, Xiaolai, sans-serif, Segoe UI Emoji" font-size="20px" fill="#1e1e1e" text-anchor="middle" style="white-space: pre;" direction="ltr" dominant-baseline="alphabetic">Flow Worker Process (C++)</text></g><g stroke-linecap="round" transform="translate(680.931423611109 238.55348229786887) rotate(0 40.497829861111086 86.36805555555566)"><path d="M20.25 0 C35.34 0, 50.43 0, 60.75 0 C74.25 0, 81 6.75, 81 20.25 C81 60.5, 81 100.76, 81 152.49 C81 165.99, 74.25 172.74, 60.75 172.74 C50.86 172.74, 40.97 172.74, 20.25 172.74 C6.75 172.74, 0 165.99, 0 152.49 C0 106.01, 0 59.53, 0 20.25 C0 6.75, 6.75 0, 20.25 0" stroke="none" stroke-width="0" fill="#b2f2bb"/><path d="M20.25 0 C36.21 0, 52.17 0, 60.75 0 M20.25 0 C34.23 0, 48.2 0, 60.75 0 M60.75 0 C74.25 0, 81 6.75, 81 20.25 M60.75 0 C74.25 0, 81 6.75, 81 20.25 M81 20.25 C81 57.88, 81 95.51, 81 152.49 M81 20.25 C81 63.94, 81 107.63, 81 152.49 M81 152.49 C81 165.99, 74.25 172.74, 60.75 172.74 M81 152.49 C81 165.99, 74.25 172.74, 60.75 172.74 M60.75 172.74 C46.07 172.74, 31.39 172.74, 20.25 172.74 M60.75 172.74 C44.77 172.74, 28.79 172.74, 20.25 172.74 M20.25 172.74 C6.75 172.74, 0 165.99, 0 152.49 M20.25 172.74 C6.75 172.74, 0 165.99, 0 152.49 M0 152.49 C0 101.13, 0 49.78, 0 20.25 M0 152.49 C0 106.61, 0 60.73, 0 20.25 M0 20.25 C0 6.75, 6.75 0, 20.25 0 M0 20.25 C0 6.75, 6.75 0, 20.25 0" stroke="#1e1e1e" stroke-width="1" fill="none"/></g><g transform="translate(690.9792794121618 299.9215378534245) rotate(0 30.449974060058594 25)"><text x="30.449974060058594" y="17.619999999999997" font-family="Excalifont, Xiaolai, sans-serif, Segoe UI Emoji" font-size="20px" fill="#1e1e1e" text-anchor="middle" style="white-space: pre;" direction="ltr" dominant-baseline="alphabetic">gRPC</text><text x="30.449974060058594" y="42.62" font-family="Excalifont, Xiaolai, sans-serif, Segoe UI Emoji" font-size="20px" fill="#1e1e1e" text-anchor="middle" style="white-space: pre;" direction="ltr" dominant-baseline="alphabetic">Server</text></g><g stroke-linecap="round" transform="translate(495.94270833333303 235.2553052145322) rotate(0 42.32725694444434 90.68229166666652)"><path d="M21.16 0 C35 0, 48.83 0, 63.49 0 C77.6 0, 84.65 7.05, 84.65 21.16 C84.65 67.74, 84.65 114.32, 84.65 160.2 C84.65 174.31, 77.6 181.36, 63.49 181.36 C47.82 181.36, 32.15 181.36, 21.16 181.36 C7.05 181.36, 0 174.31, 0 160.2 C0 130.07, 0 99.95, 0 21.16 C0 7.05, 7.05 0, 21.16 0" stroke="none" stroke-width="0" fill="#b2f2bb"/><path d="M21.16 0 C36.27 0, 51.37 0, 63.49 0 M21.16 0 C33.02 0, 44.88 0, 63.49 0 M63.49 0 C77.6 0, 84.65 7.05, 84.65 21.16 M63.49 0 C77.6 0, 84.65 7.05, 84.65 21.16 M84.65 21.16 C84.65 57.44, 84.65 93.72, 84.65 160.2 M84.65 21.16 C84.65 57.77, 84.65 94.38, 84.65 160.2 M84.65 160.2 C84.65 174.31, 77.6 181.36, 63.49 181.36 M84.65 160.2 C84.65 174.31, 77.6 181.36, 63.49 181.36 M63.49 181.36 C49.6 181.36, 35.71 181.36, 21.16 181.36 M63.49 181.36 C54.06 181.36, 44.63 181.36, 21.16 181.36 M21.16 181.36 C7.05 181.36, 0 174.31, 0 160.2 M21.16 181.36 C7.05 181.36, 0 174.31, 0 160.2 M0 160.2 C0 120.35, 0 80.49, 0 21.16 M0 160.2 C0 129.7, 0 99.2, 0 21.16 M0 21.16 C0 7.05, 7.05 0, 21.16 0 M0 21.16 C0 7.05, 7.05 0, 21.16 0" stroke="#1e1e1e" stroke-width="1" fill="none"/></g><g transform="translate(511.12998877631253 300.93759688119826) rotate(0 27.139976501464844 25)"><text x="27.139976501464844" y="17.619999999999997" font-family="Excalifont, Xiaolai, sans-serif, Segoe UI Emoji" font-size="20px" fill="#1e1e1e" text-anchor="middle" style="white-space: pre;" direction="ltr" dominant-baseline="alphabetic">RPC</text><text x="27.139976501464844" y="42.62" font-family="Excalifont, Xiaolai, sans-serif, Segoe UI Emoji" font-size="20px" fill="#1e1e1e" text-anchor="middle" style="white-space: pre;" direction="ltr" dominant-baseline="alphabetic">Client</text></g><g stroke-linecap="round"><g transform="translate(583.4644097222217 297.535387742676) rotate(0 48.20811631944366 0.9815619581650026)"><path d="M0 0 C16.07 0.33, 80.35 1.64, 96.42 1.96 M0 0 C16.07 0.33, 80.35 1.64, 96.42 1.96" stroke="#1e1e1e" stroke-width="1" fill="none"/></g><g transform="translate(583.4644097222217 297.535387742676) rotate(0 48.20811631944366 0.9815619581650026)"><path d="M72.75 10.03 C80.11 7.53, 87.46 5.02, 96.42 1.96 M72.75 10.03 C78.17 8.19, 83.58 6.34, 96.42 1.96" stroke="#1e1e1e" stroke-width="1" fill="none"/></g><g transform="translate(583.4644097222217 297.535387742676) rotate(0 48.20811631944366 0.9815619581650026)"><path d="M73.1 -7.06 C80.35 -4.26, 87.59 -1.46, 96.42 1.96 M73.1 -7.06 C78.44 -5, 83.77 -2.93, 96.42 1.96" stroke="#1e1e1e" stroke-width="1" fill="none"/></g></g><mask/><g stroke-linecap="round"><g transform="translate(678.837673611109 351.13374690371165) rotate(0 -48.26280381944389 -0.2737717832051203)"><path d="M0 0 C-16.09 -0.09, -80.44 -0.46, -96.53 -0.55 M0 0 C-16.09 -0.09, -80.44 -0.46, -96.53 -0.55" stroke="#1e1e1e" stroke-width="1" fill="none"/></g><g transform="translate(678.837673611109 351.13374690371165) rotate(0 -48.26280381944389 -0.2737717832051203)"><path d="M-72.99 -8.96 C-79.4 -6.67, -85.82 -4.38, -96.53 -0.55 M-72.99 -8.96 C-81.81 -5.81, -90.63 -2.66, -96.53 -0.55" stroke="#1e1e1e" stroke-width="1" fill="none"/></g><g transform="translate(678.837673611109 351.13374690371165) rotate(0 -48.26280381944389 -0.2737717832051203)"><path d="M-73.08 8.14 C-79.47 5.77, -85.86 3.4, -96.53 -0.55 M-73.08 8.14 C-81.87 4.88, -90.66 1.63, -96.53 -0.55" stroke="#1e1e1e" stroke-width="1" fill="none"/></g></g><mask/><g stroke-linecap="round" transform="translate(839.3931206597226 240.0344315786242) rotate(0 165.720703125 31.86197916666697)"><path d="M15.93 0 C97.66 0, 179.38 0, 315.51 0 C326.13 0, 331.44 5.31, 331.44 15.93 C331.44 22.53, 331.44 29.12, 331.44 47.79 C331.44 58.41, 326.13 63.72, 315.51 63.72 C196.07 63.72, 76.64 63.72, 15.93 63.72 C5.31 63.72, 0 58.41, 0 47.79 C0 35.19, 0 22.59, 0 15.93 C0 5.31, 5.31 0, 15.93 0" stroke="none" stroke-width="0" fill="#fab005"/><path d="M15.93 0 C107.02 0, 198.1 0, 315.51 0 M15.93 0 C77.16 0, 138.38 0, 315.51 0 M315.51 0 C326.13 0, 331.44 5.31, 331.44 15.93 M315.51 0 C326.13 0, 331.44 5.31, 331.44 15.93 M331.44 15.93 C331.44 22.57, 331.44 29.22, 331.44 47.79 M331.44 15.93 C331.44 24.21, 331.44 32.48, 331.44 47.79 M331.44 47.79 C331.44 58.41, 326.13 63.72, 315.51 63.72 M331.44 47.79 C331.44 58.41, 326.13 63.72, 315.51 63.72 M315.51 63.72 C216.25 63.72, 116.99 63.72, 15.93 63.72 M315.51 63.72 C219.58 63.72, 123.65 63.72, 15.93 63.72 M15.93 63.72 C5.31 63.72, 0 58.41, 0 47.79 M15.93 63.72 C5.31 63.72, 0 58.41, 0 47.79 M0 47.79 C0 36.29, 0 24.79, 0 15.93 M0 47.79 C0 37.48, 0 27.16, 0 15.93 M0 15.93 C0 5.31, 5.31 0, 15.93 0 M0 15.93 C0 5.31, 5.31 0, 15.93 0" stroke="#1e1e1e" stroke-width="1" fill="none"/></g><g transform="translate(888.7738961113828 246.89641074529118) rotate(0 116.33992767333984 25)"><text x="116.33992767333984" y="17.619999999999997" font-family="Excalifont, Xiaolai, sans-serif, Segoe UI Emoji" font-size="20px" fill="#1e1e1e" text-anchor="middle" style="white-space: pre;" direction="ltr" dominant-baseline="alphabetic">Computation </text><text x="116.33992767333984" y="42.62" font-family="Excalifont, Xiaolai, sans-serif, Segoe UI Emoji" font-size="20px" fill="#1e1e1e" text-anchor="middle" style="white-space: pre;" direction="ltr" dominant-baseline="alphabetic">(computation_id = &quot;id1&quot;)</text></g><g stroke-linecap="round" transform="translate(846.6123046875 342.28529963417895) rotate(0 166.15190972222217 30)"><path d="M15 0 C127.11 0, 239.22 0, 317.3 0 C327.3 0, 332.3 5, 332.3 15 C332.3 24.65, 332.3 34.31, 332.3 45 C332.3 55, 327.3 60, 317.3 60 C234.8 60, 152.29 60, 15 60 C5 60, 0 55, 0 45 C0 37, 0 28.99, 0 15 C0 5, 5 0, 15 0" stroke="none" stroke-width="0" fill="#fab005"/><path d="M15 0 C115.47 0, 215.94 0, 317.3 0 M15 0 C112.68 0, 210.35 0, 317.3 0 M317.3 0 C327.3 0, 332.3 5, 332.3 15 M317.3 0 C327.3 0, 332.3 5, 332.3 15 M332.3 15 C332.3 23.67, 332.3 32.35, 332.3 45 M332.3 15 C332.3 25.12, 332.3 35.24, 332.3 45 M332.3 45 C332.3 55, 327.3 60, 317.3 60 M332.3 45 C332.3 55, 327.3 60, 317.3 60 M317.3 60 C243.57 60, 169.84 60, 15 60 M317.3 60 C200.12 60, 82.93 60, 15 60 M15 60 C5 60, 0 55, 0 45 M15 60 C5 60, 0 55, 0 45 M0 45 C0 36.67, 0 28.35, 0 15 M0 45 C0 37.85, 0 30.7, 0 15 M0 15 C0 5, 5 0, 15 0 M0 15 C0 5, 5 0, 15 0" stroke="#1e1e1e" stroke-width="1" fill="none"/></g><g transform="translate(894.3742836846245 347.28529963417895) rotate(0 118.38993072509766 25)"><text x="118.38993072509766" y="17.619999999999997" font-family="Excalifont, Xiaolai, sans-serif, Segoe UI Emoji" font-size="20px" fill="#1e1e1e" text-anchor="middle" style="white-space: pre;" direction="ltr" dominant-baseline="alphabetic">Computation</text><text x="118.38993072509766" y="42.62" font-family="Excalifont, Xiaolai, sans-serif, Segoe UI Emoji" font-size="20px" fill="#1e1e1e" text-anchor="middle" style="white-space: pre;" direction="ltr" dominant-baseline="alphabetic">(computation_id = &quot;idN&quot;)</text></g><g transform="translate(978.5559895833326 303.2946079799149) rotate(0 8.219993591308594 12.5)"><text x="0" y="17.619999999999997" font-family="Excalifont, Xiaolai, sans-serif, Segoe UI Emoji" font-size="20px" fill="#1e1e1e" text-anchor="start" style="white-space: pre;" direction="ltr" dominant-baseline="alphabetic">...</text></g><g stroke-linecap="round" transform="translate(37.6651475694448 233.9751635354669) rotate(0 178.46375868055566 34.23611111111131)"><path d="M17.12 0 C95.9 0, 174.68 0, 339.81 0 C351.22 0, 356.93 5.71, 356.93 17.12 C356.93 29.15, 356.93 41.18, 356.93 51.35 C356.93 62.77, 351.22 68.47, 339.81 68.47 C236.81 68.47, 133.82 68.47, 17.12 68.47 C5.71 68.47, 0 62.77, 0 51.35 C0 41.52, 0 31.68, 0 17.12 C0 5.71, 5.71 0, 17.12 0" stroke="none" stroke-width="0" fill="#fab005"/><path d="M17.12 0 C108.94 0, 200.77 0, 339.81 0 M17.12 0 C123.73 0, 230.35 0, 339.81 0 M339.81 0 C351.22 0, 356.93 5.71, 356.93 17.12 M339.81 0 C351.22 0, 356.93 5.71, 356.93 17.12 M356.93 17.12 C356.93 29.53, 356.93 41.94, 356.93 51.35 M356.93 17.12 C356.93 30.62, 356.93 44.13, 356.93 51.35 M356.93 51.35 C356.93 62.77, 351.22 68.47, 339.81 68.47 M356.93 51.35 C356.93 62.77, 351.22 68.47, 339.81 68.47 M339.81 68.47 C214.49 68.47, 89.18 68.47, 17.12 68.47 M339.81 68.47 C227.85 68.47, 115.9 68.47, 17.12 68.47 M17.12 68.47 C5.71 68.47, 0 62.77, 0 51.35 M17.12 68.47 C5.71 68.47, 0 62.77, 0 51.35 M0 51.35 C0 38.6, 0 25.84, 0 17.12 M0 51.35 C0 44.49, 0 37.63, 0 17.12 M0 17.12 C0 5.71, 5.71 0, 17.12 0 M0 17.12 C0 5.71, 5.71 0, 17.12 0" stroke="#1e1e1e" stroke-width="1" fill="none"/></g><g transform="translate(48.59902954101608 243.2112746465782) rotate(0 167.52987670898438 25)"><text x="167.52987670898438" y="17.619999999999997" font-family="Excalifont, Xiaolai, sans-serif, Segoe UI Emoji" font-size="20px" fill="#1e1e1e" text-anchor="middle" style="white-space: pre;" direction="ltr" dominant-baseline="alphabetic">TTransformCompanionComputation</text><text x="167.52987670898438" y="42.62" font-family="Excalifont, Xiaolai, sans-serif, Segoe UI Emoji" font-size="20px" fill="#1e1e1e" text-anchor="middle" style="white-space: pre;" direction="ltr" dominant-baseline="alphabetic">(computation_id = &quot;id1&quot;)</text></g><g stroke-linecap="round" transform="translate(37.00385199652783 346.8619791666697) rotate(0 180.92686631944434 34.23611111111131)"><path d="M17.12 0 C129.27 0, 241.42 0, 344.74 0 C356.15 0, 361.85 5.71, 361.85 17.12 C361.85 26.78, 361.85 36.44, 361.85 51.35 C361.85 62.77, 356.15 68.47, 344.74 68.47 C234.29 68.47, 123.84 68.47, 17.12 68.47 C5.71 68.47, 0 62.77, 0 51.35 C0 43.99, 0 36.63, 0 17.12 C0 5.71, 5.71 0, 17.12 0" stroke="none" stroke-width="0" fill="#fab005"/><path d="M17.12 0 C125.34 0, 233.55 0, 344.74 0 M17.12 0 C103.89 0, 190.66 0, 344.74 0 M344.74 0 C356.15 0, 361.85 5.71, 361.85 17.12 M344.74 0 C356.15 0, 361.85 5.71, 361.85 17.12 M361.85 17.12 C361.85 26.96, 361.85 36.79, 361.85 51.35 M361.85 17.12 C361.85 25.77, 361.85 34.41, 361.85 51.35 M361.85 51.35 C361.85 62.77, 356.15 68.47, 344.74 68.47 M361.85 51.35 C361.85 62.77, 356.15 68.47, 344.74 68.47 M344.74 68.47 C225.15 68.47, 105.57 68.47, 17.12 68.47 M344.74 68.47 C215.83 68.47, 86.93 68.47, 17.12 68.47 M17.12 68.47 C5.71 68.47, 0 62.77, 0 51.35 M17.12 68.47 C5.71 68.47, 0 62.77, 0 51.35 M0 51.35 C0 37.7, 0 24.05, 0 17.12 M0 51.35 C0 41.93, 0 32.51, 0 17.12 M0 17.12 C0 5.71, 5.71 0, 17.12 0 M0 17.12 C0 5.71, 5.71 0, 17.12 0" stroke="#1e1e1e" stroke-width="1" fill="none"/></g><g transform="translate(50.4008416069878 356.098090277781) rotate(0 167.52987670898438 25)"><text x="167.52987670898438" y="17.619999999999997" font-family="Excalifont, Xiaolai, sans-serif, Segoe UI Emoji" font-size="20px" fill="#1e1e1e" text-anchor="middle" style="white-space: pre;" direction="ltr" dominant-baseline="alphabetic">TTransformCompanionComputation</text><text x="167.52987670898438" y="42.62" font-family="Excalifont, Xiaolai, sans-serif, Segoe UI Emoji" font-size="20px" fill="#1e1e1e" text-anchor="middle" style="white-space: pre;" direction="ltr" dominant-baseline="alphabetic">(computation_id = &quot;idN&quot;)</text></g><g transform="translate(194.11914486355317 308.3420138888905) rotate(0 8.219993591308594 12.5)"><text x="0" y="17.619999999999997" font-family="Excalifont, Xiaolai, sans-serif, Segoe UI Emoji" font-size="20px" fill="#1e1e1e" text-anchor="start" style="white-space: pre;" direction="ltr" dominant-baseline="alphabetic">...</text></g><g stroke-linecap="round"><g transform="translate(395.6334092881939 256.3715277777792) rotate(0 52.34809027777783 -0.2625868055556566)"><path d="M0 0 C17.45 -0.09, 87.25 -0.44, 104.7 -0.53 M0 0 C17.45 -0.09, 87.25 -0.44, 104.7 -0.53" stroke="#1e1e1e" stroke-width="1" fill="none"/></g><g transform="translate(395.6334092881939 256.3715277777792) rotate(0 52.34809027777783 -0.2625868055556566)"><path d="M81.25 8.14 C88.23 5.56, 95.21 2.98, 104.7 -0.53 M81.25 8.14 C89.15 5.22, 97.06 2.3, 104.7 -0.53" stroke="#1e1e1e" stroke-width="1" fill="none"/></g><g transform="translate(395.6334092881939 256.3715277777792) rotate(0 52.34809027777783 -0.2625868055556566)"><path d="M81.16 -8.96 C88.17 -6.45, 95.18 -3.94, 104.7 -0.53 M81.16 -8.96 C89.1 -6.12, 97.03 -3.27, 104.7 -0.53" stroke="#1e1e1e" stroke-width="1" fill="none"/></g></g><mask/><g stroke-linecap="round"><g transform="translate(498.0162217881939 278.5416666666679) rotate(0 -51.64713541666674 -1.4756944444443434)"><path d="M0 0 C-17.22 -0.49, -86.08 -2.46, -103.29 -2.95 M0 0 C-17.22 -0.49, -86.08 -2.46, -103.29 -2.95" stroke="#1e1e1e" stroke-width="1" fill="none"/></g><g transform="translate(498.0162217881939 278.5416666666679) rotate(0 -51.64713541666674 -1.4756944444443434)"><path d="M-79.57 -10.83 C-85.6 -8.82, -91.63 -6.82, -103.29 -2.95 M-79.57 -10.83 C-86.34 -8.58, -93.11 -6.33, -103.29 -2.95" stroke="#1e1e1e" stroke-width="1" fill="none"/></g><g transform="translate(498.0162217881939 278.5416666666679) rotate(0 -51.64713541666674 -1.4756944444443434)"><path d="M-80.06 6.27 C-85.97 3.92, -91.87 1.58, -103.29 -2.95 M-80.06 6.27 C-86.69 3.64, -93.32 1, -103.29 -2.95" stroke="#1e1e1e" stroke-width="1" fill="none"/></g></g><mask/><g stroke-linecap="round"><g transform="translate(406.4407009548604 367.5651041666679) rotate(0 44.61805555555566 -0.8702256944443434)"><path d="M0 0 C14.87 -0.29, 74.36 -1.45, 89.24 -1.74 M0 0 C14.87 -0.29, 74.36 -1.45, 89.24 -1.74" stroke="#1e1e1e" stroke-width="1" fill="none"/></g><g transform="translate(406.4407009548604 367.5651041666679) rotate(0 44.61805555555566 -0.8702256944443434)"><path d="M65.91 7.27 C74.28 4.04, 82.64 0.81, 89.24 -1.74 M65.91 7.27 C72.95 4.55, 79.98 1.83, 89.24 -1.74" stroke="#1e1e1e" stroke-width="1" fill="none"/></g><g transform="translate(406.4407009548604 367.5651041666679) rotate(0 44.61805555555566 -0.8702256944443434)"><path d="M65.58 -9.83 C74.06 -6.93, 82.54 -4.03, 89.24 -1.74 M65.58 -9.83 C72.72 -7.39, 79.85 -4.95, 89.24 -1.74" stroke="#1e1e1e" stroke-width="1" fill="none"/></g></g><mask/><g stroke-linecap="round"><g transform="translate(498.2896592881939 394.8046875000009) rotate(0 -48.48524305555566 -0.7573784722221717)"><path d="M0 0 C-16.16 -0.25, -80.81 -1.26, -96.97 -1.51 M0 0 C-16.16 -0.25, -80.81 -1.26, -96.97 -1.51" stroke="#1e1e1e" stroke-width="1" fill="none"/></g><g transform="translate(498.2896592881939 394.8046875000009) rotate(0 -48.48524305555566 -0.7573784722221717)"><path d="M-73.35 -9.7 C-80.58 -7.19, -87.81 -4.69, -96.97 -1.51 M-73.35 -9.7 C-79 -7.74, -84.65 -5.78, -96.97 -1.51" stroke="#1e1e1e" stroke-width="1" fill="none"/></g><g transform="translate(498.2896592881939 394.8046875000009) rotate(0 -48.48524305555566 -0.7573784722221717)"><path d="M-73.61 7.4 C-80.76 4.67, -87.91 1.94, -96.97 -1.51 M-73.61 7.4 C-79.2 5.27, -84.79 3.14, -96.97 -1.51" stroke="#1e1e1e" stroke-width="1" fill="none"/></g></g><mask/><g stroke-linecap="round"><g transform="translate(764.6828884548604 256.3498263888896) rotate(0 35.67708333333326 -0.2907986111108585)"><path d="M0 0 C11.89 -0.1, 59.46 -0.48, 71.35 -0.58 M0 0 C11.89 -0.1, 59.46 -0.48, 71.35 -0.58" stroke="#1e1e1e" stroke-width="1" fill="none"/></g><g transform="translate(764.6828884548604 256.3498263888896) rotate(0 35.67708333333326 -0.2907986111108585)"><path d="M47.93 8.16 C56.16 5.09, 64.38 2.02, 71.35 -0.58 M47.93 8.16 C54.51 5.71, 61.09 3.25, 71.35 -0.58" stroke="#1e1e1e" stroke-width="1" fill="none"/></g><g transform="translate(764.6828884548604 256.3498263888896) rotate(0 35.67708333333326 -0.2907986111108585)"><path d="M47.79 -8.94 C56.07 -6, 64.34 -3.07, 71.35 -0.58 M47.79 -8.94 C54.41 -6.59, 61.02 -4.25, 71.35 -0.58" stroke="#1e1e1e" stroke-width="1" fill="none"/></g></g><mask/><g stroke-linecap="round"><g transform="translate(836.7271592881939 282.3437500000009) rotate(0 -36.66015625 -0.004340277777828305)"><path d="M0 0 C-12.22 0, -61.1 -0.01, -73.32 -0.01 M0 0 C-12.22 0, -61.1 -0.01, -73.32 -0.01" stroke="#1e1e1e" stroke-width="1" fill="none"/></g><g transform="translate(836.7271592881939 282.3437500000009) rotate(0 -36.66015625 -0.004340277777828305)"><path d="M-49.83 -8.56 C-58.04 -5.57, -66.25 -2.58, -73.32 -0.01 M-49.83 -8.56 C-56.62 -6.09, -63.41 -3.61, -73.32 -0.01" stroke="#1e1e1e" stroke-width="1" fill="none"/></g><g transform="translate(836.7271592881939 282.3437500000009) rotate(0 -36.66015625 -0.004340277777828305)"><path d="M-49.83 8.54 C-58.04 5.56, -66.25 2.57, -73.32 -0.01 M-49.83 8.54 C-56.62 6.07, -63.41 3.6, -73.32 -0.01" stroke="#1e1e1e" stroke-width="1" fill="none"/></g></g><mask/><g stroke-linecap="round"><g transform="translate(770.7722981770826 350.6032986111122) rotate(0 35.71397569444434 0.8984375)"><path d="M0 0 C11.9 0.3, 59.52 1.5, 71.43 1.8 M0 0 C11.9 0.3, 59.52 1.5, 71.43 1.8" stroke="#1e1e1e" stroke-width="1" fill="none"/></g><g transform="translate(770.7722981770826 350.6032986111122) rotate(0 35.71397569444434 0.8984375)"><path d="M47.73 9.75 C52.62 8.11, 57.51 6.47, 71.43 1.8 M47.73 9.75 C55.2 7.24, 62.68 4.73, 71.43 1.8" stroke="#1e1e1e" stroke-width="1" fill="none"/></g><g transform="translate(770.7722981770826 350.6032986111122) rotate(0 35.71397569444434 0.8984375)"><path d="M48.16 -7.34 C52.96 -5.46, 57.77 -3.57, 71.43 1.8 M48.16 -7.34 C55.5 -4.46, 62.84 -1.58, 71.43 1.8" stroke="#1e1e1e" stroke-width="1" fill="none"/></g></g><mask/><g stroke-linecap="round"><g transform="translate(845.5726453993052 384.80034722222354) rotate(0 -37.92100694444457 -1.8663194444443434)"><path d="M0 0 C-12.64 -0.62, -63.2 -3.11, -75.84 -3.73 M0 0 C-12.64 -0.62, -63.2 -3.11, -75.84 -3.73" stroke="#1e1e1e" stroke-width="1" fill="none"/></g><g transform="translate(845.5726453993052 384.80034722222354) rotate(0 -37.92100694444457 -1.8663194444443434)"><path d="M-51.96 -11.12 C-57.35 -9.45, -62.74 -7.78, -75.84 -3.73 M-51.96 -11.12 C-60.84 -8.37, -69.72 -5.63, -75.84 -3.73" stroke="#1e1e1e" stroke-width="1" fill="none"/></g><g transform="translate(845.5726453993052 384.80034722222354) rotate(0 -37.92100694444457 -1.8663194444443434)"><path d="M-52.8 5.96 C-58 3.77, -63.21 1.58, -75.84 -3.73 M-52.8 5.96 C-61.37 2.36, -69.93 -1.25, -75.84 -3.73" stroke="#1e1e1e" stroke-width="1" fill="none"/></g></g><mask/><g transform="translate(419.1924370659726 10) rotate(0 212.14193725585938 35)"><text x="212.14193725585938" y="24.668" font-family="Excalifont, Xiaolai, sans-serif, Segoe UI Emoji" font-size="28px" fill="#1e1e1e" text-anchor="middle" style="white-space: pre;" direction="ltr" dominant-baseline="alphabetic">Request and response mapping </text><text x="212.14193725585938" y="59.668" font-family="Excalifont, Xiaolai, sans-serif, Segoe UI Emoji" font-size="28px" fill="#1e1e1e" text-anchor="middle" style="white-space: pre;" direction="ltr" dominant-baseline="alphabetic">using computation_id</text></g><g stroke-linecap="round"><g transform="translate(602.979060795753 83.20746527777737) rotate(0 -33.47446399212026 70.34724183170556)"><path d="M0 0 C-11.16 23.45, -55.79 117.25, -66.95 140.69" stroke="#1e1e1e" stroke-width="1.5" fill="none" stroke-dasharray="8 9"/></g><g transform="translate(602.979060795753 83.20746527777737) rotate(0 -33.47446399212026 70.34724183170556)"><path d="M-64.58 115.81 C-65.25 122.93, -65.93 130.05, -66.95 140.69" stroke="#1e1e1e" stroke-width="1.5" fill="none"/></g><g transform="translate(602.979060795753 83.20746527777737) rotate(0 -33.47446399212026 70.34724183170556)"><path d="M-49.13 123.16 C-54.23 128.17, -59.33 133.19, -66.95 140.69" stroke="#1e1e1e" stroke-width="1.5" fill="none"/></g></g><mask/><g stroke-linecap="round"><g transform="translate(659.5259825650896 90.19531250000182) rotate(0 32.667998300789804 70.79883053602316)"><path d="M0 0 C10.89 23.6, 54.45 118, 65.34 141.6" stroke="#1e1e1e" stroke-width="1.5" fill="none" stroke-dasharray="8 9"/></g><g transform="translate(659.5259825650896 90.19531250000182) rotate(0 32.667998300789804 70.79883053602316)"><path d="M47.73 123.85 C52.73 128.89, 57.74 133.94, 65.34 141.6" stroke="#1e1e1e" stroke-width="1.5" fill="none"/></g><g transform="translate(659.5259825650896 90.19531250000182) rotate(0 32.667998300789804 70.79883053602316)"><path d="M63.26 116.68 C63.85 123.77, 64.44 130.85, 65.34 141.6" stroke="#1e1e1e" stroke-width="1.5" fill="none"/></g></g><mask/><g transform="translate(502.3753679063575 529.5789930555557) rotate(0 155.67991638183594 17.5)"><text x="155.67991638183594" y="24.668" font-family="Excalifont, Xiaolai, sans-serif, Segoe UI Emoji" font-size="28px" fill="#1e1e1e" text-anchor="middle" style="white-space: pre;" direction="ltr" dominant-baseline="alphabetic">localhost network calls</text></g><g stroke-linecap="round"><g transform="translate(625.2319215857151 524.5008680555547) rotate(0 -0.174033657056043 -86.15147456338991)"><path d="M0 0 C-0.06 -28.72, -0.29 -143.59, -0.35 -172.3" stroke="#1e1e1e" stroke-width="1.5" fill="none" stroke-dasharray="8 9"/></g><g transform="translate(625.2319215857151 524.5008680555547) rotate(0 -0.174033657056043 -86.15147456338991)"><path d="M8.25 -148.83 C6.1 -154.69, 3.96 -160.55, -0.35 -172.3" stroke="#1e1e1e" stroke-width="1.5" fill="none"/></g><g transform="translate(625.2319215857151 524.5008680555547) rotate(0 -0.174033657056043 -86.15147456338991)"><path d="M-8.85 -148.79 C-6.73 -154.66, -4.6 -160.54, -0.35 -172.3" stroke="#1e1e1e" stroke-width="1.5" fill="none"/></g></g><mask/></svg>

You manage the companion process through the [resource](https://ytsaurus.tech/docs/en/flow/concepts/glossary.md#resource) `CompanionManager`.

### Configuration

Here’s an example of declaring the resource in a static spec for Java:

```yson
"CompanionManager" = {
    "resource_class_name" = "NYT::NFlow::NCompanion::TJavaCompanionManager";
    "parameters" = {
        "timeout" = "10s";
        "jdk_bin_path" = "/app/ytflow/jdk/bin/java";
        "main_class" = "tech.ytsaurus.flow.examples.wordcount.WordCountApplication";
        "classpath" = "/app/ytflow/lib/*";
    };
    "dependencies" = {};
};
```

For a detailed description of all `TCompanionManagerParameters` parameters, see the section [CompanionManager resource configuration](https://ytsaurus.tech/docs/en/flow/java/computation.md#companion-manager).

The Python companion configuration is described in the section [CompanionManager resource configuration (Python)](https://ytsaurus.tech/docs/en/flow/python/computation.md#companion-manager).

Here’s an example of declaring a Computation in a static spec:

```yson
"computations" = {
    "mapper" = {
        "computation_class_name" = "NYT::NFlow::NCompanion::TTransformCompanionComputation";
        "group_by_schema" = [
            {"name" = "hash"; "expression" = "farm_hash(word)"; "type" = "uint64"; required = %true;};
            {"name" = "word"; "type" = "string";};
        ];
        "input_stream_ids" = ["words"];
        "output_stream_ids" = [];
        "required_resource_ids" = {
            "CompanionManager" = {
                "controller" = false;
                "worker" = true;
            };
        };
        "parameters" = {
            "internal_states" = ["word-state"];
        };
    };
};
```

The key point in this example is using the `CompanionManager` resource to launch the companion process and the specialized Computation class `NYT::NFlow::NCompanion::TTransformCompanionComputation`.

### C++ companion {#cpp-companion}

You can move user C++ code out of the worker into a separate process as well. The SDK is located in `yt/yt/flow/library/cpp/companion/server`: you declare the served Computations in `TPipeline`, specifying the process function type (this typed declaration replaces `YT_FLOW_DEFINE_PROCESS_FUNCTION`), and build a separate binary with the `RunCompanionMain` entry point:

```cpp
int main(int argc, const char** argv)
{
    NYT::NFlow::NCompanionServer::TPipeline pipeline;
    pipeline.AddSource<TMyReadFunction, TMyReadParameters>("reader");
    pipeline.AddTransform<TMyMapFunction>("mapper");
    return NYT::NFlow::NCompanionServer::RunCompanionMain(argc, argv, std::move(pipeline));
}
```

The function is selected by the name from the `processing_function` field of the Computation spec, the same way as in the in-process `TProcessFunctionComputation` adapters. The worker launches the binary through the generic `TCompanionManager` resource:

```yson
"CompanionManager" = {
    "resource_class_name" = "NYT::NFlow::NCompanion::TCompanionManager";
    "parameters" = {
        "entrypoint" = {
            "executable" = "/path/to/my_companion";
        };
    };
};
```

A process function in a companion can take the HTTP clients from `IRuntimeInitContext` in `Init` (`GetHttpClient()`, `GetHttpsClient()`). The clients are configured by the `http_client_config`, `https_client_config` and `http_poller_threads` fields of the `companion` block of the worker node config ([TCompanionConfig](https://ytsaurus.tech/docs/en/flow/generated_docs/all_yson_structs.md#NYT_NFlow_NCompanion_TCompanionConfig)).

Limitations of the first version of the C++ companion:

- sync process functions aren’t supported (the companion protocol has no Sync phase);
- static [resources](https://ytsaurus.tech/docs/en/flow/concepts/glossary.md#resource), distributed throttlers, and the epoch timestamp (`GetCurrentTimestamp`) aren’t available;
- `GetStreamSpecs()->ComputeKey()` can’t compute a key when `group_by_schema` has computed columns: a companion doesn’t evaluate expressions. The key arrives with the message — use `message->Key`;
- external states are supported only as `TSimpleExternalState`;
- output timers can only reference the key of one of the parent entities of the batch;
- the companion runs as a single multithreaded process (`companion_process_count` is 0 or 1).

For an example, see `yt/yt/flow/examples/cpp/companion_word_count`.

### Types of Computations for working with companions {#computation-types}

- `NYT::NFlow::NCompanion::TSwiftMapCompanionComputation`: An implementation of [TSwiftMapComputation](https://ytsaurus.tech/docs/en/flow/concepts/computation.md#tswiftmapcomputation) that delegates data processing to the companion process.
- `NYT::NFlow::NCompanion::TSwiftOrderedSourceCompanionComputation`: An implementation of [TSwiftOrderedSourceComputation](https://ytsaurus.tech/docs/en/flow/concepts/computation.md#tswiftorderedsourcecomputation) that delegates data processing to the companion process.
- `NYT::NFlow::NCompanion::TTransformCompanionComputation`: An implementation of [TTransformComputation](https://ytsaurus.tech/docs/en/flow/concepts/computation.md#ttransformcomputation) that delegates data processing to the companion process.
- `NYT::NFlow::NCompanion::TTransformOrderedSourceCompanionComputation`: An implementation of [TTransformOrderedSourceComputation](https://ytsaurus.tech/docs/en/flow/concepts/computation.md#ttransformorderedsourcecomputation) that delegates data processing to the companion process.

Two modes are available for a Source computation. `TSwiftOrderedSourceCompanionComputation` doesn’t materialize the output and requires deterministic processing without user state. `TTransformOrderedSourceCompanionComputation` materializes the output and commits it together with the internal state and the source offset in the epoch transaction. Choose it for non-deterministic processing or for working with internal state; the key of such a state is the source partition key. The spec limitations are the same as for [TTransformOrderedSourceComputation](https://ytsaurus.tech/docs/en/flow/concepts/computation.md#ttransformorderedsourcecomputation).

For more details on implementing pipelines using companions, see [Java and Kotlin](https://ytsaurus.tech/docs/en/flow/java/getting-started.md), [Python](https://ytsaurus.tech/docs/en/flow/python/getting-started.md), and [Go](https://ytsaurus.tech/docs/en/flow/go/getting-started.md).

## See also

- [Computation](https://ytsaurus.tech/docs/en/flow/concepts/computation.md)
- [Quick start (Java)](https://ytsaurus.tech/docs/en/flow/java/getting-started.md)
- [Quick start (Python)](https://ytsaurus.tech/docs/en/flow/python/getting-started.md)
- [Quick start (Go)](https://ytsaurus.tech/docs/en/flow/go/getting-started.md)
<!-- endsource: en/_includes/flow/concepts/companion.md -->