Using Adapters
How to configure and use adapters in your Motia application
Adapters in Motia provide a pluggable infrastructure layer that enables horizontal scaling and custom implementations. They abstract the underlying infrastructure components (state storage, event handling, streams, and cron locking) so you can swap implementations without changing your application code.
Adapter Types
Motia supports four main adapter types:
- Streams: Real-time data streams for live updates
- State: Persistent key-value storage shared across steps
- Events: Event-based communication between steps
- Cron: Distributed cron job locking to prevent duplicate executions
Configuration
Adapters are configured in your motia.config.ts file. If no adapters are specified, Motia uses default implementations suitable for single-instance deployments.
Default Adapters (No Configuration)
Custom Adapters
Usage in Code
Stream Adapter
Streams are accessed via context.streams in your step handlers. Each stream is defined by a .stream.ts file and accessed by its name.
Defining a Stream:
Using a Stream:
State Adapter
State is accessed via context.state and provides persistent storage scoped to a trace ID.
Event Adapter
Events are used to trigger other steps. Use context.emit() to send events, and configure steps with subscribes to listen for events.
Emitting Events:
Subscribing to Events:
Cron Adapter
Cron adapters are automatically used by Motia when cron steps run. They provide distributed locking to ensure only one instance executes a cron job, even when running multiple Motia instances.
Defining a Cron Step:
The cron adapter automatically:
- Acquires a lock before execution
- Releases the lock after completion
- Prevents duplicate executions across multiple instances
No additional code is needed to use the cron adapter - it works automatically when you configure a cron step.
Available Implementations
Official Packages
@motiadev/adapter-redis-state- Redis-based state storage@motiadev/adapter-redis-streams- Redis streams implementation@motiadev/adapter-redis-cron- Redis distributed locking for cron@motiadev/adapter-rabbitmq-events- RabbitMQ event handling
Default Implementations
These are included in @motiadev/core and work without external dependencies:
FileStateAdapter- File-based state (default)MemoryStateAdapter- In-memory state (testing)FileStreamAdapter- File-based streams (default)MemoryStreamAdapter- In-memory streams (testing)InMemoryQueueEventAdapter- In-memory event queue (default)InMemoryCronAdapter- Single-instance cron locking (default)
When to Use Custom Adapters
Use default adapters when:
- Running a single Motia instance
- Local development
- Testing
Use distributed adapters (Redis, RabbitMQ) when:
- Running multiple Motia instances
- Need horizontal scaling
- Production deployments
- High availability requirements
What's Next?
🔧 Creating Adapters
Learn how to create custom adapters
📦 State Management
Learn more about state management