# Composition Patterns: Choreography, Orchestration and Fan-Out — Serverless & Edge

Source: https://www.skillbyai.com/en/serverless-edge/se-patterns

> Connect functions with events or workflows, split big jobs with fan-out, and avoid function-to-function synchronous chains.

## Two ways to connect small pieces

Larger serverless systems are compositions of small functions, and two styles connect them. In **choreography**, services publish events to a bus (EventBridge, SNS, Pub/Sub) and others react independently: an `OrderPlaced` event triggers billing, email and analytics without the order service knowing they exist. Coupling is loose, but the end-to-end flow is hard to see. In **orchestration**, a workflow engine such as AWS Step Functions, Azure Durable Functions or Cloudflare Workflows runs an explicit sequence: call payment, wait, then retry or compensate. Long, multi-step processes become easier to reason about, and the engine, not a function, does the waiting. **Fan-out/fan-in** splits a big job, such as processing ten thousand answer sheets, into many parallel invocations and then collects the results. Avoid one function synchronously invoking another and waiting: you pay for both while one idles, timeouts stack up, and their failures become coupled.

## Producers, a coordinator and workers

A producer emits work, an event bus or orchestrator decides where it goes, and independent worker functions carry it out in parallel.

![Three connected stages from left to right: a producer, an event bus or workflow orchestrator, and a group of worker functions.](assets/figures/serverless-edge/section-10-map.svg) — Figure 10.1: producer -> event bus or orchestrator -> worker functions

## The wedding planner and the family WhatsApp group

Orchestration is the wedding planner with a checklist who calls the caterer, then the decorator, and chases anyone who is late. Choreography is the family WhatsApp group: someone posts "baraat leaving now" and everyone reacts in their own way. The group is flexible; the planner knows exactly where things stand.

## Plan for compensation, not rollback

Distributed steps cannot share one database transaction. If payment succeeds but booking fails, a workflow must run a *compensating* action such as a refund. This is the saga pattern, and orchestration engines make its steps and retries explicit.

**Quiz:** Why is a function that synchronously invokes another function and waits for it considered an anti-pattern?

- [ ] Functions are technically unable to call other functions
- [x] You pay for both while the first one waits, and timeouts and failures become coupled
- [ ] It always causes a recursive loop
- [ ] It disables logging for the second function

*Answer:* You pay for both while the first one waits, and timeouts and failures become coupled. The caller is billed while idle, the chain's timeouts must nest, and one failure cascades. Queues or a workflow engine decouple the steps.
