MENU
Systems & Tools

Serverless Runner

Repository
Peak RPS14,972
P99 Latency31.5ms
Memory Cap64 MB
Fuel Limit100M instr.

A WebAssembly-based serverless execution engine built in Rust. The system sandboxes user functions via Wasmtime with 64MB memory caps and 100M-instruction fuel limits, decouples database writes through async micro-batching over tokio channels, and eliminates cold starts with a DashMap module cache. Deployed to a 32-replica Kubernetes cluster, it reaches 14,972 RPS at a P99 latency of 31.5ms.

#Rust#Kubernetes

Wasm Execution Engine

The core of the platform is a secure, sandboxed execution environment powered by Wasmtime. To prevent malicious or runaway code from affecting the host, I implemented strict resource limits: memory caps at 64MB and fuel-based instruction metering at 100M instructions. Memory pipes for stdin and stdout keep all host-guest communication in-memory, eliminating I/O round-trips.

Async Micro-Batching

Database persistence is frequently the bottleneck in high-throughput systems. To decouple the request path from the database write, each execution result is dropped into a tokio::sync::mpsc channel. A background worker aggregates these results and flushes them in batches using the PostgreSQL UNNEST operator, inserting hundreds of records in a single round-trip and cutting transaction overhead significantly.

Module Caching

To eliminate the cold-start penalty common in serverless platforms, I implemented a global module cache using DashMap. Re-compiling a Wasm binary on each request costs 50ms or more. Storing the compiled wasmtime::Module in memory reduces instantiation to under 1ms, turning a CPU-intensive compilation step into a hash map lookup.

Performance Results

The final architecture was deployed to a Kubernetes cluster with 32 replicas of the runner service and a sharded PostgreSQL backend. Benchmarked with oha, the system reached a peak throughput of 14,972 RPS with a P99 latency of 31.5ms. That is a 90x improvement over the initial single-process baseline.