SS

Search

Jump to a page, project or post

Shipped

One pricing formula, two runtimes: Rust to WebAssembly

Wrote the shared pricing and formula logic once in Rust and compiled it to WebAssembly, so the TypeScript and Rust codebases stopped drifting apart on the numbers that matter most.

Organisation
1buy.ai
Role
Backend engineer
Date
Kind
Work
One pricing formula, two runtimes: Rust to WebAssembly: system sketchcompileRust cratepricing formulasRust servicesnative callWebAssemblyFloat64Array boundaryTypeScript servicesNestJS
System sketch. Boxes are the real components; packets show the direction of flow.
Source of truth
One Rust crate
Consumers
Rust services + TypeScript services
Boundary
Columnar Float64Array
  • Rust
  • WebAssembly
  • TypeScript
  • NestJS

Context

Pricing logic lived in two places: TypeScript services that serve the product, and Rust services that do heavy computation. Every formula change meant two implementations and a slow drift between them.

Approach

  • Moved the formulas into a single Rust crate with a small, pure API.
  • Rust services call it natively. TypeScript services call the same crate compiled to WebAssembly.
  • Passed data across the WASM boundary as columnar typed arrays rather than per-row objects, which keeps the call cheap for large batches.
  • Added a parity test that runs the same inputs through both consumers and fails if the outputs differ.

What I learned

Not everything belongs in WASM. Formulas that must run inside a MongoDB aggregation pipeline still have to be expressed in the pipeline language, so the crate is the source of truth for the logic, and the pipeline version is checked against it.