What is a Canister?
A canister is an evolved form of a smart contract with several key characteristics:WebAssembly Execution
Runs compiled WebAssembly code for high performance
Persistent State
Maintains state across executions with automatic persistence
Isolated Execution
Runs in sandboxed environment for security
Cycles-Powered
Uses cycles for computation and storage costs
The canister execution environment is implemented in
rs/execution_environment/ and provides the runtime for all canister operations.Canister Lifecycle
States and Transitions
A canister progresses through several states during its lifetime:- Running
- Stopping
- Stopped
The canister is active and can:
- Process incoming messages
- Execute scheduled tasks
- Send outgoing messages
- Access its persistent state
- Consume cycles for operations
Lifecycle Management
Canister Types
Application Canisters
User-deployed canisters that implement dapp logic: Characteristics:- Created by users or other canisters
- Controlled by principals (users or other canisters)
- Subject to cycles consumption
- Can be upgraded by controllers
- Implement arbitrary business logic
- DeFi protocols
- Social networks
- DAOs and governance systems
- Gaming applications
- Data storage services
System Canisters
Special canisters that provide core IC functionality:NNS Canisters
Governance, Registry, Ledger, Root, and other NNS components
Management Canister
Virtual canister (ic00) for administrative operations
SNS Canisters
Service Nervous System components for dapp governance
System Subnet Canisters
Specialized canisters on system subnets (Cycles Minting, etc.)
- Often have privileged operations
- May have special cycle charging rules
- Cannot always be upgraded through normal means
- Integrated with IC protocol
Execution Model
Message Processing
Canisters respond to different types of messages:Update Calls
Update Calls
Modify canister state and are committed to blockchain:Properties:
- 2-4 second latency (consensus delay)
- Certified responses
- Consumes cycles
- Can call other canisters
Query Calls
Query Calls
Read-only operations that don’t modify state:Properties:
- Sub-second latency
- No state changes committed
- Lower cycle cost
- Cannot call other canisters
Inter-Canister Calls
Inter-Canister Calls
Asynchronous calls between canisters:Properties:
- Asynchronous execution model
- Multiple round trips possible
- Each step goes through consensus
- Cycles transferred for computation
Heartbeats and Timers
Heartbeats and Timers
Scheduled periodic execution:Properties:
- Automatic invocation
- Useful for maintenance tasks
- Consumes cycles on each execution
- Must complete within instruction limits
Execution Limits
Canisters operate within defined resource limits:| Resource | Limit | Purpose |
|---|---|---|
| Instructions | ~5-20 billion per message | Prevent infinite loops and DoS |
| Message Size | 2 MB | Limit network bandwidth usage |
| Wasm Memory | 4 GB (32-bit) or more (64-bit) | Canister heap space |
| Stable Memory | Up to 400 GB | Persistent storage across upgrades |
| Call Stack | 500 levels | Prevent deep recursion |
Sandboxed Execution
Canisters run in isolated sandboxes for security:Process Isolation
Each canister runs in a separate OS process
Memory Protection
Canisters cannot access each other’s memory
Resource Limits
CPU and memory limits enforced by OS
Syscall Filtering
Restricted system call access via seccomp
State Management
Wasm Memory
Canister heap memory (volatile):Stable Memory
Persistent storage across upgrades:Cycles and Resource Costs
Canisters pay for resources using cycles: Cost Factors:- Computation: Instructions executed
- Storage: Bytes stored over time
- Network: Message sizes
- Special Operations: HTTPS outcalls, threshold signatures, etc.
System API
Canisters interact with the IC through the System API:Message Inspection
Message Inspection
Canister Management
Canister Management
Inter-Canister Calls
Inter-Canister Calls
Time and Randomness
Time and Randomness
Storage
Storage
Best Practices
Design for Upgrades
- Use stable memory for critical state
- Implement pre/post upgrade hooks
- Version your data structures
- Test upgrade paths thoroughly
Manage Resources
- Monitor cycle balance
- Set up cycle top-up mechanisms
- Optimize instruction usage
- Use query calls when possible
Handle Errors
- Check return values from System API
- Handle inter-canister call failures
- Implement retry logic
- Validate input data
Security First
- Validate all inputs
- Check caller identity
- Use access control
- Audit upgrade permissions
Source Code Reference
rs/execution_environment/src/execution_environment.rs: Main execution logicrs/execution_environment/src/hypervisor.rs: Wasm execution and System APIrs/canister_sandbox/src/replica_controller/: Sandbox managementrs/replicated_state/src/canister_state/: Canister state structures
Related Topics
State Management
Learn about state synchronization and persistence
Network Nervous System
Understand IC governance and system canisters
Cryptography
Explore IC’s cryptographic features
Service Nervous System
Build decentralized dapp governance