Stable Memory & Canister Upgrades
What This Is
Stable memory is persistent storage on Internet Computer that survives canister upgrades. Whether ordinary variables survive depends on the language:
- Rust -- heap data (
thread_local! { RefCell<T> }, plain statics) is wiped on every upgrade. Any data you care about must live in stable memory, either through ic-stable-structures or by serializing it yourself in pre_upgrade/post_upgrade.
- Motoko -- enhanced orthogonal persistence is on by default, so
let and var in a persistent actor already survive upgrades with no stable keyword and no upgrade hooks. Only transient declarations are wiped.
Prerequisites
- For Motoko: mops with
core = "2.0.0" in mops.toml, and moc >= 1.7.0 (dot notation on Map/List needs it)
- For Rust:
ic-stable-structures = "0.7" in Cargo.toml
Canister IDs
No external canister dependencies. Stable memory is a local canister feature.
Mistakes That Break Your Build
Using thread_local! { RefCell<T> } for user data (Rust) -- This is heap memory. It is wiped on every canister upgrade. All user data, balances, settings stored this way will vanish after icp deploy. Use StableBTreeMap instead.
Forgetting #[post_upgrade] handler (Rust) -- Without a post_upgrade function, the canister may silently reset state or behave unexpectedly after upgrade. Always define both #[init] and #[post_upgrade].
Using stable keyword in persistent actors (Motoko) -- In mo:core persistent actor, all let and var declarations are automatically stable. Writing stable let produces warning M0218 and stable var is redundant. Just use let and var.
Confusing heap memory limits with stable memory limits (Rust) -- Heap (Wasm linear) memory is limited to 4GB for wasm32 and 6GB for wasm64. Stable memory can grow up to hundreds of GB (the subnet storage limit). The real danger: if you use pre_upgrade/post_upgrade hooks to serialize heap data to stable memory and deserialize it back, you are limited by the heap memory size AND by the instruction limit for upgrade hooks. Large datasets will trap during upgrade, bricking the canister. The solution is to use stable structures (StableBTreeMap, StableCell, etc.) that read/write directly to stable memory, bypassing the heap entirely. Use MemoryManager to partition stable memory into virtual memories so multiple structures can coexist without overwriting each other.
Treating a rejected upgrade as data loss (Motoko) -- Enhanced orthogonal persistence checks the new program against the existing state and either accepts the upgrade or rejects it before it takes effect. A rejected upgrade is not data loss: the canister keeps running the previous version with its state intact, so you fix the code and redeploy.
Which schema changes need a migration, and how to write one, are owned by migrating-motoko-actors (the mops-managed migration chain); load it for the rules and syntax. The one thing to know here is the common surprise: changing a record type that an existing persistent variable holds is an incompatible change and needs a migration, even for an optional added field, and regardless of where the record lives (a top-level var, a Map value, etc.). Whether adding a new stable field itself needs a migration depends on the project's setup — under the enhanced migration chain a stable field is declared type-only, so a new one needs a migration; a plain persistent actor with an initializer on the new field does not — which is exactly why the rules live in that skill rather than as a table here. An incompatible upgrade is rejected at runtime with RTS error: Memory-incompatible program upgrade (the old state stays intact — it is not data loss).
Don't discover this at deploy time. Configure [canisters.<name>.check-stable] in mops.toml so mops check runs the stable-compatibility check against the previous version's .most (load mops-cli): it tells you at check time whether a change is compatible or needs a migration, instead of surfacing the RTS error on the upgrade call.
Serializing large data in pre_upgrade (Rust) -- pre_upgrade has a fixed instruction limit. If you serialize a large HashMap to stable memory in pre_upgrade, it will hit the limit and trap, bricking the canister. Use StableBTreeMap which writes directly to stable memory and needs no serialization step.
Mismatching the actor form and the --default-persistent-actors flag (Motoko) -- Both plain actor and persistent actor are correct; which one compiles depends on that compiler flag. Enabling it is the recommended setup, because it keeps the code less verbose:
# mops.toml — recommended; see `mops-cli`
[moc]
args = ["--default-persistent-actors"]
With the flag, every actor is persistent, so write plain actor { }; the persistent keyword still compiles but is redundant (warning M0217). Without the flag, persistent actor { } is required, and plain actor fails with [M0220] this actor or actor class should be declared persistent. No annotation rescues it — stable, transient, and pre_upgrade/post_upgrade hooks all still fail, because the error is on the actor itself:
// without --default-persistent-actors
actor {
stable var users = Map.empty<Nat, Text>(); // M0220 — `stable` does not help
};
persistent actor {
let users = Map.empty<Nat, Text>(); // compiles; persisted automatically
};
The examples below use persistent actor, which compiles either way.
One trap when the flag is off: if the actor body has un-annotated let/var, moc reports [M0219] this declaration is currently implicitly transient, please declare it explicitly transient on those lines and M0220 never appears. Do not follow that suggestion — transient discards the data on upgrade, and the actor still fails M0220. The fix is persistent on the actor, or the flag.
Implementation
Motoko
With mo:core 2.0, persistent actor makes stable storage trivial. All let and var declarations inside the actor body are automatically persisted across upgrades.
import Map "mo:core/Map";
import List "mo:core/List";
import Nat "mo:core/Nat"; // required: the compiler derives Map's implicit `compare` from this module
import Text "mo:core/Text";
import Time "mo:core/Time";
persistent actor {
// Types: in the actor body (as here) or in an imported module -- but never
// between the imports and the actor, which is M0141. See the `writing-motoko` skill.
type User = {
id : Nat;
name : Text;
created : Int;
};
// These survive upgrades automatically -- no "stable" keyword needed
let users = Map.empty<Nat, User>();
var userCounter : Nat = 0;
let tags = List.empty<Text>();
// Transient data -- reset to initial value on every upgrade
transient var requestCount : Nat = 0;
public func addUser(name : Text) : async Nat {
let id = userCounter;
users.add(id, {
id;
name;
created = Time.now();
});
userCounter += 1;
requestCount += 1;
id
};
public query func getUser(id : Nat) : async ?User {
users.get(id)
};
public query func getUserCount() : async Nat {
users.size()
};
// requestCount resets to 0 after every upgrade
public query func getRequestCount() : async Nat {
requestCount
};
}
Key rules for Motoko persistent actors:
let for Map, List, Set, Queue -- auto-persisted, no serialization
var for simple values (Nat, Text, Bool) -- auto-persisted
transient var for caches, counters that should reset on upgrade
- NO
pre_upgrade / post_upgrade needed -- the runtime handles it
- NO
stable keyword -- it is redundant and produces warnings
mops.toml
[package]
name = "my-project"
version = "0.1.0"
[dependencies]
core = "2.0.0"
# Required. Without a [toolchain] moc pin, mops falls back to the dfx cache and
# the build dies with `dfx: not found` (see `mops-cli`).
[toolchain]
moc = "1.9.0"
Rust
Rust canisters use ic-stable-structures for persistent storage. The MemoryManager partitions stable memory (up to hundreds of GB, limited by subnet storage) into virtual memories, each backing a different data structure.
Cargo.toml
[package]
name = "stable_memory_backend"
version = "0.1.0"
edition = "2021"
[lib]
crate-type = ["cdylib"]
[dependencies]
ic-cdk = "0.19"
ic-stable-structures = "0.7"
candid = "0.10"
serde = { version = "1", features = ["derive"] }
ciborium = "0.2"
Single Stable Structure (Simple Case)
use ic_stable_structures::{
memory_manager::{MemoryId, MemoryManager, VirtualMemory},
storable::{Bound, Storable},
DefaultMemoryImpl, StableBTreeMap,
};
use ic_cdk::{init, post_upgrade, query, update};
use candid::CandidType;
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::cell::RefCell;
type Memory = VirtualMemory<DefaultMemoryImpl>;
// -- Implement Storable for custom types --
// StableBTreeMap keys need Storable + Ord, values need Storable.
// Storable defines how a type is serialized to/from bytes in stable memory.
// Use CBOR (via ciborium) for serialization -- compact binary format, faster than candid.
#[derive(CandidType, Serialize, Deserialize, Clone)]
struct User {
id: u64,
name: String,
created: u64,
}
impl Storable for User {
// Recommended: prefer Unbounded to avoid backwards compatibility issues when adding new fields.
// Bounded requires a fixed max_size -- adding a field that increases the size will break existing data.
const BOUND: Bound = Bound::Unbounded;
fn to_bytes(&self) -> Cow<'_, [u8]> {
let mut buf = vec![];
ciborium::into_writer(self, &mut buf).expect("Failed to encode User");
Cow::Owned(buf)
}
fn into_bytes(self) -> Vec<u8> {
let mut buf = vec![];
ciborium::into_writer(&self, &mut buf).expect("Failed to encode User");
buf
}
fn from_bytes(bytes: Cow<'_, [u8]>) -> Self {
ciborium::from_reader(bytes.as_ref()).expect("Failed to decode User")
}
}
// Bound::Bounded { max_size, is_fixed_size: true } exists for fixed-size types but is NOT
// recommended -- adding a new field later will exceed max_size and break deserialization.
// Stable storage -- survives upgrades
thread_local! {
static MEMORY_MANAGER: RefCell<MemoryManager<DefaultMemoryImpl>> =
RefCell::new(MemoryManager::init(DefaultMemoryImpl::default()));
static USERS: RefCell<StableBTreeMap<u64, User, Memory>> =
RefCell::new(StableBTreeMap::init(
MEMORY_MANAGER.with(|m| m.borrow().get(MemoryId::new(0)))
));
// Counter stored in stable memory via StableCell
static COUNTER: RefCell<ic_stable_structures::StableCell<u64, Memory>> =
RefCell::new(ic_stable_structures::StableCell::init(
MEMORY_MANAGER.with(|m| m.borrow().get(MemoryId::new(1))),
0u64,
));
}
#[init]
fn init() {
// Any one-time initialization
}
#[post_upgrade]
fn post_upgrade() {
// Stable structures auto-restore -- no deserialization needed
// Re-init timers or other transient state here
}
#[update]
fn add_user(name: String) -> u64 {
let id = COUNTER.with(|c| {
let mut cell = c.borrow_mut();
let current = *cell.get();
cell.set(current + 1);
current
});
let user = User {
id,
name,
created: ic_cdk::api::time(),
};
USERS.with(|users| {
users.borrow_mut().insert(id, user);
});
id
}
#[query]
fn get_user(id: u64) -> Option<User> {
USERS.with(|users| users.borrow().get(&id))
}
#[query]
fn get_user_count() -> u64 {
USERS.with(|users| users.borrow().len())
}
ic_cdk::export_candid!();
Multiple Stable Structures with MemoryManager
use ic_stable_structures::{
memory_manager::{MemoryId, MemoryManager, VirtualMemory},
DefaultMemoryImpl, StableBTreeMap, StableCell, StableLog,
};
use std::cell::RefCell;
type Memory = VirtualMemory<DefaultMemoryImpl>;
// Each structure gets its own MemoryId -- NEVER reuse IDs
const USERS_MEM_ID: MemoryId = MemoryId::new(0);
const POSTS_MEM_ID: MemoryId = MemoryId::new(1);
const COUNTER_MEM_ID: MemoryId = MemoryId::new(2);
const LOG_INDEX_MEM_ID: MemoryId = MemoryId::new(3);
const LOG_DATA_MEM_ID: MemoryId = MemoryId::new(4);
thread_local! {
static MEMORY_MANAGER: RefCell<MemoryManager<DefaultMemoryImpl>> =
RefCell::new(MemoryManager::init(DefaultMemoryImpl::default()));
static USERS: RefCell<StableBTreeMap<u64, Vec<u8>, Memory>> =
RefCell::new(StableBTreeMap::init(
MEMORY_MANAGER.with(|m| m.borrow().get(USERS_MEM_ID))
));
static POSTS: RefCell<StableBTreeMap<u64, Vec<u8>, Memory>> =
RefCell::new(StableBTreeMap::init(
MEMORY_MANAGER.with(|m| m.borrow().get(POSTS_MEM_ID))
));
static COUNTER: RefCell<StableCell<u64, Memory>> =
RefCell::new(StableCell::init(
MEMORY_MANAGER.with(|m| m.borrow().get(COUNTER_MEM_ID)),
0u64,
));
static AUDIT_LOG: RefCell<StableLog<Vec<u8>, Memory, Memory>> =
RefCell::new(StableLog::init(
MEMORY_MANAGER.with(|m| m.borrow().get(LOG_INDEX_MEM_ID)),
MEMORY_MANAGER.with(|m| m.borrow().get(LOG_DATA_MEM_ID)),
));
}
Key rules for Rust stable structures:
MemoryManager partitions stable memory -- each structure gets a unique MemoryId
- NEVER reuse a
MemoryId for two different structures -- they will corrupt each other
StableBTreeMap keys must implement Storable + Ord, values must implement Storable
- Implement
Storable for custom types: define BOUND, to_bytes, into_bytes, and from_bytes. Use ciborium::into_writer/ciborium::from_reader for CBOR serialization (compact, fast). Prefer Bound::Unbounded -- it avoids backwards compatibility breakage when adding new fields. Bound::Bounded exists but is not recommended because exceeding max_size after a schema change breaks deserialization
- Primitive types (
u64, bool, f64, etc.), String, Vec<u8>, and Principal already implement Storable -- no manual impl needed
StableCell for single values (counters, config)
StableLog for append-only logs (needs two memory regions: index + data)
thread_local! { RefCell<StableBTreeMap<...>> } is the correct pattern -- the RefCell wraps the stable structure, not a heap HashMap
- No
pre_upgrade/post_upgrade serialization needed -- data is already in stable memory
Deploy & Test
Motoko: Verify Persistence Across Upgrades
# Start local replica
icp network start -d
# Deploy
icp deploy backend
# Add data
icp canister call backend addUser '("Alice")'
# Expected: (0 : nat)
icp canister call backend addUser '("Bob")'
# Expected: (1 : nat)
# Verify data exists
icp canister call backend getUserCount '()'
# Expected: (2 : nat)
icp canister call backend getUser '(0)'
# Expected: (opt record { id = 0 : nat; name = "Alice"; created = ... })
# Now upgrade the canister (simulates code change + redeploy)
icp deploy backend
# Verify data survived the upgrade
icp canister call backend getUserCount '()'
# Expected: (2 : nat) -- STILL 2, not 0
icp canister call backend getUser '(1)'
# Expected: (opt record { id = 1 : nat; name = "Bob"; created = ... })
Rust: Verify Persistence Across Upgrades
icp network start -d
icp deploy backend
icp canister call backend add_user '("Alice")'
# Expected: (0 : nat64)
icp canister call backend get_user_count '()'
# Expected: (1 : nat64)
# Upgrade
icp deploy backend
# Verify persistence
icp canister call backend get_user_count '()'
# Expected: (1 : nat64) -- data survived
icp canister call backend get_user '(0)'
# Expected: (opt record { id = 0 : nat64; name = "Alice"; created = ... })
Verify It Works
The definitive test for stable memory: data survives upgrade.
# 1. Deploy and add data
icp deploy backend
icp canister call backend addUser '("TestUser")'
# 2. Record the count
icp canister call backend getUserCount '()'
# Note the number
# 3. Upgrade (redeploy)
icp deploy backend
# 4. Check count again -- must be identical
icp canister call backend getUserCount '()'
# Must match step 2
# 5. Verify transient data DID reset
icp canister call backend getRequestCount '()'
# Expected: (0 : nat) -- transient var resets on upgrade
If the count drops to 0 after step 3, your data is NOT in stable memory. Review your storage declarations.
1---2name: stable-memory3description: Persist canister state across upgrades. Covers StableBTreeMap and MemoryManager in Rust, persistent actor in Motoko, and upgrade hook patterns. Use when dealing with canister upgrades, data persistence, data lost after upgrade, stable storage, StableBTreeMap, pre_upgrade traps, or heap vs stable memory. Do NOT use for inter-canister calls or access control — use multi-canister or canister-security instead.4license: Apache-2.05---67# Stable Memory & Canister Upgrades89## What This Is10Stable memory is persistent storage on Internet Computer that survives canister upgrades. Whether ordinary variables survive depends on the language:1112- **Rust** -- heap data (`thread_local! { RefCell<T> }`, plain `static`s) is wiped on every upgrade. Any data you care about must live in stable memory, either through `ic-stable-structures` or by serializing it yourself in `pre_upgrade`/`post_upgrade`.13- **Motoko** -- enhanced orthogonal persistence is on by default, so `let` and `var` in a `persistent actor` already survive upgrades with no `stable` keyword and no upgrade hooks. Only `transient` declarations are wiped.1415## Prerequisites1617- For Motoko: mops with `core = "2.0.0"` in mops.toml, and `moc >= 1.7.0` (dot notation on `Map`/`List` needs it)18- For Rust: `ic-stable-structures = "0.7"` in Cargo.toml1920## Canister IDs21No external canister dependencies. Stable memory is a local canister feature.2223## Mistakes That Break Your Build24251. **Using `thread_local! { RefCell<T> }` for user data (Rust)** -- This is heap memory. It is wiped on every canister upgrade. All user data, balances, settings stored this way will vanish after `icp deploy`. Use `StableBTreeMap` instead.26272. **Forgetting `#[post_upgrade]` handler (Rust)** -- Without a `post_upgrade` function, the canister may silently reset state or behave unexpectedly after upgrade. Always define both `#[init]` and `#[post_upgrade]`.28293. **Using `stable` keyword in persistent actors (Motoko)** -- In mo:core `persistent actor`, all `let` and `var` declarations are automatically stable. Writing `stable let` produces warning M0218 and `stable var` is redundant. Just use `let` and `var`.30314. **Confusing heap memory limits with stable memory limits (Rust)** -- Heap (Wasm linear) memory is limited to 4GB for wasm32 and 6GB for wasm64. Stable memory can grow up to hundreds of GB (the subnet storage limit). The real danger: if you use `pre_upgrade`/`post_upgrade` hooks to serialize heap data to stable memory and deserialize it back, you are limited by the heap memory size AND by the instruction limit for upgrade hooks. Large datasets will trap during upgrade, bricking the canister. The solution is to use stable structures (`StableBTreeMap`, `StableCell`, etc.) that read/write directly to stable memory, bypassing the heap entirely. Use `MemoryManager` to partition stable memory into virtual memories so multiple structures can coexist without overwriting each other.32335. **Treating a rejected upgrade as data loss (Motoko)** -- Enhanced orthogonal persistence checks the new program against the existing state and either accepts the upgrade or rejects it *before* it takes effect. A rejected upgrade is **not** data loss: the canister keeps running the previous version with its state intact, so you fix the code and redeploy.3435 Which schema changes need a migration, and how to write one, are owned by `migrating-motoko-actors` (the mops-managed migration chain); load it for the rules and syntax. The one thing to know here is the common surprise: **changing a record type that an existing persistent variable holds is an incompatible change and needs a migration**, even for an *optional* added field, and regardless of where the record lives (a top-level `var`, a `Map` value, etc.). Whether adding a *new* stable field itself needs a migration depends on the project's setup — under the enhanced migration chain a stable field is declared type-only, so a new one needs a migration; a plain `persistent actor` with an initializer on the new field does not — which is exactly why the rules live in that skill rather than as a table here. An incompatible upgrade is rejected at runtime with `RTS error: Memory-incompatible program upgrade` (the old state stays intact — it is not data loss).3637 Don't discover this at deploy time. Configure `[canisters.<name>.check-stable]` in `mops.toml` so `mops check` runs the stable-compatibility check against the previous version's `.most` (load `mops-cli`): it tells you at check time whether a change is compatible or needs a migration, instead of surfacing the `RTS error` on the upgrade call.38396. **Serializing large data in pre_upgrade (Rust)** -- `pre_upgrade` has a fixed instruction limit. If you serialize a large HashMap to stable memory in pre_upgrade, it will hit the limit and trap, bricking the canister. Use `StableBTreeMap` which writes directly to stable memory and needs no serialization step.40417. **Mismatching the actor form and the `--default-persistent-actors` flag (Motoko)** -- Both plain `actor` and `persistent actor` are correct; which one compiles depends on that compiler flag. Enabling it is the recommended setup, because it keeps the code less verbose:4243 ```toml44 # mops.toml — recommended; see `mops-cli`45 [moc]46 args = ["--default-persistent-actors"]47 ```4849 With the flag, every actor is persistent, so write plain `actor { }`; the `persistent` keyword still compiles but is redundant (warning M0217). Without the flag, `persistent actor { }` is required, and plain `actor` fails with `[M0220] this actor or actor class should be declared persistent`. No annotation rescues it — `stable`, `transient`, and `pre_upgrade`/`post_upgrade` hooks all still fail, because the error is on the actor itself:5051 ```motoko52 // without --default-persistent-actors53 actor {54 stable var users = Map.empty<Nat, Text>(); // M0220 — `stable` does not help55 };5657 persistent actor {58 let users = Map.empty<Nat, Text>(); // compiles; persisted automatically59 };60 ```6162 The examples below use `persistent actor`, which compiles either way.6364 One trap when the flag is off: if the actor body has un-annotated `let`/`var`, moc reports `[M0219] this declaration is currently implicitly transient, please declare it explicitly transient` on those lines and M0220 never appears. Do not follow that suggestion — `transient` discards the data on upgrade, and the actor still fails M0220. The fix is `persistent` on the actor, or the flag.6566## Implementation6768### Motoko6970With mo:core 2.0, `persistent actor` makes stable storage trivial. All `let` and `var` declarations inside the actor body are automatically persisted across upgrades.7172```motoko73import Map "mo:core/Map";74import List "mo:core/List";75import Nat "mo:core/Nat"; // required: the compiler derives Map's implicit `compare` from this module76import Text "mo:core/Text";77import Time "mo:core/Time";7879persistent actor {8081 // Types: in the actor body (as here) or in an imported module -- but never82 // between the imports and the actor, which is M0141. See the `writing-motoko` skill.83 type User = {84 id : Nat;85 name : Text;86 created : Int;87 };8889 // These survive upgrades automatically -- no "stable" keyword needed90 let users = Map.empty<Nat, User>();91 var userCounter : Nat = 0;92 let tags = List.empty<Text>();9394 // Transient data -- reset to initial value on every upgrade95 transient var requestCount : Nat = 0;9697 public func addUser(name : Text) : async Nat {98 let id = userCounter;99 users.add(id, {100 id;101 name;102 created = Time.now();103 });104 userCounter += 1;105 requestCount += 1;106 id107 };108109 public query func getUser(id : Nat) : async ?User {110 users.get(id)111 };112113 public query func getUserCount() : async Nat {114 users.size()115 };116117 // requestCount resets to 0 after every upgrade118 public query func getRequestCount() : async Nat {119 requestCount120 };121}122```123124Key rules for Motoko persistent actors:125- `let` for Map, List, Set, Queue -- auto-persisted, no serialization126- `var` for simple values (Nat, Text, Bool) -- auto-persisted127- `transient var` for caches, counters that should reset on upgrade128- NO `pre_upgrade` / `post_upgrade` needed -- the runtime handles it129- NO `stable` keyword -- it is redundant and produces warnings130131#### mops.toml132133```toml134[package]135name = "my-project"136version = "0.1.0"137138[dependencies]139core = "2.0.0"140141# Required. Without a [toolchain] moc pin, mops falls back to the dfx cache and142# the build dies with `dfx: not found` (see `mops-cli`).143[toolchain]144moc = "1.9.0"145```146147### Rust148149Rust canisters use `ic-stable-structures` for persistent storage. The `MemoryManager` partitions stable memory (up to hundreds of GB, limited by subnet storage) into virtual memories, each backing a different data structure.150151#### Cargo.toml152153```toml154[package]155name = "stable_memory_backend"156version = "0.1.0"157edition = "2021"158159[lib]160crate-type = ["cdylib"]161162[dependencies]163ic-cdk = "0.19"164ic-stable-structures = "0.7"165candid = "0.10"166serde = { version = "1", features = ["derive"] }167ciborium = "0.2"168```169170#### Single Stable Structure (Simple Case)171172```rust173use ic_stable_structures::{174 memory_manager::{MemoryId, MemoryManager, VirtualMemory},175 storable::{Bound, Storable},176 DefaultMemoryImpl, StableBTreeMap,177};178use ic_cdk::{init, post_upgrade, query, update};179use candid::CandidType;180use serde::{Deserialize, Serialize};181use std::borrow::Cow;182use std::cell::RefCell;183184type Memory = VirtualMemory<DefaultMemoryImpl>;185186// -- Implement Storable for custom types --187// StableBTreeMap keys need Storable + Ord, values need Storable.188// Storable defines how a type is serialized to/from bytes in stable memory.189// Use CBOR (via ciborium) for serialization -- compact binary format, faster than candid.190191#[derive(CandidType, Serialize, Deserialize, Clone)]192struct User {193 id: u64,194 name: String,195 created: u64,196}197198impl Storable for User {199 // Recommended: prefer Unbounded to avoid backwards compatibility issues when adding new fields.200 // Bounded requires a fixed max_size -- adding a field that increases the size will break existing data.201 const BOUND: Bound = Bound::Unbounded;202203 fn to_bytes(&self) -> Cow<'_, [u8]> {204 let mut buf = vec![];205 ciborium::into_writer(self, &mut buf).expect("Failed to encode User");206 Cow::Owned(buf)207 }208209 fn into_bytes(self) -> Vec<u8> {210 let mut buf = vec![];211 ciborium::into_writer(&self, &mut buf).expect("Failed to encode User");212 buf213 }214215 fn from_bytes(bytes: Cow<'_, [u8]>) -> Self {216 ciborium::from_reader(bytes.as_ref()).expect("Failed to decode User")217 }218}219// Bound::Bounded { max_size, is_fixed_size: true } exists for fixed-size types but is NOT220// recommended -- adding a new field later will exceed max_size and break deserialization.221222// Stable storage -- survives upgrades223thread_local! {224 static MEMORY_MANAGER: RefCell<MemoryManager<DefaultMemoryImpl>> =225 RefCell::new(MemoryManager::init(DefaultMemoryImpl::default()));226227 static USERS: RefCell<StableBTreeMap<u64, User, Memory>> =228 RefCell::new(StableBTreeMap::init(229 MEMORY_MANAGER.with(|m| m.borrow().get(MemoryId::new(0)))230 ));231232 // Counter stored in stable memory via StableCell233 static COUNTER: RefCell<ic_stable_structures::StableCell<u64, Memory>> =234 RefCell::new(ic_stable_structures::StableCell::init(235 MEMORY_MANAGER.with(|m| m.borrow().get(MemoryId::new(1))),236 0u64,237 ));238}239240#[init]241fn init() {242 // Any one-time initialization243}244245#[post_upgrade]246fn post_upgrade() {247 // Stable structures auto-restore -- no deserialization needed248 // Re-init timers or other transient state here249}250251#[update]252fn add_user(name: String) -> u64 {253 let id = COUNTER.with(|c| {254 let mut cell = c.borrow_mut();255 let current = *cell.get();256 cell.set(current + 1);257 current258 });259260 let user = User {261 id,262 name,263 created: ic_cdk::api::time(),264 };265266 USERS.with(|users| {267 users.borrow_mut().insert(id, user);268 });269270 id271}272273#[query]274fn get_user(id: u64) -> Option<User> {275 USERS.with(|users| users.borrow().get(&id))276}277278#[query]279fn get_user_count() -> u64 {280 USERS.with(|users| users.borrow().len())281}282283ic_cdk::export_candid!();284```285286#### Multiple Stable Structures with MemoryManager287288```rust289use ic_stable_structures::{290 memory_manager::{MemoryId, MemoryManager, VirtualMemory},291 DefaultMemoryImpl, StableBTreeMap, StableCell, StableLog,292};293use std::cell::RefCell;294295type Memory = VirtualMemory<DefaultMemoryImpl>;296297// Each structure gets its own MemoryId -- NEVER reuse IDs298const USERS_MEM_ID: MemoryId = MemoryId::new(0);299const POSTS_MEM_ID: MemoryId = MemoryId::new(1);300const COUNTER_MEM_ID: MemoryId = MemoryId::new(2);301const LOG_INDEX_MEM_ID: MemoryId = MemoryId::new(3);302const LOG_DATA_MEM_ID: MemoryId = MemoryId::new(4);303304thread_local! {305 static MEMORY_MANAGER: RefCell<MemoryManager<DefaultMemoryImpl>> =306 RefCell::new(MemoryManager::init(DefaultMemoryImpl::default()));307308 static USERS: RefCell<StableBTreeMap<u64, Vec<u8>, Memory>> =309 RefCell::new(StableBTreeMap::init(310 MEMORY_MANAGER.with(|m| m.borrow().get(USERS_MEM_ID))311 ));312313 static POSTS: RefCell<StableBTreeMap<u64, Vec<u8>, Memory>> =314 RefCell::new(StableBTreeMap::init(315 MEMORY_MANAGER.with(|m| m.borrow().get(POSTS_MEM_ID))316 ));317318 static COUNTER: RefCell<StableCell<u64, Memory>> =319 RefCell::new(StableCell::init(320 MEMORY_MANAGER.with(|m| m.borrow().get(COUNTER_MEM_ID)),321 0u64,322 ));323324 static AUDIT_LOG: RefCell<StableLog<Vec<u8>, Memory, Memory>> =325 RefCell::new(StableLog::init(326 MEMORY_MANAGER.with(|m| m.borrow().get(LOG_INDEX_MEM_ID)),327 MEMORY_MANAGER.with(|m| m.borrow().get(LOG_DATA_MEM_ID)),328 ));329}330```331332Key rules for Rust stable structures:333- `MemoryManager` partitions stable memory -- each structure gets a unique `MemoryId`334- NEVER reuse a `MemoryId` for two different structures -- they will corrupt each other335- `StableBTreeMap` keys must implement `Storable` + `Ord`, values must implement `Storable`336- Implement `Storable` for custom types: define `BOUND`, `to_bytes`, `into_bytes`, and `from_bytes`. Use `ciborium::into_writer`/`ciborium::from_reader` for CBOR serialization (compact, fast). Prefer `Bound::Unbounded` -- it avoids backwards compatibility breakage when adding new fields. `Bound::Bounded` exists but is not recommended because exceeding `max_size` after a schema change breaks deserialization337- Primitive types (`u64`, `bool`, `f64`, etc.), `String`, `Vec<u8>`, and `Principal` already implement `Storable` -- no manual impl needed338- `StableCell` for single values (counters, config)339- `StableLog` for append-only logs (needs two memory regions: index + data)340- `thread_local! { RefCell<StableBTreeMap<...>> }` is the correct pattern -- the RefCell wraps the stable structure, not a heap HashMap341- No `pre_upgrade`/`post_upgrade` serialization needed -- data is already in stable memory342343## Deploy & Test344345### Motoko: Verify Persistence Across Upgrades346347```bash348# Start local replica349icp network start -d350351# Deploy352icp deploy backend353354# Add data355icp canister call backend addUser '("Alice")'356# Expected: (0 : nat)357358icp canister call backend addUser '("Bob")'359# Expected: (1 : nat)360361# Verify data exists362icp canister call backend getUserCount '()'363# Expected: (2 : nat)364365icp canister call backend getUser '(0)'366# Expected: (opt record { id = 0 : nat; name = "Alice"; created = ... })367368# Now upgrade the canister (simulates code change + redeploy)369icp deploy backend370371# Verify data survived the upgrade372icp canister call backend getUserCount '()'373# Expected: (2 : nat) -- STILL 2, not 0374375icp canister call backend getUser '(1)'376# Expected: (opt record { id = 1 : nat; name = "Bob"; created = ... })377```378379### Rust: Verify Persistence Across Upgrades380381```bash382icp network start -d383384icp deploy backend385386icp canister call backend add_user '("Alice")'387# Expected: (0 : nat64)388389icp canister call backend get_user_count '()'390# Expected: (1 : nat64)391392# Upgrade393icp deploy backend394395# Verify persistence396icp canister call backend get_user_count '()'397# Expected: (1 : nat64) -- data survived398399icp canister call backend get_user '(0)'400# Expected: (opt record { id = 0 : nat64; name = "Alice"; created = ... })401```402403## Verify It Works404405The definitive test for stable memory: data survives upgrade.406407```bash408# 1. Deploy and add data409icp deploy backend410icp canister call backend addUser '("TestUser")'411412# 2. Record the count413icp canister call backend getUserCount '()'414# Note the number415416# 3. Upgrade (redeploy)417icp deploy backend418419# 4. Check count again -- must be identical420icp canister call backend getUserCount '()'421# Must match step 2422423# 5. Verify transient data DID reset424icp canister call backend getRequestCount '()'425# Expected: (0 : nat) -- transient var resets on upgrade426```427428If the count drops to 0 after step 3, your data is NOT in stable memory. Review your storage declarations.