# How forkrail works

> A router in your AWS account takes every request, answers what it can by itself, and runs your code on AWS Lambda workers that keep a connection open to it.

- Category: Get started
- URL: https://www.forkrail.com/docs/how-forkrail-works

## The path of a request

**A request, from visitor to function**

```text
visitor ──▶ *.example.com (DNS) ──▶ forkrail router (EC2, your account)
                                          │
        ┌──────────────┬──────────────────┼──────────────────┬───────────────┐
        ▼              ▼                  ▼                  ▼               ▼
    firewall      redirects and      static files      cached pages     your function
    and rules     routing rules      (S3, memory)      (ISR, s-maxage)  (AWS Lambda)
```

1. **DNS sends the visitor to the router**

   `*.<your domain>` points at the router’s fixed address. Custom domains point there too, with a CNAME or A record.

2. **The router decides**

   It terminates TLS, finds the deployment by hostname, runs the firewall and deployment protection, then the project’s redirects and routing rules.

3. **It answers what it can**

   Static files come from S3 (the busiest from memory) and cached pages from its own cache. Neither wakes a function.

4. **Everything else runs your code**

   The request goes to a worker of the deployment’s Lambda function. The response streams back through the router to the visitor.

## Workers that dial back

A Lambda worker opens a connection back to the router when it starts and keeps it open while it runs. With [concurrent compute](https://www.forkrail.com/docs/concurrent-compute) on (the default), the router sends one worker up to 10 requests at once over that connection, so an API that spends most of its time waiting on a database pays for one environment instead of ten.

- **Idle costs nothing.** Every deployment is a published Lambda version. Previews and old releases cost nothing until someone opens them.
- **Warm before it’s live.** Before production moves to a new deployment, forkrail starts a few of its environments so the first visitors don’t wait for a cold start.
- **Keep warm, if you want it.** A project can ask the routers to keep environments ready around the clock. See [Function settings](https://www.forkrail.com/docs/functions#keep-warm).

## The parts in your account

| Part | Runs on | What it does |
| --- | --- | --- |
| Router | EC2 (Graviton) | TLS, routing, firewall, static files, response cache, rate limits, metering for spend limits. |
| Workers | AWS Lambda (arm64) | Your functions. One Lambda function per project, one version per deployment. |
| Builder | AWS Lambda (container image) | Builds every deploy from source, on the same Linux your functions run on. |
| Control | AWS Lambda | Publishes deployments, promotes, rolls back, and holds the rights to decrypt env vars. |
| Storage | S3 and DynamoDB | Packages, static files, the routing table, project settings and deployment records. |
| Logs | ClickHouse on EC2 | Every request, app log line, outgoing call and Web Analytics event. |
| Dashboard | A forkrail project | The web app your team signs in to, deployed like any other project. |

## Regions and failover

An install has one router, in the region you pick when you install. Each project then picks the regions its functions run in, from 20 AWS regions, with no router needed there: the router invokes the function in that region, so a function can sit next to its database wherever that is. See [Function regions](https://www.forkrail.com/docs/function-regions).

The router can also run in several regions at once, behind two anycast addresses (AWS Global Accelerator) that send each visitor to the nearest healthy router and move them to another region in under a minute if one stops answering. Setting up a second router region is a manual step today; the installer sets up one. Router updates start the new router beside the old one, so restarts have no gap.

## Who can read what

- Environment variable values are encrypted with a KMS key that only the control function and your administrators can use. The routers, the dashboard and your apps can’t read other projects’ values.
- The builder can’t reach the install’s state, secrets or other projects. It reads its source and writes its output through URLs signed for that one build.
- Project settings and team data live in DynamoDB tables whose resource policies refuse everyone but the services that need them, with point-in-time recovery for 35 days.
- Every change is recorded with who made it.
