Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scheduler in a java spring boot microservice

We have a microservice written using Spring boot which has its own NoSQL datastore. We are working on functionality whereby we want to delete some old data (in magnitude of 0.5 million documents) and want to do it on a regular basis(once a day) based on presence of records of particular type in data store.

Is having a scheduler which runs once everyday and does the deletion, a correct approach for it ? Also since its a microservice and several instances of it will be running, how do we control that this scheduler runs on only 1 instance ?

like image 918
user762421 Avatar asked Aug 05 '26 15:08

user762421


1 Answers

There are multiple options I can think of now:

  1. If there is a single instance of micro-service deployed, you can use something like quartz to time the job.
  2. Create a RESTful API for cleanup, invoke it using a script, please refer to https://stackoverflow.com/a/15090893/2817980 for example. This will make sure that only one instance of the service works on cleanup.
  3. If there is a master-slave replica, ask the master to allocate to only 1 instance
  4. Create a scheduled job using something like quartz and then check if the job already taken up by some other scheduler in zookeeper/redis/db or any other storage.

I can discuss more on this.

like image 94
Shashank Avatar answered Aug 07 '26 05:08

Shashank



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!