Background Jobs
Learn how to create async background jobs and scheduled tasks with Motia
What You'll Build
A pet management system with background jobs that handle:
- Event Step - Async job that sets feeding reminders when pets are created
- Cron Step - Scheduled job that runs daily to clean up deleted pets
Getting Started
Clone the example repository:
Install dependencies:
Start the Workbench:
Your Workbench will be available at http://localhost:3000.
Project Structure
Files like features.json and tutorial/tutorial.tsx are only for the interactive tutorial and are not part of Motia's project structure.
All code examples in this guide are available in the build-your-first-app repository.
You can follow this guide to learn how to build background jobs with Motia step by step, or you can clone the repository and dive into our Interactive Tutorial to learn by doing directly in the Workbench.

Understanding Background Jobs
Background jobs let you handle time-consuming tasks without blocking your API responses. When a user creates a pet, they get an immediate response while tasks like sending emails or processing data happen in the background.
Motia provides two types of background jobs:
- Event Steps - Triggered by events from your API endpoints
- Cron Steps - Run on a schedule (like daily cleanup tasks)
Creating Your First Event Step
Let's create a background job that sets feeding reminders when a pet is created. First, we need to emit an event from our API endpoint.
Step 1: Emit Events from API
View on GitHub:
The API endpoint now emits an event after creating a pet. The response returns immediately while the background job processes asynchronously.
Step 2: Create the Event Step
Now let's create the background job that listens for this event and sets feeding reminders.
View on GitHub:
How Event Steps Work
Event Steps have a few key differences from API Steps:
- type is set to
'event'instead of'api' - subscribes lists the events this job listens for
- handler receives the event data as the first argument
When you create a pet, the API returns immediately. The background job picks up the event and processes it asynchronously.
Testing Your Background Job
Create a pet and watch the background job execute:
Check the logs in Workbench to see both the API call and the background job execution:

You'll see:
- "Pet created" log from the API endpoint
- "Setting next feeding reminder" log from the background job
- "Next feeding reminder set" log when the job completes
Creating a Scheduled Cron Job
Now let's create a cron job that runs daily to clean up soft-deleted pets. This demonstrates how to handle scheduled maintenance tasks.
View on GitHub:
Understanding Cron Steps
Cron Steps run on a schedule defined by a cron expression:
- type is set to
'cron' - cron defines when the job runs (e.g.,
'0 2 * * *'= daily at 2 AM) - handler receives only the context (no input data like Event Steps)
Common cron patterns:
'*/5 * * * *'- Every 5 minutes'0 * * * *'- Every hour'0 0 * * *'- Daily at midnight'0 9 * * 1'- Every Monday at 9 AM
Monitoring Background Jobs
Workbench provides tools to monitor your background jobs:
Tracing
See the complete execution flow from API call to background job:

Each trace shows:
- When the API endpoint was called
- When events were emitted
- When background jobs started and completed
- Total processing time
🎉 Congratulations! You've successfully created background jobs with Motia. Your pet store now handles async tasks efficiently without blocking API responses.
What's Next?
You now have a complete backend system with API endpoints and background jobs! But there's more power in Motia when you combine everything into workflows.
In the next guide, we'll build complete workflow orchestrations that connect multiple Steps together:
- Queue-Based Job Processing - SetNextFeedingReminder triggered by pet creation, processing asynchronously without blocking API responses
- Scheduled Maintenance Tasks - Deletion Reaper running daily at 2 AM to permanently remove soft-deleted pets past their purge date
- Pet Lifecycle Orchestration - Staff-driven workflow managing pet status transitions from creation through quarantine, health checks, and adoption
- Event-Driven State Management - Centralized orchestrator ensuring consistent pet status changes with automatic progressions and staff decision points
Let's continue building by creating workflows that orchestrate your APIs and background jobs into powerful, event-driven systems.
