dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.15k stars 9.92k forks source link

Stateful Background Services in Container Orchestration? #14583

Closed scalablecory closed 3 years ago

scalablecory commented 4 years ago

From @BrandonCookeDev on Friday, September 20, 2019 5:44:30 PM

Issue Title

Alternative to Stateful Background Services in Container Orchestration?

General

Hello, I'm the devops lead for my company. We use .NET Core and are currently running into a complication. Essentially we have code that uses a few background services in ASP.NET (using IHostedService), however it appears those services are stateful in nature. This is problematic because we wish to host this ASP.NET application in a container orchestration environment (AWS ECS).

Because of the stateful nature, we can't have three different instances of the service running, otherwise data consistency issues or deadlocking issues become introduced. The obvious alternative is to create individual microservice/APIs for these, but given our timetable we are looking for a quicker solution that still may follow a "Best Practice".

I haven't been able to find information elsewhere on others running into this issue. Would you happen to have insight for what to do here? Thank you!

(PS. I think you guys are killing it with .NET Core. Keep up the great work!)

Copied from original issue: dotnet/core#3410

scalablecory commented 4 years ago

From @carlossanlop on Saturday, September 21, 2019 12:12:13 AM

@Pilchie can you help here?

Pilchie commented 4 years ago

@anurse ?

davidfowl commented 4 years ago

@BrandonCookeDev I'm not 100% sure what's being asked here.

otherwise data consistency issues or deadlocking issues become introduced

That's extremely application specific. Can you clarify what you're looking for.

analogrelay commented 4 years ago

Yeah, this looks like an architectural discussion rather than something we can specify in our docs. I don't think there's necessarily anything stateful in IHostedService itself, though perhaps @BrandonCookeDev can clarify.

BrandonCookeDev commented 4 years ago

Hey guys, thanks for the activity!

So we were handed an ASP.NET application in NETCORE 2.2 that implemented some background services that did things such as sync database information with what was in memory, and other stateful distributed actions.

Now this is fine if one instance of the application is running, as the state the application holds would more or less be in a singleton; however, since we utilize AWS ECS for container orchestration it would be silly to force only one instance of the app to run. Yet, the issue is that if we have two or more instances of the app up at any given time they could have a different set of stateful data to be synced which introduces potential deadlocking and data consistency issues.

Obviously, the best option is to take the background service and make a FAAS (AWS Lambda, etc) and have that be triggered on a schedule outside of this ASP.NET app; yet due to time restrictions that may not be possible.

So I guess my question is for the above scenario is there any sort of ASP.NET CORE approach to solve the problem? Let me know if more information is needed. I also understand that it's quite possible that this is not solvable from the app and that the stateful piece will have to be removed altogether and shifted elsewhere, but I figured asking wouldn't hurt :)

Thanks guys

davidfowl commented 4 years ago

This isn’t an asp.net core problem. It’s a general issue with having more than one source of truth. I’m. It sure what advice you’re looking for outside of:

BrandonCookeDev commented 4 years ago

This isn’t an asp.net core problem

For reference I never said it was. I am just looking for potential solutions that coincide with ASP.NET best practices or adhere to ASP.NET design patterns.

If those don't exist that is understandable, but I figured if I didn't ask then we would never know.

davidfowl commented 4 years ago

Ah I see, there’s no data syncing primitive to handles these types of scenarios. It sounds like this app was designed with a single instance in mind.

BrandonCookeDev commented 4 years ago

Thanks for the feedback guys! I assumed this would be the case, but figured I'd ask just in case

Rick-Anderson commented 4 years ago

@anurse can this be closed?

analogrelay commented 4 years ago

Fine with me

ghost commented 3 years ago

Thank you for contacting us. Due to a lack of activity on this discussion issue we're closing it in an effort to keep our backlog clean. If you believe there is a concern related to the ASP.NET Core framework, which hasn't been addressed yet, please file a new issue.

This issue will be locked after 30 more days of inactivity. If you still wish to discuss this subject after then, please create a new issue!