Maiko
Event-driven actors for Tokio
What is Maiko?
Maiko is a lightweight actor runtime for building event-driven concurrent systems in Rust. Unlike traditional actor frameworks, Maiko actors communicate through topic-based pub/sub rather than direct addressing, making them loosely coupled and ideal for stream processing workloads.
The Problem Maiko Solves
Building complex Tokio applications often leads to channel spaghetti:
// Without Maiko: Manual channel orchestration
let = channel;
let = channel;
let = channel;
let tx1_clone = tx1.clone;
let tx2_clone = tx2.clone;
spawn;
spawn;
// ... and it gets worse with more tasks
With Maiko, channels disappear from your code:
// Actors just subscribe to topics - Maiko handles all routing
sup.add_actor?;
sup.add_actor?;
sup.add_actor?;
// No manual channel creation, cloning, or wiring needed!
Maiko manages the entire channel topology internally, letting you focus on business logic instead of coordination.
Why "Maiko"?
Maiko (舞妓) are traditional Japanese performers known for their coordinated dances and artistic discipline. Like maiko who respond to music and each other in harmony, Maiko actors coordinate through events in the Tokio runtime.
When to Use Maiko
Maiko excels at processing unidirectional event streams where actors don't need to know about each other:
- System event processing - inotify, epoll, signals, device monitoring
- Data stream handling - stock ticks, sensor data, telemetry pipelines
- Network event processing - packet handling, protocol parsing
- Reactive architectures - event sourcing, CQRS patterns
- Game engines - entity systems, event-driven gameplay
Maiko vs Alternatives
Feature Comparison:
| Feature | Maiko | Actix | Ractor | Tokio Channels |
|---|---|---|---|---|
| Pub/Sub Topics | ✅ | ❌ | ❌ | ❌ |
| Actor Addressing | ❌ | ✅ | ✅ | N/A |
| Supervision Trees | ❌ | ✅ | ✅ | N/A |
| Loose Coupling | ✅ | ❌ | ❌ | ✅ |
| Event Metadata | ✅ | ❌ | ❌ | ❌ |
| Correlation Tracking | ✅ | ❌ | ❌ | ❌ |
| Type-Safe Routing | ✅ | ✅ | ✅ | ✅ |
| Learning Curve | Low | Medium | Low | Low |
Use Case Guide:
| Use Case | Best Choice |
|---|---|
| Event streams, pub/sub patterns | Maiko ✅ |
| Request/reply, actor addressing | Actix, Ractor |
| Supervision trees, fault tolerance | Actix, Ractor |
| Simple async tasks | Tokio tasks |
| Distributed messaging | Kafka, NATS |
Quick Start
Add Maiko to your Cargo.toml:
[]
= "0.1.0"
= { = "1", = ["full"] }
Hello World Example
use *;
// Define your events
// Create an actor
async
Core Concepts
1. Events
Events are messages that flow through the system. They must implement the Event trait:
2. Topics
Topics route events to interested actors. Define custom topics for fine-grained control:
Or use Broadcast to broadcast to all actors:
sup.add_actor?;
3. Actors
Actors are independent units that process events asynchronously:
4. Context
The Context provides actors with capabilities:
// Send events to topics
ctx.send.await?;
// Send with correlation (for tracking related events)
ctx.send_child_event.await?;
// Check if system is still running
if !ctx.is_alive
// Get actor's name
let name = ctx.name;
5. Actor Patterns: Handle vs Tick
Maiko actors typically follow one of two patterns:
Handle-Heavy Actors (Event Processors):
// Telemetry actor - processes many incoming events
Tick-Heavy Actors (Event Producers):
// Stock data reader - polls external source, emits many events
// System event monitor - polls inotify/epoll
The tick() method runs when the event queue is empty, making it perfect for:
- Polling external sources (WebSockets, file descriptors, system APIs)
- Periodic tasks (metrics reporting, health checks)
- Timeout logic (detecting stale connections)
- Housekeeping (buffer flushing, cache cleanup)
6. Supervisor
The Supervisor manages actor lifecycles:
let mut sup = new;
// Add actors with subscriptions
sup.add_actor?;
sup.add_actor?;
sup.add_actor?;
// Start all actors
sup.start.await?;
// ... application runs ...
// Graceful shutdown
sup.stop.await?;
Design Philosophy
Loose Coupling Through Topics
Maiko actors don't know about each other. They only know about:
- Events they can send
- Topics they subscribe to
This is fundamentally different from Akka/Actix where actors have addresses:
// ❌ Traditional actors (tight coupling)
actor_ref.tell; // Must know the actor's address
// ✅ Maiko (loose coupling)
ctx.send.await?; // Only knows about event types
Unidirectional Flow
Events typically flow in one direction:
System Event → Parser → Validator → Processor → Logger
This makes Maiko ideal for pipeline architectures and stream processing.
Type Safety
Event types are checked at compile time. Invalid event routing is impossible:
// Compile error - wrong event type!
ctx.send.await?;
Advanced Features
Correlation IDs
Track related events across actors:
async
Error Handling
Control error propagation:
Custom Configuration
Fine-tune actor behavior:
let config = default
.with_channel_size // Event queue size per actor
.with_max_events_per_tick; // Events processed per tick cycle
let mut sup = new;
Examples
See the examples/ directory for complete programs:
- pingpong.rs - Simple event exchange between actors
- guesser.rs - Multi-actor game with topics and timing
Run examples with:
Roadmap
v0.1.0 (Current - Single Process)
- Topic-based event routing
- Async actor lifecycle hooks
- Graceful shutdown via cancellation tokens
- Correlation ID tracking
- Flexible error handling
v0.2.0 (Supervision & Control)
- Actor restart policies and strategies
- Supervisor metrics and monitoring
- Dynamic actor spawning at runtime
- Backpressure configuration
- Enhanced error recovery
v0.3.0 (Cross-Process Communication)
- IPC bridge actors (Unix sockets, TCP)
- Event serialization framework (bincode, JSON, protobuf)
- Remote topic subscriptions
- Multi-supervisor coordination
- Process-level fault isolation
v0.4.0+ (Ready-to-Use Actor Library)
- Inter-supervisor communication - Unix socket, gRPC bridge actors
- Networking actors - HTTP client/server, WebSocket, TCP/UDP handlers
- Telemetry actors - OpenTelemetry integration, metrics exporters
- Storage actors - Database connectors, file watchers, cache adapters
- Authentication and encryption for network bridges
Architecture
┌─────────────────────────────────────────────────────┐
│ Supervisor │
│ ┌────────────────────────────────────────────────┐ │
│ │ Central Broker │ │
│ │ (Topic-based routing & filtering) │ │
│ └────────────────────────────────────────────────┘ │
│ │ │ │ │
│ ┌────▼───┐ ┌────▼───┐ ┌────▼───┐ │
│ │Actor A │ │Actor B │ │Actor C │ │
│ │ │ │ │ │ │ │
│ │Topics: │ │Topics: │ │Topics: │ │
│ │ [T1] │ │[T1,T2] │ │ [T2] │ │
│ └────┬───┘ └───┬────┘ └───┬────┘ │
│ │ │ │ │
│ └─────────────┴──────────────┘ │
│ Events flow up to broker │
└─────────────────────────────────────────────────────┘
Key Components:
- Supervisor - Manages actor lifecycles and shutdown
- Broker - Routes events to subscribed actors based on topics
- Actors - Independent event processors with isolated state
- Context - Actor's interface to send events and check system state
Contributing
Contributions are welcome! Please feel free to:
- Report bugs via GitHub Issues
- Suggest features and improvements
- Improve documentation
- Submit pull requests
Code Philosophy
Maiko is 100% human-written code, crafted with passion for Rust and genuine love for coding. While AI tools have been valuable for architectural discussions, code reviews, and documentation, every line of implementation code comes from human creativity and expertise.
We believe in:
- Thoughtful design over automated generation
- Deep understanding of the code we write
- Human craftsmanship in software engineering
Contributors are expected to write their own code. AI may assist with reviews, discussions, and documentation, but implementations should reflect your own understanding and skills.
License
Licensed under the MIT License.
Acknowledgments
Inspired by:
- Kafka - Topic-based event streaming
- Akka Streams - Reactive stream processing
- Tokio - Async runtime foundation
Built with ❤️ and by humans, for humans 🦀