Book 0 · Lesson 0.3

Memory, stack, and heap

  • Contrast stack frames vs heap allocations
  • Relate call depth to webhook handler flow in Rust

Prerequisites: 0.1 · 0.2

Programs need memory. Two regions matter first:

RegionHoldsLifetime
StackLocal variables, return addresses, call framesShort — pops when a function returns
HeapShared, long-lived dataUntil freed or process exits

Webhook handler mental model

When Axum receives a POST:

  1. A frame for handle_webhook is pushed
  2. Nested calls (parse JSON, publish Kafka) add frames
  3. When the HTTP response is sent, those frames pop
  4. Rows in Postgres and Kafka buffers live on the heap (or owned structures) much longer

Stack grows down on paper; here we show active frames top → bottom.

Heapteam_events rows · Redis payloads · Kafka message buffers
main()— entry

Why this matters for Team Radar

A burst of webhooks means many overlapping stacks — one per concurrent request — while heap usage grows with stored events and Redis payloads.

Rust’s ownership rules (Book 1) exist partly to make heap lifetimes explicit. For now: stack = temporary work, heap = remembered state.

Teach-back prompt

When ingestion returns 200, what memory is definitely freed vs what remains for the pipeline?

Trace a call stack

Lesson 0.3 check

1. Stack memory is typically…