# Concurrent compute

> The router sends several requests at once to one warm worker, so an API that spends its time waiting on a database pays for one environment instead of ten.

- Category: Functions
- URL: https://www.forkrail.com/docs/concurrent-compute

## How it works

Classic serverless runs one request per environment. Ten requests in flight means ten environments billed, even when nine of them are waiting on a Postgres query. With concurrent compute (on by default), a running worker takes up to **10 requests at once** over its connection to the router. When every worker is full, the router starts another, as many as traffic needs.

- The router learns how much memory each route’s requests use and adds a request to a busy worker only when it fits.
- If a new worker hasn’t connected within about 100 ms, the router starts a second one in parallel, so one slow cold start doesn’t hold a request.
- Idle workers retire as soon as keeping them costs more than starting a new one, and every worker is replaced after 13 minutes.

It’s forkrail’s version of what Vercel calls fluid compute, and it reads Vercel’s `fluid` key in `vercel.json`.

## Max requests per instance

Ten is the default. Set anywhere from 2 to 50 under **Settings → Functions → Concurrent compute**, where the dashboard offers about one per 50 MB of memory (15 at 768 MB, 50 from 2,560 MB). It applies within seconds, without a redeploy.

```bash
forkrail project my-api --max-inflight 3        # 2 to 50
forkrail project my-api --max-inflight default  # back to 10
```

> **A limit per worker, not per project:** This number caps how many requests share **one** worker. It doesn’t limit the project: there’s no cap on requests per second, and when every worker is full the router starts another. How many workers run at once is bounded only by your AWS account’s Lambda concurrency quota (1,000 per region by default, lower on some new accounts; AWS raises it on request), by a cap you choose with [`forkrail limits --max-workers`](https://www.forkrail.com/docs/functions#max-workers) (none by default), and by a [spend limit](https://www.forkrail.com/docs/spend-budgets) pause. A worker can also take fewer requests than the setting while its CPU is busy.

Fewer means fewer requests share each environment and its outbound IP address, and more workers start under load. A Python WSGI app (Flask, Django) runs at most 10 requests at once per worker whatever the setting. An ASGI app (FastAPI) runs `async` routes on one event loop, so a blocking call in one route holds up the others.

## One request per worker

Some APIs need each request in its own environment, for strict isolation or so that concurrent requests leave from different IP addresses. Turn concurrent compute off, in order of precedence:

```bash
# For one deploy
forkrail deploy --concurrent false

# In vercel.json (Vercel reads the same key)
#   { "fluid": false }

# As the project's setting
forkrail project my-api --concurrent false
```

Prefer `vercel.json` or the project setting: a later deploy without the flag would turn concurrent compute back on. The CLI warns when a deploy or promote would replace a one-request-per-worker production deployment with a concurrent one. Off, it bills about what classic Lambda does.

## What it saves

Lambda bills each worker from start to finish. Requests that overlap on one worker share that time:

|  | One request per worker | Concurrent compute |
| --- | --- | --- |
| 10 requests, 1 s each, all at once, 640 MB | 10 workers × 1 s = 6.25 GB-s | 1 worker × ~1 s = 0.625 GB-s |
| Invocations | 10 | 1 |
| Outbound IPs | 10 | 1 |

The saving is largest for I/O-bound work (APIs calling databases, other APIs or LLMs). CPU-bound requests gain less, because they compete for the same CPU.
