
I built an open-source, high-concurrency state machine for AI tokens using Scala 3, Pekko, and GraalVM
Hey r/scala,
I recently, I noticed AI startups hitting massive race-condition problems when trying to track API token usage natively in Postgres.
I decided to build a standalone, open-source quota firewall to solve this, and I wanted to push the JVM to its limits.
The architecture:
- Written in Scala 3.
- Core engine uses Apache Pekko (Typed/Cluster). The wallet state lives entirely in-memory on a single Actor, making double-spending mathematically impossible without any row-level database locking.
- Event Sourced via Pekko Persistence R2DB, JDBC (Postgres) for the immutable audit log.
- I set it up to compile to a standalone GraalVM native image for instant startup and tiny memory footprints.
It was an awesome challenge figuring out how to handle "Zombie Holds" (when an LLM API times out and freezes a user's tokens) using Pekko's TimerScheduler.
I'd love for some experienced Scala devs to tear apart the architecture or give me feedback on the GraalVM/Pekko setup.
Repo is here: https://github.com/baseledger-io/baseledger