Overview
The storage system provides:- High performance - Synchronous, memory-mapped I/O
- Type safety - Generic Storage class with schema validation
- Scoped storage - Separate instances for different data domains
- Encryption support - Optional encryption for sensitive data
- JSON serialization - Automatic handling of complex types
Storage Instances
Rainbow exports several pre-configured storage instances:src/storage/index.ts
Storage Class
The genericStorage class provides type-safe access:
src/storage/index.ts
Schema Definition
Define storage schemas for type safety:src/storage/schema.ts
Basic Usage
Reading and Writing
Multiple Values
Clear All
Creating Custom Storage
Define Schema
Use in App
Encryption
Create Encrypted Storage
Add/Remove Encryption
Store Integration
MMKV storage integrates with Rainbow stores:RainbowStore Persistence
rainbowStorage (MMKV):
src/state/internal/rainbowStorage.ts
Query Store Persistence
Performance Characteristics
Speed Comparison
| Operation | MMKV | AsyncStorage |
|---|---|---|
| Write | 0.1ms | 10-50ms |
| Read | 0.05ms | 5-20ms |
| Type | Synchronous | Asynchronous |
Why MMKV?
Synchronous API
Synchronous API
No async/await needed - simpler code:
Memory-mapped I/O
Memory-mapped I/O
MMKV uses mmap for direct memory access, avoiding serialization overhead.
Built-in encryption
Built-in encryption
Native AES encryption for sensitive data:
Cross-platform
Cross-platform
Same API and performance on iOS and Android.
Data Migration
Handle version migrations:Legacy Storage
For migrating from AsyncStorage:src/storage/legacy.ts
Best Practices
Define schemas
Define schemas
Always define TypeScript schemas for type safety:
Use separate instances
Use separate instances
Create separate Storage instances for different domains:
Encrypt sensitive data
Encrypt sensitive data
Use encryption for API keys, tokens, etc.:
Handle undefined values
Handle undefined values
Always check for undefined when reading:
Version your data
Version your data
Track schema versions for migrations:
Common Patterns
Feature Flags
First-Time User Detection
Analytics Opt-Out
Cached Responses
Debugging
View Storage Contents
Clear All Storage
Testing
Next Steps
Analytics
Learn about event tracking
Logging
Understand the logging system