Web requests should be fast. If you need image processing, exports, notifications, or cleanup tasks, do them outside the request lifecycle using cron and CLI workers.
1. Keep the web request thin
The request should validate, store intent, and exit. Heavy processing happens later.
2. Use locking to avoid duplicate runs
Overlapping jobs cause double emails, corrupted exports, and race conditions. Use a simple file lock.
3. Log output and errors
Send stdout/stderr to a log file and keep logs private. If you can’t debug it, you can’t operate it.
4. Idempotency
Design jobs so repeating them doesn’t corrupt state. Store “processed” markers and use unique keys to prevent duplicate side effects.
5. Don’t run CLI code via the browser
CLI workers should be executed by the OS scheduler, not publicly accessible endpoints.
A disciplined job system is one of the most effective ways to increase performance and reliability without changing your core application.