Documentation Index
Fetch the complete documentation index at: https://mintlify.com/MotiaDev/motia/llms.txt
Use this file to discover all available pages before exploring further.
Engine Overview
The iii Engine is a WebSocket-based backend orchestration system that unifies your backend stack with two core primitives: Functions and Triggers.Architecture
The engine is built in Rust and provides a high-performance, distributed runtime for backend functions. It consists of several key components:Core Components
Engine Core
TheEngine struct (src/engine/mod.rs) is the central orchestrator that manages:
- Worker Registry: Tracks connected workers (SDK clients)
- Function Registry: Maintains all registered functions
- Trigger Registry: Manages trigger types and trigger instances
- Service Registry: Houses engine modules
- Invocation Handler: Routes and tracks function invocations
- Channel Manager: Manages WebSocket channels for real-time communication
Workers
Workers are SDK clients that connect to the engine via WebSocket. Each worker can:- Register functions
- Register triggers
- Handle function invocations
- Receive invocation results
worker_id and maintain:
- Active function registrations
- Active trigger registrations
- Pending invocations
- WebSocket channel for bidirectional communication
Functions
Functions are the basic units of work. They can:- Exist anywhere: locally, on cloud, serverless, or as HTTP endpoints
- Receive input and optionally return output
- Invoke other functions
- Be triggered by various event sources
id: Unique identifier (e.g.,"math.add")description: Optional descriptionrequest_format: Optional JSON schema for inputresponse_format: Optional JSON schema for outputmetadata: Optional metadata- Handler implementation (for engine-native functions)
Triggers
Triggers define how and when functions are invoked:- Trigger Types: Categories of triggers (e.g.,
http,queue,cron) - Trigger Instances: Specific trigger configurations
Communication Flow
How It Works
1. Initialization
The engine starts by:- Loading configuration from
config.yaml(or using defaults) - Initializing modules (REST API, Queue, Cron, Stream, Observability)
- Binding to WebSocket port (default: 49134)
- Starting background tasks
2. Worker Connection
When an SDK connects:- WebSocket handshake establishes connection
- Engine creates a
Workerinstance with unique ID - Engine sends
WorkerRegisteredmessage - Worker can now register functions and triggers
3. Function Registration
Workers register functions by sending aRegisterFunction message:
4. Trigger Registration
Workers register triggers to connect functions to event sources:RestApiModule for HTTP triggers).
5. Function Invocation
When a trigger fires:- Module creates invocation with data payload
- Engine looks up function in registry
- Engine sends
InvokeFunctionmessage to worker - Worker executes handler and returns result
- Engine forwards result back to caller
- Synchronous: With
invocation_idfor awaiting results - Fire-and-forget: Without
invocation_idfor async execution - Distributed tracing: Via W3C
traceparentandbaggageheaders
6. Distributed Tracing
The engine supports OpenTelemetry for observability:- Automatic trace propagation across function calls
- Span creation for invocations
- Metrics collection (invocations, errors, latency)
- Log correlation with trace context
Engine Configuration
The engine is configured viaEngineBuilder:
- Environment variable expansion:
${VAR:default} - Module configuration
- Port and host customization
- Adapter selection per module
Key Features
Hot Reloading
Functions and triggers can be registered/unregistered at runtime without restarting the engine.Multi-Language Support
SDKs available for:- Node.js/TypeScript
- Python
- Rust
Built-in Modules
The engine includes production-ready modules:- REST API (HTTP triggers)
- Queue (message queue with DLQ)
- Cron (distributed scheduling)
- Stream (WebSocket pub/sub)
- State (key-value store with triggers)
- Observability (OpenTelemetry)
Adapter Pattern
Modules use adapters for pluggable backends:- Queue: Redis, RabbitMQ, or in-memory
- State: Redis, KV store, or in-memory
- Cron: Redis or KV-based coordination
Service Registry
Modules register as services, enabling:- Function-to-module routing
- Module discovery
- Cross-module communication
Performance Characteristics
- Low latency: Sub-millisecond invocation overhead
- High throughput: Handles thousands of concurrent invocations
- Scalability: Horizontal scaling via Redis adapters
- Resource efficient: Rust’s zero-cost abstractions
- Async runtime: Built on Tokio for efficient I/O
Security
- WebSocket connections can be secured with TLS
- HTTP API supports CORS configuration
- External function invocations support authentication (Bearer, Basic, Custom)
- URL validation for HTTP invocations
- Private IP blocking for external calls
Next Steps
- Modules Reference - Detailed module documentation
- Protocol Reference - WebSocket message types
- Configuration Reference - YAML configuration options