Use cases · Web
Web servers and APIs in Tern
Write handlers as plain functions, get an HTTP server on every core, and deploy one small binary. No async/await, no runtime to install.
curl -fsSL https://ternlang.dev/install.sh | sh
tern new myapp && cd myappA JSON API in 16 lines
import http
import json
struct Link:
code: str
url: str
fn handle(req: http.Request) http.Response:
if req.path == "/health":
return http.text("ok")
if req.path == "/link":
return http.json(json.encode(Link("tern", "https://ternlang.dev")))
return http.not_found()
fn main():
http.serve(8080, handle)tern run main.tn # serving http://localhost:8080
curl localhost:8080/link # {"code":"tern","url":"https://ternlang.dev"}The handler is an ordinary function from Request to Response, so you test it by calling it. Built on macOS, this server is a 73 KB binary. The web apps guide adds routing, state and a database.
Benchmarks
| One server thread | Tern | Rust axum | Go net/http |
|---|---|---|---|
| HTTP plaintext requests/s | 169k | 149k | 115k |
| With middleware requests/s | 163k | 86k | 97k |
| SSE fan-out events/s | 13.6M | 3.5M | 0.51M |
| Peak memory under load | 39 MB | 44 MB | 102 MB |
| Web server binary | 254 KB | 1.3 MB | 8.1 MB |
| Rebuild after a one-line edit optimized | 0.67 s | 26.4 s | 2.54 s |
Tern’s numbers are for roost, the web framework, on the standard library’s http engine.
What comes with it
- roost: axum-style routing with path parameters, extractors, middleware, WebSockets, server-sent events and static files.
- Typed SQL for PostgreSQL and SQLite: migrations plus generated query functions, so a renamed column is a compile error.
- Green-thread tasks: a million spawned and joined in 41 ms (tokio 219 ms, Go 342 ms), and 100k waiting tasks in 28 MB.
- Static Linux builds:
tern build --target linux-staticmakes one executable that runs in aFROM scratchcontainer. - JSON encode and decode with codecs the compiler writes for your structs: 172 ms for 300k structs, against serde’s 249 ms.
Questions
Is there a web framework for Tern?
Yes: roost, an axum-style framework with routing, middleware, WebSockets and SSE. The standard library's http module also serves requests on its own.
Does Tern use async/await?
No. Handlers are plain functions on green-thread tasks, so blocking-looking code doesn't block the server.
Which databases does Tern support?
PostgreSQL and SQLite, with migrations and generated, typed query functions, plus a Redis client.
More about Tern
Tern (ternlang) is an early release: the language will change, and so will these numbers. Benchmarks run on an Apple M4 Pro; the full tables, losses included, are on the home page.