I lead a small team working on a single ASP.NET Core web API. The API is deployed to a Kubernetes cluster as a load-balanced service, and can have many replicas active at any given time.
A new feature requires that we schedule tasks to be run in the background at set intervals. These usually involve running slow queries and caching the results until the next scheduled run. The best way to do this appears to be via hosted services, but I don't want multiple instances executing the same task concurrently.
How do you implement IHostedService (or some other background task manager) in a load balancer setting? My first instinct is to use pessimistic concurrency (locks, etc.), but is there a more elegant way to approach this? Since we're a single small team, I'd really prefer to avoid building and maintaining a separate project just for this.