Medusa v2 Deployment
Before writing code
Fetch live docs:
- Web-search
site:docs.medusajs.com deployment production for production deployment guides
- Web-search
site:docs.medusajs.com medusa build for build process details
- Fetch
https://docs.medusajs.com/learn/fundamentals/cli for CLI command reference
- Web-search
site:docs.medusajs.com environment variables configuration for env var reference
- Web-search
site:docs.medusajs.com redis cache events for Redis caching and event bus setup
Build Process
Build Commands
| Command |
Purpose |
npx medusa build |
Compile server + admin dashboard for production |
npx medusa db:migrate |
Run pending database migrations |
npx medusa start |
Start the production server |
npx medusa worker |
Start the background worker process |
The build step compiles TypeScript, bundles admin extensions (Vite), and prepares the .medusa/ output directory.
Build Output Structure
.medusa/
├── server/ — Compiled server code
│ ├── src/ — Custom modules, routes, workflows
│ └── medusa-config.js
└── admin/ — Bundled admin dashboard (static)
Server vs Worker Mode
Medusa v2 supports running the server and background workers as separate processes:
| Mode |
Command |
Handles |
| Server |
npx medusa start |
HTTP requests, API routes, admin dashboard |
| Worker |
npx medusa worker |
Workflows, scheduled jobs, event subscribers |
| Combined |
Default start behavior |
Both server and worker (single process) |
When to Separate
- Development — combined mode is fine
- Production — separate for reliability and independent scaling
- High traffic — scale server instances independently from workers
- Worker mode requires Redis for job queue communication between processes
Environment Variables
Required Variables
| Variable |
Purpose |
Example |
DATABASE_URL |
PostgreSQL connection string |
postgres://user:pass@host:5432/medusa |
COOKIE_SECRET |
Session cookie signing |
Random 32+ character string |
JWT_SECRET |
JWT token signing |
Random 32+ character string |
NODE_ENV |
Runtime environment |
production |
Optional but Recommended
| Variable |
Purpose |
Default |
REDIS_URL |
Redis connection for cache/events/workers |
None (in-memory) |
STORE_CORS |
Store API CORS origins |
http://localhost:8000 |
ADMIN_CORS |
Admin API CORS origins |
http://localhost:9000 |
AUTH_CORS |
Auth route CORS origins |
Combination of store + admin |
PORT |
Server listen port |
9000 |
MEDUSA_ADMIN_ONBOARDING_TYPE |
Admin onboarding flow |
default |
MEDUSA_WORKER_MODE |
server, worker, or shared |
shared |
Database Configuration
PostgreSQL Requirements
- PostgreSQL required (see official docs for version compatibility)
- Enable SSL in production: append
?sslmode=require to DATABASE_URL
- Connection pooling recommended for high-traffic deployments
Migration Workflow
# Generate migration after DML model changes
npx medusa db:generate <module_name>
# Apply migrations
npx medusa db:migrate
- Always run migrations before starting the new server version
- Back up the database before running migrations in production
- Test migrations against a staging database first
Redis Configuration
Redis serves three roles in production Medusa:
| Role |
Purpose |
Required? |
| Event Bus |
Pub/sub for event-driven subscribers |
Recommended |
| Cache |
Module data caching layer |
Recommended |
| Worker Queue |
Job queue for background workflows |
Required for worker mode |
Configure in medusa-config.ts by registering the Redis modules:
// Fetch live docs for Redis module registration
// in medusa-config.ts modules array
- Use separate Redis databases (db index) for cache vs events vs queues
- Set appropriate
maxmemory and eviction policies for cache
- Monitor Redis memory usage and connection count in production
Hosting Options
| Platform |
Type |
Notes |
| Railway |
PaaS |
One-click deploy, managed PostgreSQL and Redis |
| DigitalOcean App Platform |
PaaS |
Managed infrastructure, auto-scaling |
| AWS (EC2/ECS/Fargate) |
IaaS/CaaS |
Full control, use with RDS and ElastiCache |
| Google Cloud Run |
Serverless containers |
Auto-scaling, pay-per-use |
| Render |
PaaS |
Simple deploy, managed databases |
| Self-hosted (Docker) |
Container |
Full control, use Docker Compose or Kubernetes |
| Vercel |
Serverless |
Admin/storefront hosting only (not the Medusa server) |
Docker Deployment
# Fetch live docs for official Medusa Dockerfile
# and docker-compose.yml patterns
A typical Docker Compose setup includes three services: Medusa server, PostgreSQL, and Redis.
Production Checklist
Pre-Deploy
Infrastructure
Post-Deploy
Scaling Strategies
Horizontal Scaling
- Run multiple server instances behind a load balancer
- Use Redis-backed sessions for sticky-session-free scaling
- Run multiple worker instances for parallel job processing
Vertical Scaling
- Increase PostgreSQL connection pool size for heavier workloads
- Allocate more memory to Redis for larger cache datasets
- Use
--max-old-space-size for Node.js memory limits
Best Practices
- Separate server and worker — run as independent processes in production; scale each based on demand; use Redis as the communication backbone
- Environment variable hygiene — never commit secrets to source control; use platform-native secret management (AWS Secrets Manager, Railway variables, etc.); rotate secrets periodically
- Database management — always test migrations on staging first; automate backups; use connection pooling (PgBouncer) for high concurrency
- Monitoring — track API response times, worker queue depth, database connection count, and Redis memory; set alerts for anomalies
Fetch the Medusa deployment documentation for exact build flags, Docker configuration, and platform-specific deployment guides before deploying.
1---2name: medusa-deploy3description: Deploy Medusa v2 to production — build process, server vs worker mode, environment variables, hosting options, Redis caching, database configuration, and production checklist. Use when deploying Medusa applications.4---56# Medusa v2 Deployment78## Before writing code910**Fetch live docs**:111. Web-search `site:docs.medusajs.com deployment production` for production deployment guides122. Web-search `site:docs.medusajs.com medusa build` for build process details133. Fetch `https://docs.medusajs.com/learn/fundamentals/cli` for CLI command reference144. Web-search `site:docs.medusajs.com environment variables configuration` for env var reference155. Web-search `site:docs.medusajs.com redis cache events` for Redis caching and event bus setup1617## Build Process1819### Build Commands2021| Command | Purpose |22|---------|---------|23| `npx medusa build` | Compile server + admin dashboard for production |24| `npx medusa db:migrate` | Run pending database migrations |25| `npx medusa start` | Start the production server |26| `npx medusa worker` | Start the background worker process |2728The build step compiles TypeScript, bundles admin extensions (Vite), and prepares the `.medusa/` output directory.2930### Build Output Structure3132```33.medusa/34├── server/ — Compiled server code35│ ├── src/ — Custom modules, routes, workflows36│ └── medusa-config.js37└── admin/ — Bundled admin dashboard (static)38```3940## Server vs Worker Mode4142Medusa v2 supports running the server and background workers as separate processes:4344| Mode | Command | Handles |45|------|---------|---------|46| **Server** | `npx medusa start` | HTTP requests, API routes, admin dashboard |47| **Worker** | `npx medusa worker` | Workflows, scheduled jobs, event subscribers |48| **Combined** | Default `start` behavior | Both server and worker (single process) |4950### When to Separate5152- **Development** — combined mode is fine53- **Production** — separate for reliability and independent scaling54- **High traffic** — scale server instances independently from workers55- Worker mode requires Redis for job queue communication between processes5657## Environment Variables5859### Required Variables6061| Variable | Purpose | Example |62|----------|---------|---------|63| `DATABASE_URL` | PostgreSQL connection string | `postgres://user:pass@host:5432/medusa` |64| `COOKIE_SECRET` | Session cookie signing | Random 32+ character string |65| `JWT_SECRET` | JWT token signing | Random 32+ character string |66| `NODE_ENV` | Runtime environment | `production` |6768### Optional but Recommended6970| Variable | Purpose | Default |71|----------|---------|---------|72| `REDIS_URL` | Redis connection for cache/events/workers | None (in-memory) |73| `STORE_CORS` | Store API CORS origins | `http://localhost:8000` |74| `ADMIN_CORS` | Admin API CORS origins | `http://localhost:9000` |75| `AUTH_CORS` | Auth route CORS origins | Combination of store + admin |76| `PORT` | Server listen port | `9000` |77| `MEDUSA_ADMIN_ONBOARDING_TYPE` | Admin onboarding flow | `default` |78| `MEDUSA_WORKER_MODE` | `server`, `worker`, or `shared` | `shared` |7980## Database Configuration8182### PostgreSQL Requirements8384- PostgreSQL required (see official docs for version compatibility)85- Enable SSL in production: append `?sslmode=require` to `DATABASE_URL`86- Connection pooling recommended for high-traffic deployments8788### Migration Workflow8990```bash91# Generate migration after DML model changes92npx medusa db:generate <module_name>93# Apply migrations94npx medusa db:migrate95```9697- Always run migrations before starting the new server version98- Back up the database before running migrations in production99- Test migrations against a staging database first100101## Redis Configuration102103Redis serves three roles in production Medusa:104105| Role | Purpose | Required? |106|------|---------|-----------|107| **Event Bus** | Pub/sub for event-driven subscribers | Recommended |108| **Cache** | Module data caching layer | Recommended |109| **Worker Queue** | Job queue for background workflows | Required for worker mode |110111Configure in `medusa-config.ts` by registering the Redis modules:112113```ts114// Fetch live docs for Redis module registration115// in medusa-config.ts modules array116```117118- Use separate Redis databases (db index) for cache vs events vs queues119- Set appropriate `maxmemory` and eviction policies for cache120- Monitor Redis memory usage and connection count in production121122## Hosting Options123124| Platform | Type | Notes |125|----------|------|-------|126| **Railway** | PaaS | One-click deploy, managed PostgreSQL and Redis |127| **DigitalOcean App Platform** | PaaS | Managed infrastructure, auto-scaling |128| **AWS (EC2/ECS/Fargate)** | IaaS/CaaS | Full control, use with RDS and ElastiCache |129| **Google Cloud Run** | Serverless containers | Auto-scaling, pay-per-use |130| **Render** | PaaS | Simple deploy, managed databases |131| **Self-hosted (Docker)** | Container | Full control, use Docker Compose or Kubernetes |132| **Vercel** | Serverless | Admin/storefront hosting only (not the Medusa server) |133134### Docker Deployment135136```dockerfile137# Fetch live docs for official Medusa Dockerfile138# and docker-compose.yml patterns139```140141A typical Docker Compose setup includes three services: Medusa server, PostgreSQL, and Redis.142143## Production Checklist144145### Pre-Deploy146147- [ ] Set `NODE_ENV=production`148- [ ] Configure unique `COOKIE_SECRET` and `JWT_SECRET`149- [ ] Set `DATABASE_URL` with SSL mode enabled150- [ ] Configure `REDIS_URL` for events, cache, and worker queue151- [ ] Set CORS variables (`STORE_CORS`, `ADMIN_CORS`, `AUTH_CORS`)152- [ ] Run `npx medusa build` successfully153- [ ] Run `npx medusa db:migrate` against production database154155### Infrastructure156157- [ ] PostgreSQL with automated backups and point-in-time recovery158- [ ] Redis with persistence enabled (RDB or AOF)159- [ ] HTTPS termination (TLS certificate) via reverse proxy or load balancer160- [ ] Health check endpoint configured for load balancer161- [ ] Log aggregation (stdout/stderr to centralized logging)162163### Post-Deploy164165- [ ] Verify admin dashboard loads and login works166- [ ] Verify store API responds with publishable key167- [ ] Confirm background workers are processing jobs168- [ ] Test payment provider webhooks reach the server169- [ ] Monitor error rates and response times170171## Scaling Strategies172173### Horizontal Scaling174175- Run multiple server instances behind a load balancer176- Use Redis-backed sessions for sticky-session-free scaling177- Run multiple worker instances for parallel job processing178179### Vertical Scaling180181- Increase PostgreSQL connection pool size for heavier workloads182- Allocate more memory to Redis for larger cache datasets183- Use `--max-old-space-size` for Node.js memory limits184185## Best Practices186187- **Separate server and worker** — run as independent processes in production; scale each based on demand; use Redis as the communication backbone188- **Environment variable hygiene** — never commit secrets to source control; use platform-native secret management (AWS Secrets Manager, Railway variables, etc.); rotate secrets periodically189- **Database management** — always test migrations on staging first; automate backups; use connection pooling (PgBouncer) for high concurrency190- **Monitoring** — track API response times, worker queue depth, database connection count, and Redis memory; set alerts for anomalies191192Fetch the Medusa deployment documentation for exact build flags, Docker configuration, and platform-specific deployment guides before deploying.