1

I have a Spring Boot app which has a scheduler that insert data to a remote database at 2 a.m. every day.

@Scheduled(cron = "0 0 2 * * ?")
public void reportDataToDB() {
    // code omitted
}

The problem is, the app runs on multiple machines, so the database would receive multiple duplicate insertions of data.

What is the idiomatic way to solve this?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • 1
    There is no simple one recipe for multiple service work coordination, recenly I came across an article covering the most approaches to your issue: https://blog.hazelcast.com/distributed-task-coordination-with-hazelcast/ I am trying to solve the same issue you are, and thinking abount using hazelcast for miltiple note work coordination – Artyom Rebrov Oct 08 '18 at 07:37
  • https://stackoverflow.com/questions/31288810/spring-scheduled-task-running-in-clustered-environment – xingbin Oct 08 '18 at 08:16

1 Answers1

0

We solved such a problem by using a central scheduler. In our case we use Rundeck, which then calls a URL on our service (by going through the loadbalancer), which then executes the task (in our case data cleanup). This way you can make sure, that the logic is only executed on one instance of the service.

dunni
  • 43,386
  • 10
  • 104
  • 99