FAQ - Help and questions

How do I run the old and the new implementation side by side while porting?

#porting#testing#incrementalpublished 0 replies0 views
ReplyWatch
Share
Hacker NewsRedditLinkedInE-mail

Short answer

Put both versions behind one interface, keep using the old answer, run the new one alongside and record every difference. This is called shadow mode. When the differences stop, switch the answer over, keep the old code for a while, then delete it. The comparison finds boundary cases that unit tests miss.

Checked with rustc 1.93.0, Windows 11

Frequently asked question, written and answered by the Ruststudio team. Replies are open: corrections are welcome.

#1

Answer#

An incremental port needs a moment where you decide that the Rust version is good enough. Shadow mode makes that decision evidence-based. For every call, the caller receives the old implementation's answer, the new implementation is called with the same input, and any difference is logged. Users see no change until you switch.

The example has a pricing rule (10.00 per unit, 10 percent off from 50 units upwards). The port has a deliberate boundary mistake, > where the old code has >=, so that you can see what a shadow comparison reports. It was compiled and run with rustc 1.93.0.

src/main.rsrust
trait Pricer {
    fn price_cents(&self, qty: u32) -> u64;
}

struct Old;
impl Pricer for Old {
    fn price_cents(&self, qty: u32) -> u64 {
        // 10.00 each, 10 percent off from 50 units upwards
        let base = qty as u64 * 1000;
        if qty >= 50 { base * 90 / 100 } else { base }
    }
}

struct Ported;
impl Pricer for Ported {
    fn price_cents(&self, qty: u32) -> u64 {
        let base = qty as u64 * 1000;
        // deliberate bug for this example: the boundary is > instead of >=
        if qty > 50 { base * 90 / 100 } else { base }
    }
}

fn shadow<'a>(old: &dyn Pricer, new: &dyn Pricer, inputs: impl Iterator<Item = u32> + 'a) -> Vec<(u32, u64, u64)> {
    inputs
        .filter_map(|q| {
            let (a, b) = (old.price_cents(q), new.price_cents(q));
            (a != b).then_some((q, a, b))
        })
        .collect()
}

fn main() {
    let diffs = shadow(&Old, &Ported, 0..=200);
    println!("{} inputs differ", diffs.len());
    for (q, a, b) in diffs {
        println!("qty {q}: old {a}, new {b}");
    }
}
text
1 inputs differ
qty 50: old 45000, new 50000

Of 201 inputs, one differs, and it is the boundary. Hand-picked unit tests often miss exactly that one.

How to do it#

  1. Define the interface that both versions implement (a trait, as above, or an extern "C" function when the old code is in C). Keep it narrow: plain data in, plain data out.
  2. In the caller, call the old version first and use its result. Call the new version second, compare, and log a difference with the inputs and both outputs. Never let a failure in the new code change what the caller receives.
  3. If the old code has side effects (writes to a database, sends mail), the new version must not repeat them: give it a read-only or in-memory stand-in during the shadow period.
  4. Feed it real inputs: recorded production requests, or the inputs of the golden-file tests (see the thread on golden files).
  5. When the difference log stays empty over a period that covers your real traffic patterns (end of month, peak load), switch the answer over, keep the old code callable for a while, and then delete it.

Common mistakes#

  • Comparing floating-point results with ==. Decide a tolerance, or compare formatted output, and write down which. Two correct implementations can differ in the last digit.
  • Comparing only the "happy" answers. Compare errors too: the old code's error for bad input is part of its behaviour.
  • Running the shadow in the same thread and letting a slow new version delay the user. Run it after the response, or in a separate task.

Does shadow mode work when the old code is in another language?#

Yes. The old code can stay where it is, and the new Rust code can be called through FFI, a local process or a service (see the threads on PyO3, ctypes and napi-rs). The comparison logic lives in whichever side is easier to change.

How is this different from a unit test?#

A unit test checks the cases someone thought of. Shadow mode checks the cases that actually occur.

Sources#

    QuoteReply
    Share
    Hacker NewsRedditLinkedInE-mail

    No replies yet