Rust for platform libraries: crate APIs people adopt, secrets in types, hot-path overhead

A platform crate succeeds on defaults, a one-line common case and cheap migration. Rust's types can make misuse a compile error. On a hot path nothing allocates, locks or formats on the critical thread.

Read the briefing
About 3 min to read 6 drills

What to remember

  1. The right default is the feature. Nobody reads your docs under deadline. If the one-line call is safe and fast, that's what the fleet will run.
  2. Make the mistake not compile. A Secret type with no Debug or Display turns a code-review comment into a compile error.
  3. Zeroize isn't magic. A move can leave a copy behind, and so can the allocator. Say what it guarantees and what it doesn't.
  4. Record on the hot thread, format on another. Formatting and I/O belong off the critical core. Put a fixed-size record in a ring, and let another core do the rest.
  5. Say honestly what level you're at. This is the job's first bullet, and a good interviewer finds your level in two questions. Show how you think, and say where you stop.

The big picture

The job asks you to own the Rust libraries every service imports: logging, metrics, tracing, secrets and config. That's more about API design and adoption than clever Rust, but you must read and reason about Rust fluently. Learn the design ideas, write some by hand, and be precise about your level.

The mental model

You're designing a power socket. Everybody will plug in without reading anything, it has to be hard to wire wrongly, and you can't change its shape later without visiting every house.

Diagrams

A Secret type
pub struct Secret<T: Zeroize>(Box<T>);

// no Debug, no Display, no Serialize: logging
// or printing one is a compile error

impl<T: Zeroize> Secret<T> {
    pub fn expose(&self) -> &T { &self.0 }
}
impl<T: Zeroize> Drop for Secret<T> {
    fn drop(&mut self) { self.0.zeroize(); }
}

In depth

APIs people adopt

The common case takes one line and is safe by default. Advanced cases are possible through a builder, not required. Errors say what to do. Migration is a shim plus a codemod, rather than a memo. Then semver discipline: #[non_exhaustive] on public enums and structs so you can add variants and fields, small default features, and a deprecation path with dates. Measure adoption, and fix what makes people avoid it.

Types that prevent mistakes

A Secret<T> with no Debug or Display, and a deliberately awkward expose(), so a grep finds every use. Newtypes, so a Pubkey can't be passed as an AccountId. Typestate, so a thing that must be validated before signing has a different type once it has been: sign(ValidatedTx) can't take raw bytes.

Memory, honestly

zeroize clears the bytes you own, and stops the compiler optimising that away. But moving a value can leave a copy on the old stack frame, so box it early. Swap and core dumps need mlock and RLIMIT_CORE/PR_SET_DUMPABLE. The allocator may hold freed pages. So it's defence in depth against accidents, not a guarantee against root.

The hot path

On a critical thread: no allocation after start-up, no locks that can be contended, no formatting, no syscalls you can avoid. Metrics are per-thread or atomic counters, histograms are pre-sized (HDR style), logs are fixed-size records in a single-producer ring read by another core. tracing is cheap when a callsite is disabled, but enabled spans and their subscribers cost real time, so it's a choice per callsite, and benchmarks in CI hold the line.

Errors and panics

A library on a critical path doesn't panic on bad input. It returns typed errors, avoids unwrap and unchecked indexing on untrusted data, and decides what a poisoned lock means. A panic in a signer is an availability incident, and panic = abort versus unwinding is a decision to make deliberately.

Myths and facts

Myth: Rust's memory safety means the code is secure.

Fact: It removes a big class of bugs. Logic errors, leaks through logs, panics as denial of service and unsafe blocks in dependencies remain.

Myth: Zeroizing a key on drop means it's gone from memory.

Fact: Earlier moves and copies can remain, and so can swap and core dumps. It clears what you own, at that moment.

Myth: A good library gets adopted because it's good.

Fact: It gets adopted when migrating is cheap and the default is right. Otherwise everyone keeps their own version, which is what the job post says is happening.

How pros do it

  • Make the one-line call safe and fast, and put the options behind a builder.
  • Use #[non_exhaustive], small default features and dated deprecations.
  • Give secrets their own type, and make exposing one easy to grep for.
  • Pre-allocate, and benchmark hot paths in CI against a budget.
  • Run cargo-deny and keep dependencies few in anything near a key.

Questions people ask

How do I talk about my Rust level honestly?

Say what you've done exactly: run it in production, built its pipelines, directed and reviewed an engine with AI writing most of it, and what you've written by hand. Then show you can reason about an API. Being accurate reads as senior. Being caught out doesn't.

They ask me to design a logging crate. Where do I start?

With who uses it and what's wrong today: every service logs its own way. Then the one-line call, the default that's right, and the hot-path variant that records without formatting. Then redaction by type, and how fifty services migrate.

async or threads for a hot path?

Pinned threads with busy-polling for the hottest path, because an async runtime's scheduling adds jitter. async is fine around it, for the control plane and I/O that isn't critical. Say what you'd measure to decide.

Further reading

Drills · 6

Answer in your head, or out loud, before you reveal. Mark each one: "Again" brings it back, "Got it" counts on your desk.

1 Design round

Every service here logs, emits metrics and reads config its own way. Design the crate they should all import, including what it costs on the hot path, and how you'd get them onto it.

  • Some services have a microsecond budget on a critical thread
  • No mandate: teams adopt it because it helps them
In short

One safe line for the common case, a separate no-allocation form for the hot path, secrets that can't be logged, a measured budget, and migration cheap enough that people choose it.

A strong answer covers
  • A one-line common case with defaults that are right
  • A separate hot-path form: fixed records in a ring, formatted on another core
  • Secrets can't be logged, by type
  • Overhead is measured, with a budget in CI
  • A migration path: a shim, a codemod, and adoption measured
  • Semver and deprecation discipline once others depend on it
What a strong answer does
  • API and defaults: A one-line safe default, with options behind it, and secrets that can't be logged.
  • Hot path: A separate record-now, format-later path, with a measured budget.
  • Adoption: Cheap migration, a shim or codemod, adoption measured, and a reason teams want it.
Why

It's an adoption question as much as a Rust question. Candidates who only design the types miss that today "each of them does it its own way" for a reason: migrating costs them and helps someone else.

The common mistake

Designing a beautiful, configurable framework. The word that loses here is 'flexible'.

2 Read the output

You're reviewing this. What happens, and what would you change so it can't happen again?

#[derive(Debug, Clone)]
pub struct Creds {
    pub user: String,
    pub api_token: String,
}

tracing::info!(?creds, "connected to venue");
  1. It logs safely: tracing redacts fields with 'token' in the name. tracing does no such thing.
  2. The token is written to the logs in plain text. Give it a type with no Debug, so this doesn't compile. ?creds uses Debug, and the derive prints every field. A type removes the whole class.
  3. It won't compile: Debug can't be derived for structs with public fields. It compiles fine, which is the problem.
  4. It only leaks at debug level, so production is safe. ? means Debug formatting, not debug level. This is an info event.
In short

It logs the token in plain text; give the token a type with no Debug, so this can't compile.

Why

This is how seed phrases end up in monitoring systems. A redaction regex over logs is a weak second line; a type that can't be printed is a first line that doesn't need anyone to remember.

The common mistake

Saying 'I'd write a custom Debug that masks it', which is fine, but then a new field gets added and the derive comes back.

3 Pick the strongest answer

A teammate says: "Our key is wrapped in Zeroizing, so it's guaranteed to be gone from memory on drop." What's the honest correction?

  1. It clears those bytes at drop. Earlier moves can leave copies, and swap and core dumps need their own controls. It helps against accidents and doesn't stop root. Being precise about what a control does is the signal.
  2. It doesn't work at all, because the compiler removes the writes. That's exactly what zeroize is built to stop.
  3. It's right: zeroize guarantees no copies exist. It can only clear what it can reach.
  4. Rust never copies data on a move. A move is a bitwise copy, and the old location isn't cleared. Boxing early limits this.
In short

It clears those bytes, but moves can leave copies, and swap, core dumps and root remain, so it's protection against accidents, not a guarantee.

Why

Box the key early, so moves pass a pointer. mlock to stay out of swap, no core dumps. And state plainly that root on the box can still read it.

4 Pick the strongest answer

A trading thread on a microsecond budget needs to emit a log line for each order. What's the right design?

  1. Use a logging macro with async stdout, since async is non-blocking. It still allocates and formats on the thread, and the runtime adds jitter.
  2. Don't log on that thread at all, and infer everything from metrics. You'd lose what you need during an incident. Cheap recording is possible.
  3. Log to a file with a mutex, flushing every line so nothing's lost. Lock contention and a syscall per line are what you're avoiding.
  4. Write a fixed-size record to a single-producer ring, and have another core format and ship it. The hot thread does a bounded write with no allocation, no lock and no syscall.
In short

Write a fixed-size record to a single-producer ring on the hot thread, and format and ship it from another core.

Why

Decide what happens when the ring is full: usually drop and count for diagnostics, never block. For audit records of signatures, the answer is different, because a dropped record breaks the guarantee, so there it's a decision to stop.

5 Answer out loud

The role needs someone proficient in Rust, designing crate APIs other people depend on. Tell me honestly where you are with that.

In short

Say exactly what you've done and haven't, without inflating or apologising, then show how you think about an API and how you'd close the gap.

What a strong answer does
  • Accuracy: Says exactly what's been run, directed and written by hand.
  • Reasoning shown: Reasons about a real API decision, and why it's shaped that way.
  • Closing the gap: Something already started, with a date for the next piece.
Why

This is an honesty test as much as a skills test. Saying exactly what's true, then showing how you'd reason about a real API, is much stronger than either bluffing or apologising. Only you know the true answer, so write it before the round.

The common mistake

Overclaiming, because one question will find it. Also underselling: having run Rust in production and directed a Rust engine is real experience. Say it exactly.

6 Quick recall

Which Rust attribute lets you add a variant to a public enum later without breaking other people's code?

In short

[non_exhaustive]: outside users must handle unknown variants, so adding one doesn't break them.

Accepted

non_exhaustive · #[non_exhaustive] · non exhaustive

Why

Downstream matches must include a wildcard arm. On structs it also stops outside code building them by literal, so you can add fields.

Next lesson The signing policy proxy: decide, sign, record, and fail safely on a latency budget
Drill · Rust for platform libraries
0:00

Loading…