Backend servers
A backend in one script and one binary.
An HTTP server, actors that run on several cores, and sessions that sleep in storage are part of SCUA. The service behind a game or an app is a script, with no runtime or framework to install on the machine beside it.
import http
fn handle(req)
if req.method == "GET" and req.path == "/health" then
return { status = 200, body = b"ok" }
end
return { status = 404, body = b"not found" }
end
http.serve(handle, { bind = "127.0.0.1:8080" })?
$ scua --allow-serve=127.0.0.1:8080 serve.scua
scua: serving on http://127.0.0.1:8080/ — 1 worker (8 with SCUA_SCHEDULER=threaded), up to 10000 connections …HTTP
Serve HTTP from one plain function
http.serve accepts connections, parses requests and frames responses. Your
script supplies the handler. Opening a port is its own permission, and the grant names the
exact address the script may bind.
Each request runs with its own state, as if in a partition of its own. A handler can wait on
a database, another service or a file, and the server answers other requests meanwhile.
There is no async and no await, so a function that waits looks
like any other function.
- Streamed responses, for server-sent events and long downloads
- Streamed uploads, with body limits the server reports
- A graceful stop that finishes the requests in flight
- Routing with typed path parameters, from the
@scua/webpackage
Throughput
Ahead of Bun when the handler does work
We measured SCUA against the fast JavaScript servers on one machine. The chart is one thread each, which is SCUA's default. Where the handler does real work, a 20,000-step loop per request, SCUA answered the most requests a second, because the JIT compiles the handler from end to end. On a fixed plaintext reply, which measures the server more than the language, SCUA was ahead of Bun and behind uWebSockets.js.
On four threads SCUA was again first on the working handler: 146,210 requests a second, against 113,396 for uWebSockets.js and 106,494 for Bun. The full table is in the 0.35.0 release post.
Concurrency
State lives in actors, so nothing needs a lock
Anything that must last from one request to the next lives in a partition: a counter, a cache, a matchmaking queue, a connection pool. A partition handles one message at a time and its state is private, so two requests cannot corrupt it between them.
Partitions run on several cores by default.
For a batch of independent work, wait_all and map_all run
ordinary function calls and give the results back in order, with one failure never
cancelling the rest. Under --io=async their waits overlap.
Idle cost
Idle sessions hold no memory
Most sessions spend most of their life waiting: for the player's next move, the user's next message, a webhook. Keeping a process or a container warm for each one costs memory around the clock.
A SCUA session can be frozen to a blob, dropped from memory and stored as one object. When something arrives for it, the host loads the blob and the session carries on. A thousand idle sessions are a thousand stored objects.
A frozen session wakes into the same script that froze it. State that must survive a code change goes in a typed save, which carries a version and is upgraded by the migrate hooks you write.
Hibernate a session →Operations
One file lists everything a service may touch
A service starts with no access. You grant what it needs on the command line or in a permissions file you can review and keep in version control: which folder, which hosts, which environment variables, which address to bind.
A value set to false in that file is a refusal that holds even when somebody
runs the program with --allow-all.
- Secrets print as
<secret>, in logs and in error messages --max-memcaps the whole program, actors included--max-opsbounds the work a single turn may do
[capabilities]
fs = "./data"
net = ["api.internal", "cdn.example"]
env = ["PORT", "HOME"]
serve = "127.0.0.1:8080"
$ scua --allow-file=permissions.toml app.scuaWhat you build it from
The usual parts of a service, already written
Databases
Packages for PostgreSQL and Redis speak the wire protocols over TCP or TLS, with a connection pool. SQLite is bound through the experimental FFI.
Database packages →Calling other services
An HTTP client and TLS are built in. Packages add OAuth 2.0, JWT, AWS request signing and an OpenAPI client driven by the spec.
Fetch over HTTP →Money that adds up
decimal values and money literals such as 19.99 USD keep totals exact, in code and in data tables.
An append-only log
A durable log with an optional hash chain, which recovers from a write torn by a crash.
Keep an append-only log →Config for a fleet
A small table of settings replicated between machines by gossip, so a flag changed on one reaches the rest.
Share config across a cluster →One file to deploy
The runtime is a single binary. A program, its locked dependencies and its data can be bundled into one .scuapp file.
Packages for services
Read how a handler is written
The serving guide starts from the ten-line server above and goes as far as streaming and uploads.