Backend Server/Worker Mode

Medusa has a backend architecture that typically operates in two primary modes: Server mode and Worker mode.

The Server mode is where the main application logic runs. This mode is responsible for handling incoming HTTP requests (such as API calls) and managing overall business logic (e.g., user authentication, order processing, etc.).

The Worker mode is used for offloading long-running tasks or background jobs that are not ideal to run during an HTTP request. These tasks often include time-consuming operations such as sending emails, processing payments asynchronously, updating inventory, or generating reports.

We will use PM2: A process manager that can be used to run both server and worker instances and keep them running in the background.

# Install PM2 globally (for managing processes)
npm install -g pm2

Server Mode

Now to start server mode, edit the .env file in the medusa backend folder and change parameter MEDUSA_WORKER_MODE

Note if required we need to enable admin along with server mode

Also keep default server port 9000 - Your NGINX reverse proxy will redirect here

DISABLE_MEDUSA_ADMIN=false
MEDUSA_WORKER_MODE=server
PORT=9000

Start backend in server mode

pm2 start npm --name "medusa-server" -- run start

Worker Mode

Now to start worker mode, edit the .env file in the medusa backend folder and change parameter MEDUSA_WORKER_MODE

Note for worker instance we dont need admin so disable the same. Also both server and worker instance cannot run on same port.

So here we changed the worker port to 5173

DISABLE_MEDUSA_ADMIN=true
MEDUSA_WORKER_MODE=worker
PORT=5173

Start backend in worker mode

pm2 start npm --name "medusa-worker" -- run start

You can view and manage these processes using PM2 commands

pm2 list         # Lists all running processes
pm2 logs         # View logs for both server and worker
pm2 logs medusa-server      # Server only log
pm2 restart medusa-server   # Restart the server process
pm2 restart medusa-worker   # Restart the worker process
pm2 stop medusa-worker      # Stops the worker process
pm2 kill         # kill all process

Last updated