<!-- https://tinyactors.dev/recipes/ -->
# Recipes · tinyactors

Kind: reference. For: [Agent-platform engineer](https://tinyactors.dev/audiences/agent-platform.xml), [Workflow developer who tests](https://tinyactors.dev/audiences/workflow-tester.xml), [Tech lead who approves dependencies](https://tinyactors.dev/audiences/tech-lead.xml).
Voice: [Reference](https://tinyactors.dev/voices/reference.xml).

> Ten applications built with tinyactors, from a shop's orders to a game server that survives being killed, each with its problem, the features it uses and its source.

Ten applications, from a shop's orders to a game server that comes back after `SIGKILL`. Each one's test plays the cases that make it hard, under Node.js and under Bun.

[Read the source](https://github.com/tinyactors-dev/tinyactors/tree/main/tinyactors-napi/examples) · [Browse the guides](https://tinyactors.dev/guides/index.md)

<a id="sessions"></a>

## One actor per session or connection

A session's limits, timers and members live in one actor. The code around it only finds the actor by name and sends it events.

- **[A rate-limited API gateway](https://github.com/tinyactors-dev/tinyactors/tree/main/tinyactors-napi/examples/http-gateway)**: Every client session needs a token bucket, a cap on requests in flight and timeouts against a slow, flaky upstream. On `node:http`, the session's chart picks every status, 429, 502 and 504 included, and retries only a `GET`. It shows [an async I/O processor](https://tinyactors.dev/guides/io-processors/index.md#answering-later) that answers with `fetch`, and a request's `traceparent` carried to the upstream and to [OpenTelemetry](https://tinyactors.dev/guides/telemetry/index.md#trace-context).
- **[A chat with workspaces, rooms and connections](https://github.com/tinyactors-dev/tinyactors/tree/main/tinyactors-napi/examples/chat-rooms)**: Thousands of rooms and connections keep their members and history, though most sit idle. A workspace decides which rooms exist and spawns them; only a bound of actors stays in memory, the rest one file each on disk. It shows [a store and a resident bound](https://tinyactors.dev/guides/hibernation/index.md#a-store-and-a-bound), [the Spawner](https://tinyactors.dev/guides/io-processors/index.md#spawning-from-a-chart) and [each way a send meets an actor that is not there](https://tinyactors.dev/guides/missing-actors/index.md).

<a id="workflows"></a>

## Retries, timeouts and refunds as states

Each of these runs a business process whose steps fail the way real ones do. Its test plays every failure on a [simulated clock](https://tinyactors.dev/guides/testing-recipes/index.md#stepping-the-clock), so a 48-hour SLA takes no time.

- **[An order that pays, ships and refunds](https://github.com/tinyactors-dev/tinyactors/tree/main/tinyactors-napi/examples/order-fulfillment)**: A payment times out or its answer is lost, a customer cancels during the shipment, the stock runs out after the charge. The order retries under one idempotency key and refunds what it charged, so a delivered order is charged exactly once ([its test](https://github.com/tinyactors-dev/tinyactors/blob/main/tinyactors-napi/examples/order-fulfillment/test.mjs) loses an answer and checks that the retry charges nothing more). It shows [invoked services](https://tinyactors.dev/guides/services/index.md) for the payment and the shipment, timeouts as delayed sends and [a failed send](https://tinyactors.dev/guides/io-processors/index.md#answer-or-fail) that becomes a retry.
- **[Subscriptions billed during a release](https://github.com/tinyactors-dev/tinyactors/tree/main/tinyactors-napi/examples/subscription-billing)**: A subscription lives for years, and its chart changes under it. Release 2 renames a variable and adds states; a canary, then each chart, moves to it while renewals and payments are in flight. It shows [migration](https://tinyactors.dev/guides/migration/index.md) of a mostly [hibernated](https://tinyactors.dev/guides/hibernation/index.md) population, and [the typed methods](https://tinyactors.dev/guides/inspection/index.md) an operator uses to find who is left on the old release.
- **[A document reviewed by legal and finance](https://github.com/tinyactors-dev/tinyactors/tree/main/tinyactors-napi/examples/document-approval)**: Two reviews run at once, finance waits for legal on large amounts, and a stage past its SLA escalates the document to a manager. Parallel regions, `In()` guards and history states hold those rules. It shows an admin view over HTTP that reads every document's states, variables and due SLAs with [the typed methods](https://tinyactors.dev/guides/inspection/index.md#delayed-sends-and-history).
- **[Background jobs with retries and deadlines](https://github.com/tinyactors-dev/tinyactors/tree/main/tinyactors-napi/examples/job-scheduler)**: Workers throw, hang or run past a deadline, and a slow recurring run must not push the schedule back. A job's delayed sends are its whole schedule, and the process exits by itself when the last job ends. It shows [an I/O processor](https://tinyactors.dev/guides/io-processors/index.md) that aborts a hung worker, a graceful shutdown on `SIGTERM` and the drift of fixed-delay jobs, measured.

<a id="systems"></a>

## Two systems, each a host, over a socket

The host decides which sends leave its system. The wire, the connection and what to do when it is gone are your code's, written in [a transport](https://tinyactors.dev/guides/other-systems/index.md).

- **[Stock held across a warehouse and a storefront](https://github.com/tinyactors-dev/tinyactors/tree/main/tinyactors-napi/examples/inventory-sync)**: Two carts race for the last unit, a hold expires, the warehouse goes down in the middle of a checkout. Each SKU is an actor that takes one request at a time and answers a repeated one as it answered the first ([its test](https://github.com/tinyactors-dev/tinyactors/blob/main/tinyactors-napi/examples/inventory-sync/test.mjs) duplicates every message both ways); a cart compensates by releasing every line. It shows [a transport over TCP](https://tinyactors.dev/guides/other-systems/index.md#over-a-socket), [a failed delivery](https://tinyactors.dev/guides/other-systems/index.md#failed-delivery) and [a snapshot](https://tinyactors.dev/guides/snapshots/index.md) the warehouse comes back from.
- **[A Python system and a Node.js system](https://github.com/tinyactors-dev/tinyactors/tree/main/tinyactors-napi/examples/remote-python)**: Actors in two processes and two languages address each other by `tinyactors://` address. A Node.js chart and a Python chart trade pings over one TCP connection, and a send to an actor the other side lacks fails back to its sender. Values cross unchanged, integers up to 2^64 included.

<a id="sandbox"></a>

## Customers' code in a sandbox per rule

- **[Alert rules your customers upload](https://github.com/tinyactors-dev/tinyactors/tree/main/tinyactors-napi/examples/iot-rules)**: An uploaded rule may loop for ever, hoard memory, look for `require` or send to another customer's device. Each rule runs on a host of its own on [the quickjs datamodel](https://tinyactors.dev/guides/quickjs/index.md), with limits on operations and memory, so a runaway evaluation becomes `error.execution` and the rule is suspended after three. Its README measures how long a loop holds the host and what a rule costs per device.

<a id="realtime"></a>

## Turn timers that survive a crash

- **[A game server that survives being killed](https://github.com/tinyactors-dev/tinyactors/tree/main/tinyactors-napi/examples/turn-game)**: A match's turn timers must come back from a crash with the time they had left. The server takes [a snapshot](https://tinyactors.dev/guides/snapshots/index.md) of the whole host every second, [journals](https://tinyactors.dev/guides/journal/index.md) each client input, and after `SIGKILL` restores and replays the journal, or drops it. Its matchmaking service [starts again](https://tinyactors.dev/guides/snapshots/index.md#restarting-services) from the lobby's waiting list.

<a id="run"></a>

## Run a recipe on its own

A recipe runs against the published package, outside the repository. Install `@tinyactors/node` in a new project and copy the recipe into its `examples/` directory:

```shell
git clone --depth 1 https://github.com/tinyactors-dev/tinyactors
mkdir -p recipes/examples && cd recipes
npm install @tinyactors/node@0.4.0
cp -r ../tinyactors/tinyactors-napi/examples/order-fulfillment examples/
node examples/order-fulfillment/main.mjs
node examples/order-fulfillment/test.mjs
```

`main.mjs` runs the application and prints what its README describes; `test.mjs` plays its hard cases. Three recipes differ:

- **inventory-sync**: reuses the framing of remote-python, so copy `remote-python` beside it.
- **subscription-billing**: runs on 0.4.0, but two of its tests, a payment in flight during a migration, need a core change made after that release.
- **remote-python**: builds the Python binding from the repository, so it runs only there: `mise run remote-python` in `tinyactors-napi`.
