Could Trails Work in a Serverless Environment?

Just before noon on November 18, I typed a question into a coding agent session: Could trails work in a serverless environment?

Nobody had tried. There was no story for it and no RFC. Four hours later there was a PR: a trails app bundled into a single esbuild file, deployed to AWS Lambda on Node 26, serving requests from both SQLite and RDS PostgreSQL, measured, and torn down. A few hours later, I asked a better question: where else could trails go that Rails couldn't?

Why Rails doesn't go there

You can run Rails on Lambda. People do. But it's always been a fight, for two reasons.

The first is runtime support. Ruby is a second-class citizen on most serverless platforms, and on the newer ones it isn't a citizen at all. Edge runtimes like Cloudflare Workers and Deno Deploy run JavaScript. If your framework is Ruby, those doors are closed.

The second is packaging. A Rails app is a Ruby interpreter, a bundle of gems, native extensions compiled for the right architecture, and a filesystem layout that Rails expects to walk at boot. Getting that into a function means layers or container images, and a lot of work making sure the native pieces match.

Trails has neither problem. It's TypeScript, so it runs wherever JavaScript runs. And it bundles.

One file

Bundling is where the afternoon went. Rails assumes a filesystem: Zeitwerk walks app/ to find constants, views are read and compiled from disk, the app finds its root by looking for config.ru. A bundle has none of that.

Most of the pieces already existed. trails build already writes an ahead-of-time entry that turns config, routes and environment loads into literal import() calls a bundler can follow. The spike found what was still missing, and each one turned into a framework fix with a test:

  • An esbuild crash. Three classes were assigned to static fields with the same name as an aliased import. esbuild renamed the import back to that name, and the bundle threw at load.
  • The app root. The boot map located compiled files relative to its own file, which pointed one directory too high once it was inlined into a bundle. It now finds the root the way the app does, which is the way Rails does: look for the rackup file.
  • Autoloading without a directory walk. The Zeitwerk manifest now ships in the build, so a production boot doesn't read app/.
  • Precompiled views. Views load from the build's module map, not from disk.

The result is one 8 MB file and a handler. The Lambda handler and a fetch-style handler both turn a request into a Rack env and the response back, the same way a Rails app sits behind Rack.

The numbers

On Lambda, at 1 GB of memory:

SQLite RDS Postgres
Cold boot 1.67 s 1.43–1.86 s
First request 355 ms 280–310 ms
Warm request 8–14 ms 7–30 ms

The number I compare that cold start to is a sleeping Heroku dyno. Waking one up used to take 10 to 30 seconds. Trails takes under two seconds, from nothing, in a function that costs nothing while it sits idle.

Bundling is most of that win. Measured locally, from starting the process to the first response, over ten fresh starts:

Median 90th percentile
Unbundled 1.95 s 2.05 s
One esbuild bundle 1.11 s 1.24 s
Bundle plus a warm compile cache 0.91 s 0.97 s

About 80% of what's left is the JavaScript engine parsing and evaluating modules, not anything Rails-shaped. Trails.initialize, all 76 initializers, takes about 160 ms. The first request takes about 75 ms.

Some of the follow-ups have already landed. A SQLite app's bundle no longer carries the MySQL and Postgres drivers. The class-path tables that map Ruby constant names to modules were costing 26 ms of boot, and now cost 11. A Vite build of the same app, which used to crash on an import cycle in ActiveRecord, now serves requests too.

One didn't work out. I hoped a V8 startup snapshot of a booted app could skip most of the boot. For now it can't: the snapshot builder only accepts a single CommonJS script, and the dependency graph has top-level awaits in it. That's a no for now, not a no forever.

Where else

The port is close to done. For most of this project the question was whether trails matched Rails. Increasingly, the question is what a Rails that runs on JavaScript can do that Rails can't.

Lambda is the only one proven so far. The rest are ideas, and I'll say so until each one has a test that boots an app there:

  • Edge workers. Cloudflare Workers is the obvious next target, and the harder one: no filesystem, no native modules, no runtime import(), and tight CPU limits on boot.
  • Phones. Rails reaches mobile through native shells around a web view of a server. Trails could run inside the app, with ActiveRecord over on-device SQLite. The same models, validations and migrations on the phone and the server.
  • Local-first in the browser. ActiveRecord over SQLite in the browser's own storage, working offline and syncing later.
  • Desktop apps. A whole Rails-shaped app in one installer, without packaging a Ruby interpreter.
  • Inside other JavaScript apps. A Next.js or SvelteKit project could pick up ActiveRecord, ActiveModel validations or ActiveJob without becoming a Rails app. You can't pull Ruby gems into a JavaScript stack.
  • Shared validations. The same ActiveModel validations running in the form and on the server, without a second schema in JavaScript.

I don't have plans for most of these. That's not the point yet. The point is that all this time the work has been making trails behave exactly like Rails, and the payoff turns out to be all the places it can go that Rails can't.

The Lambda example has merged into trails, Terraform included.

Comments

Leave a Comment

Used for confirmation only. Not displayed publicly.