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:
| Region | Holds | Lifetime |
|---|---|---|
| Stack | Local variables, return addresses, call frames | Short — pops when a function returns |
| Heap | Shared, long-lived data | Until freed or process exits |
Webhook handler mental model
When Axum receives a POST:
- A frame for
handle_webhookis pushed - Nested calls (parse JSON, publish Kafka) add frames
- When the HTTP response is sent, those frames pop
- 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()— entryWhy 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
Lab complete — nice work.