You're running multiple instances of a Node.js app. Only ONE instance should actively perform a recurring background task β polling a 3rd-party API every 5 seconds.
If the leader goes down, another instance must take over within 5 seconds. Redis will be used to coordinate this.
- Implement leader election using Redis.
- Only the leader should perform the polling task.
- Automatically fail over if the leader dies.
- Implement
/status
endpoint to show:- Whether this instance is leader
- When it last polled the API
PORT=3000 npm start
PORT=3001 npm start
Watch the logs to see leadership change and polling activity.
npm install
npm start
src/index.js
- App entrypointsrc/lib/leader.js
- Leader election logic (you implement)src/tasks/poll.js
- Task runner logic (only leader runs it)src/routes/status.js
- Status endpoint
- You can mock the 3rd-party API with console.log
- Do not use a DB β everything should work with Redis and in-memory state
- Use
ioredis
orredlock
if you prefer (both supported)
Good luck!