We have microservices running inside Azure Service fabric and we have a background task that needs to run once every 15 minutes to do some DB related operations. I see 2 options.

  1. Use the StatelessService.RunAsync method and implement the background task under one of the relavant microservices.

  2. Implement the logic using HostedService and do services.AddHostedService inside ConfigureServices method in startup.cs of the relevant microservice.

What's the difference and which way would be preferred?

Thanks

1

There are 1 best solutions below

2
On

Options:

  • Reliable actor timers
  • Reliable actor reminders
  • .NET Hosted services: If you have a multi node cluster and you implement a hosted service it will run on each node where the Service is running. So only do this if you are okay with multiple invocations.
  • Third party library for job sheduling like Hangfire, Quartz.Net, Jobbr, ... (These might have issues with the service fabric runtime)

Have a look at this blog for reliable actors with reminders