akdarrah
# subscribe

New notes by email, about one a week.

no spam · one-click outor rss · json

adam@akdarrah~/posts$cat i-rewrote-this-blog-in-rust-and-you-cant-tell.md

I rewrote this blog in Rust and you can't tell

4 min #rails#rust#ai

Last night at 03:16 UTC this blog stopped running on Rails. The page you're reading came from a Rust binary. Nothing should look different: same pages, same links, same feeds, and if you were signed in, you still are.

The reason is memory. This is a small blog, and the Rails container serving it used 520 to 670 MB of RAM, mostly sitting idle. I'd seen other developers move apps to Rust and wanted to know if I could do it without a single reader noticing.

$ docker stats --no-stream akdarrah-web
NAME           CPU %     MEM USAGE
akdarrah-web   0.00%     12.5MiB      # Rails: 520–670 MiB

What "1:1" meant

I set three rules before writing any code:

  • Readers notice nothing. The HTML should be identical to what Rails sends, apart from per-request tokens.
  • Everyone stays signed in. Rust has to read and write Rails' encrypted session cookie, its CSRF tokens and its flash messages.
  • Every link still works. Sign-in links in inboxes, unsubscribe links in old newsletters (they never expire), signed image URLs, old slugs that redirect.

The third one is what makes a rewrite like this hard. A lot of Rails is formats nobody writes down: how a cookie is encrypted, how an image URL is signed, which CSS class the syntax highlighter puts on a shell prompt. Rust had to produce the same bytes.

# subscribe

Get the next note like this by email.

about one a week · no spam · one-click out · or rss

Rails as the answer key

The method that made this work: never guess what Rails does. Ask it.

For every piece I ported, Claude wrote a small Rails script that ran the real code on test inputs and saved the output as a fixture. The Rust tests compare against those files. That covered cookie encryption, sign-in tokens, the view helpers, the markdown pipeline, the syntax highlighter (every code block on the site matches byte for byte) and every email the site sends.

Then came harnesses that ran Rails and Rust side by side on two copies of the same database: a reader's whole journey (subscribe, sign in, comment, react, unsubscribe), every admin action, and the full API. Each one compared the responses and then the database rows left behind.

A preview on the real site

Tests on my laptop only go so far, so the Rust app ran in production for a day before the switch, next to Rails, at a hidden path. /__rust/archive was Rust serving the archive from the real database. It refused every write, sent no mail and ran no background jobs.

That made the real check easy: fetch every page from Rails and from Rust and compare them. At the end:

  • 86 of 86 public pages identical
  • 56 of 56 pages identical while signed in as me, including every admin screen
  • 65 of 65 image URLs byte-identical
  • 18 of 18 screenshots pixel-identical, desktop and phone

Comparing whole pages and whole journeys is where the real bugs showed up. The 404 page's links pointed back to the wrong place. Forms that send DELETE were being read as POST, so "sign out" signed you in again. No unit test would have caught either one.

The reviews

Six rounds of code review, each by a fresh agent that hadn't seen the code before. The first found one request that crashed the whole server, and the preview turned out not to be fully read-only: "sign out" on a preview page deleted a real session.

Most of the review time went into the newsletter. Rails sends it with a background job: it records that you got the post, then sends the email. If the send fails, you're recorded but never mailed, and nobody knows. I tried to do better and spent four rounds patching rules to tell "this address is bad" from "the mail server is down". Every rule had a hole.

What finally worked was deleting the rules. Now there's one: a delivery is recorded only after the mail server accepts the email. If anything fails, Rust tries again later, with longer and longer gaps, for up to five days. A deploy in the middle of a send can't lose anyone's email anymore. The trade-off: a bad address gets retried for days instead of being marked sent, and a database failure at exactly the wrong moment could send someone a duplicate. I'll take a rare duplicate over a silent drop.

The switch

The switch was one deploy. Kamal started the Rust container, waited for its health check, and moved traffic over. No downtime. Then a few steps from a runbook:

  1. Mark every post that was already live as announced, so nobody gets an old post emailed again.
  2. Turn on background jobs.
  3. Request a sign-in link to my own address, because Rust had never sent a real email before.

The email arrived in about a second. Rolling back is one command, and Rails is still sitting there, stopped.

What I'd do again

Three things made this work, and none of them are specific to Rust:

  • Use the old app as the answer key. Don't read the framework's docs and guess; run its code and save what it produces.
  • Preview on the real site before switching. Same data, same server, read-only, compared page by page.
  • Check against the framework's own list of routes, not just the URLs you think of. Comparing against Rails' route table at the very end turned up three routes I'd missed.
share
view .md
# subscribe

Liked this? Get the next note by email.

about one a week · no spam · one-click out · or rss

older hnewer lsections j/kyank code ycomments c
↑↓ move↵ open` shellesc close
akdarrah0:posts1:archive2:about3:shell* / search · ` shell
adam@akdarrah~$help
  ls [posts|tags]   list things      cat <slug>   open a post
  grep <word>       search           tree         the site
  subscribe <email>
adam@akdarrah~$tree -L 2 ~
~
├── posts/
│   ├── i-rewrote-this-blog-in-rust-and-you-cant-tell.md
│   ├── the-mac-mini-stays-home.md
│   ├── share-it-before-youre-sure.md
│   └── … 31 more
├── tags/
│   ├── ai  tools  claude-code
│   ├── video  rails  design
│   ├── process  agents  business
│   ├── api  cloudflare  games
│   ├── leadership  meta  retro
│   └── rust  work
├── about.md
├── archive/
├── feed.xml  feed.json  llms.txt
└── privacy.md  terms.md

4 directories, 57 files