Imagine you're building an app that needs to perform a task every 5 minutes.
Examples:
- Send reminder notifications
- Check product expiry dates
- Generate daily reports
- Sync data from third-party APIs
- Clean old records from the database
A common beginner question is:
If nobody opens my app, who will trigger these jobs every 5 minutes?
Let's break down how this works in production.
Option 1: Cron Job on a Server
The simplest solution is using a Cron Job.
A cron job is a scheduler built into Linux that can execute commands at specific intervals.
Example:
*/5 * * * * node reminderJob.js
This means:
Every 5 minutes
Run reminderJob.js
Architecture:
┌─────────────┐
│ Linux Server│
└──────┬──────┘
│
▼
Cron Scheduler
│
▼
Node.js Job
│
▼
Database
Works well for:
- Small projects
- Internal tools
- MVPs
Problem With a Single Server
What happens if the server crashes?
Server Down
↓
Cron Stops
↓
Jobs Missed
For critical applications, this is unacceptable.
Imagine:
- Payment settlement jobs
- Order processing
- OTP cleanup
- Subscription renewals
Missing even one execution can create problems.
Option 2: Dedicated Background Worker
Large systems separate API servers and background workers.
Architecture:
Users
│
▼
API Servers
│
▼
Database
▲
│
Worker Service
│
▼
Scheduled Jobs
Benefits:
- APIs remain fast
- Jobs don't affect user requests
- Easier to scale independently
Option 3: Kubernetes CronJobs
Modern cloud applications often run on Kubernetes.
Kubernetes provides a resource called CronJob.
Example:
schedule: "*/5 * * * *"
Every 5 minutes Kubernetes automatically creates a container and runs the job.
Architecture:
Kubernetes
│
▼
CronJob
│
▼
New Container
│
▼
Execute Job
Advantages:
- Automatic retries
- Auto-healing
- Easy deployment
- Cloud-native
Preventing Duplicate Execution
Suppose you have 3 servers.
Server A
Server B
Server C
If all three execute the same job:
Send Notification
Send Notification
Send Notification
Users receive the same notification three times.
Not good.
Distributed Locking
To ensure only one server executes the job, companies use distributed locks.
Popular choices:
- Redis Lock
- Database Lock
- ZooKeeper
- etcd
Flow:
Worker Starts
│
▼
Acquire Lock
│
▼
Lock Success ?
/ \
Yes No
│ │
▼ ▼
Run Job Exit
Only one worker gets the lock.
How Big Companies Do It
Companies rarely depend on a single cron job.
Typical architecture:
Scheduler
│
▼
Message Queue
│
┌───────────┼───────────┐
▼ ▼ ▼
Worker 1 Worker 2 Worker 3
│ │ │
└───────────┼───────────┘
▼
Database
Popular technologies:
- Cron
- Kubernetes CronJobs
- Redis
- RabbitMQ
- Apache Kafka
- AWS EventBridge
- Google Cloud Scheduler
The scheduler creates tasks.
Workers process those tasks.
This makes the system scalable and fault tolerant.
Example: Expiry Reminder App
Let's say you built an app that stores expiry dates.
Every 5 minutes:
- Scheduler triggers a job.
- Job finds products expiring soon.
- Notification tasks are added to a queue.
- Workers send notifications.
- Results are stored in the database.
Architecture:
Cron Scheduler
│
▼
Expiry Checker
│
▼
Database Query
│
▼
Message Queue
│
▼
Notification Workers
│
▼
Firebase / APNs
Key Takeaway
A scheduled task running every 5 minutes is usually not handled by user traffic.
Production systems use:
- Cron Jobs
- Background Workers
- Kubernetes CronJobs
- Message Queues
- Distributed Locks
The bigger the scale, the more important reliability, retries, and duplicate prevention become.
Next time you receive a notification exactly on time, remember:
Somewhere in the cloud, a scheduler woke up, executed a job, and triggered that notification.
Top comments (0)