
# What a function does differently

The code is that of a server. What compiles but works differently is below.

## The table

| | On a server | In a function |
|---|---|---|
| Start | once, when the process starts | once per instance, with its first request (Supabase, AWS: while it loads) |
| TLS, HTTP/2, compression, connection limits | `server.*` | the platform |
| `server.max-http-request-body-size`, `server.request-timeout` | applied | applied |
| A target with `%2F`, `%5C`, `%00`, `.` or `..` | 400 | 400 (`requestTargetProblem`, the same check) |
| The address of the client | the peer of the socket | what the platform names; never a header a client can set |
| Server-sent events, downloads | streamed | streamed (AWS: with `--streaming`) |
| WebSocket endpoints | yes | no: `UnsupportedError` |
| Multipart uploads | on disk above a threshold | in memory |
| `dart:io`, isolates, a database driver | yes | do not compile to JavaScript: the compiler says so |
| Sessions | in memory | per instance: use tokens (`SessionCreationPolicy.stateless`) |

## Work after the answer

A platform may stop an instance as soon as its response is sent. `EdgeRuntime.waitUntil` keeps it until the work ends:

```dart
@PostMapping('/orders')
Order place(@RequestBody() Order order) {
  runtime.waitUntil(mail.confirm(order)); // after the response
  return order;
}
```

`EdgeRuntime.waitUntil` on Supabase, `ctx.waitUntil` on Cloudflare, Netlify and Vercel; on AWS the invocation ends when the work did. On a server, and on Deno Deploy, the work just runs. `EdgeRuntime.platform` is `supabase`, `cloudflare`, `aws`, `deno`, `netlify`, `vercel`, or `null` on a server.

## Work by a schedule

A timer does not outlive a request, so `@Scheduled` methods do not run between requests. The platform's schedule does that work: it runs the function, and the function calls every `EdgeScheduledHandler` bean.

```dart
@Component()
class NightlyReport implements EdgeScheduledHandler {
  NightlyReport(this.reports);
  final Reports reports;

  @override
  Future<void> onScheduled(EdgeScheduledEvent event) async {
    if (event.schedule == '0 3 * * *') {
      await reports.send();
    }
  }
}
```

- Cloudflare: a cron trigger; `event.schedule` is its expression.
- AWS: an EventBridge rule or schedule with the function as its target; `event.schedule` is the rule's ARN.
- Supabase: `pg_cron` calls an endpoint of the function over HTTP, which is an ordinary request.

A handler that fails fails the run, after the other handlers ran.

## The warning list

What compiles but works differently is said once, when a function starts:

```
WARN  Running as a function; these work differently here:
  - the rate limiter counts in memory: every instance has its own counters, ...
  - @Scheduled Rates.refresh (PeriodicTrigger(fixedRate: 1:00:00.000000)): timers do not outlive a request; ...
  - cached values (bean 'cacheManager'): kept in the memory of each short-lived instance, ...
  - sessions (bean 'httpSessionRepository'): kept in the memory of each short-lived instance, ...
```

Each line comes from an `EdgeWarningContributor` bean. The starter has three:

- `RateLimitStoreEdgeWarning`: an in-memory rate limiter.
- `TimerWorkEdgeWarning`: every `@Scheduled` method (the beans that are a `TimerDrivenWork`).
- `InstanceStateEdgeWarning`: every bean that keeps its state in the memory of its instance (the beans that are an `IsolateLocalState`: the in-memory caches and sessions; a cache kept elsewhere is not listed).

`TimerDrivenWork` and `IsolateLocalState` are `spring_core_dart`'s, and say nothing about a platform: a pool of isolates warns about the same beans. A bean of the contributor's type adds its own lines. `spring.edge.warnings: false` turns the list off.

The list is a startup check: `edgeWarnings` and the three contributors are in a module of their own, which the generator puts into the build by `spring.main.startup-checks.enabled`. `true` (in `application.yml` or `application.properties`): in every build. `false`: in none, no code, so no list. Not set: in debug and profile builds only. `dart compile js` and `wasm` do not define `dart.vm.product`, so a function compiled by them keeps the list unless the key is `false`; `spring.edge.warnings` turns it off where it is in the build.

## The rate limiter

`spring.web.rate-limit.*` and `@RateLimiter` work as on a server, but an instance's memory is not a place to count: use a shared store.

| Platform | Store |
|---|---|
| Supabase, with the Supabase starter | the project's database function (`rate_limit_acquire`, called as the service role) |
| Cloudflare | a D1 database (`spring.edge.cloudflare.rate-limit.database`, the store `d1`) |
| elsewhere | declare a `RateLimitStore` bean |

When the store fails, the request is refused (`on-store-failure: deny`, the default).

The store is checked when the function starts, which is the first request of an instance: the Supabase project's database function is called once, the D1 binding is asked for and its table created. A function that was never migrated (`sql/rate_limit.sql` of the Supabase starter) or a binding missing from `wrangler.toml` fails the start: the request is answered 500, the log says what to run, and the next request tries again. It costs one call to the store per cold start; `spring.web.rate-limit.verify-store: false` skips it. See [Rate limiting](../spring-boot-starter-web/rate-limiting.md).

## See also

- [All properties](properties.md)
- [Supabase Edge Functions](supabase.md)
- [Cloudflare Workers](cloudflare.md)
