Runtime & scaling
How function workers run your code, and how timeouts, warm workers and concurrency settings affect latency and throughput.
Workers
Function code runs in workers: Node.js 22 processes started by the API. Each worker belongs to one function and handles that function's events. When an event arrives, the API sends it to a free worker or starts a new one.
The first event of a fresh worker pays a cold start: Node.js starts and your module and its dependencies are loaded. Code at the top level of your module runs once per worker, so it's the right place for expensive setup that can be reused, such as SDK clients.
import * as Bucket from "@spica-devkit/bucket";
Bucket.initialize({apikey: process.env.SPICA_API_KEY});
const cache = new Map();
export default async function (req) {
// Runs per event. The client and the cache are reused across events.
}Settings per function
| Setting | Default | Effect |
|---|---|---|
timeout | Seconds an event may run. Capped by --function-timeout. | |
warmWorkers | 0 | Workers kept started and loaded, so events skip the cold start. Capped by --function-warm-workers-max. |
concurrencyPerWorker | 1 | Events one worker runs at the same time. Capped by --function-worker-event-concurrency. |
All three accept ${ENV.KEY} placeholders, so you can tune them per environment.
When to raise concurrencyPerWorker
Raise it for I/O-bound handlers that mostly wait on the network or the database, such as API proxies. Keep it at 1 for CPU-heavy handlers and for code that keeps per-event state in module-level variables.
When to use warm workers
Use them for user-facing HTTP functions with strict latency targets. Each warm worker is a running Node.js process, so they cost memory even when idle.
Instance-wide settings
| Option | Default | Effect |
|---|---|---|
--function-timeout | 60 | Upper bound for every function's timeout, in seconds. |
--function-worker-concurrency | 2 | Workers that can run in parallel for the same function. |
--function-worker-event-concurrency | 100 | Upper bound for concurrencyPerWorker. |
--function-warm-workers-max | 10 | Upper bound for warmWorkers. 0 disables warm workers. |
--function-worker-cutover-grace-ms | 30000 | When you deploy new code under traffic, old workers keep serving until the new ones are ready. After this many milliseconds they are retired anyway, so a broken release surfaces. |
--function-limit | Maximum number of functions. | |
--function-debug | false | Starts workers in debugging mode. |
--payload-size-limit | 15 | Maximum request body size in MiB, for functions and the rest of the API. |
Deploying new code
When you save a function, Spica builds it and replaces the workers. Events that already started finish on the old code. New events go to the new workers once they're ready.
Timeouts
When an event exceeds its timeout, Spica aborts it and logs a message such as 10 seconds timeout value has been reached for function 'onPost'. An HTTP request whose handler times out gets no response from the function, so the client waits until its own timeout or the proxy's, which usually answers 504 Gateway Time-out.
Design for timeouts:
- Return HTTP responses quickly and continue heavy work elsewhere, for example in a bucket trigger or a queue consumer.
- Process scheduled work in batches that fit in the timeout.
- Set timeouts close to the real duration of the handler, so stuck events are released early.
Calling the API from functions
Functions reach the API at --function-api-url, which defaults to http://127.0.0.1:<port>, a loopback connection that skips your ingress. The devkit uses it automatically. Set it explicitly when workers can't reach the API on localhost.
Debugging
Use logs first: add console.debug lines and filter on them in Functions → Logs. For local development, run Spica with spica project start and iterate with spica dev, which applies your file changes as you save.