dotnet / docs

This repository contains .NET Documentation.
https://learn.microsoft.com/dotnet
Creative Commons Attribution 4.0 International
4.23k stars 5.87k forks source link

No mention of support for distributed/load-balanced systems? #23440

Open wizofaus opened 3 years ago

wizofaus commented 3 years ago

The only reason I've never seriously looked into using IHostedService for background tasks is because it's never been clear how it might work in load-balanced web-servers where you might have 4 or 5 machines all running the same service behind a load-balancer. Could this be explained here?


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

sughosneo commented 3 years ago

Hi @wizofaus, thank you for your feedback.

With the microservices architecture in place, you can implement the background task in a separate process/container to scale it up/down based on your need. You don't need to have a load-balancer for the background services as it's not going to process any incoming request. You can rely on pub/sub mechanism to coordinate among different service instances, in case if you want to scale your background tasks. To refer similar implementation, see the Ordering.BackgroundTasks in eShopOnContainers

wizofaus commented 3 years ago

Thanks for that, so I've looked at the eShopOnContainershttps://github.com/dotnet-architecture/eShopOnContainers sample, and frankly it's not clear to me how there's any load-balancing for GracePeriodManagerService, given it's basically just a polling loop that keeps checking the DB, and in fact the way it's written it's not load-balancer safe at all - assuming Azure had fired up multiple instances to run your web server, and hence there were multiple GracePeriodManagerService background tasks running, they'd all find the same orders and publish multiple events for them. Which is exactly why I've been concerned about using BackgroundService. [https://avatars.githubusercontent.com/u/27453006?s=400&v=4]https://github.com/dotnet-architecture/eShopOnContainers dotnet-architecture/eShopOnContainers: Cross-platform .NET sample microservices and container based application that runs on Linux Windows and macOS. Powered by .NET 5, Docker Containers and Azure Kubernetes Services. Supports Visual Studio, VS for Mac and CLI based environments with Docker CLI, dotnet CLI, VS Code or any other code editor. - GitHubhttps://github.com/dotnet-architecture/eShopOnContainers Dev branch contains the latest beta code and their images are tagged with :linux-dev in our Docker Hub. Getting Started. Make sure you have installed and configured docker in your environment. After that, you can run the below commands from the /src/ directory and get started with the eShopOnContainers immediately. github.com


From: Sumit Ghosh @.> Sent: Tuesday, 6 April 2021 5:13 PM To: dotnet/docs @.> Cc: wizofaus @.>; Mention @.> Subject: Re: [dotnet/docs] No mention of support for distributed/load-balanced systems? (#23440)

Hi @wizofaushttps://github.com/wizofaus, thank you for your feedback.

With the microservices architecture in place, you can implement the background task in a separate process/container to scale it up/down based on your need. You don't need to have a load-balancer for the background services as it's not going to process any incoming request. You can rely on pub/sub mechanism to coordinate among different service instances, in case if you want to scale your background tasks. To refer similar implementation, see the Ordering.BackgroundTaskshttps://github.com/dotnet-architecture/eShopOnContainers/tree/dev/src/Services/Ordering/Ordering.BackgroundTasks in eShopOnContainershttps://github.com/dotnet-architecture/eShopOnContainers

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/dotnet/docs/issues/23440#issuecomment-813885210, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ABI5UAKR6KDGRPTBH4XLTL3THKYDJANCNFSM4ZTZ5LXA.

thaxy commented 3 years ago

@wizofaus @sughosneo @tdykstra this makes sense but could we add a small paragraph with an example how exactly this could be achieved? How do I configure my .NET Core App to only start the hosted background service and under other cirumcances the Controllers or other services?

With Docker this could be done via CMD or Entrypoint but how to make this play nicely together with our application? These two points would have to be covered:

It would be awesome if you could point me out some resources here. As my first google attempts don't lead me to acceptable solutions.

Update: It seems that the demo project just creates separate projects: https://github.com/dotnet-architecture/eShopOnContainers/tree/d0cd2830a864a8b975341b02b93a0887a3908c04/src/Services/Ordering

Is this really recommended?