Scaling an Entity Framework application / Multiple applications that fall into the same database?

I have an application that has been programmed using MVC / EF Code First. It does most of the server-side processing and is quite resource intensive.

I know how to set up load balancing, but I want to know how scalable an EF application is as simple as preparing a new server, deploying an application and pointing to a database cluster - or are there any problems that I will have to deal with in several EF applications that hit the same database server?

I can't find any tips / guides for this, and I'm worried that I made the wrong choice by choosing EF over something simpler / more direct!

+4
source share
1 answer

... problems ... regarding multiple EF applications that hit the same database server?

Rewind a little that your application is an application based on ASP.NET MVC. Having multiple instances of this is likely to raise the ghost of public administration.

MSDN has a pretty good idea of why this is a problem :

HTTP is a stateless protocol. This means that the web server processes each HTTP request for the page as an independent request. The server does not know the values ​​of the variables that were used during previous queries. Session State ASP.NET identifies requests from the same browser for a limited time in the form of a session and provides the ability to store variable values ​​throughout the session. By default, ASP.NET session state is enabled for all ASP.NET applications.

Alternatives to session state include the following:

  • The state of the application that stores the variables that all ASP.NET application users can access.

This point is an extremely common way to store state, but it breaks when there are multiple instances of the application (the state is "visible" to only one of the instances).

This is usually done using the value StateServer or SQLServer SessionStateMode . The same article provides a pretty good summary of each option (emphasis mine).

  • StateServer , which saves session state in a separate process called the ASP.NET public service. This ensures that the session state is preserved if the web application is restarted , and also makes the session state available for multiple web servers in the web farm .

  • SQLServer mode saves session state in a SQL Server database. This ensures that the session state is preserved if the web application is restarted , and also makes the session state available for multiple web servers in the web farm .

If your application has no status, this is a point of contention.

I'm worried I made the wrong choice by choosing EF

As for problems with multiple instances of your application accessing the database, you will have problems with any data access technology.

Here's the main scenario: let's say your application sends welcome letters to users on a schedule.

Given the Users table:

 UserId | Email | WelcomeLetterSent -------+-----------------+------------------ 1 | user@domain.com | 0 

And some kind of pseudo code:

 foreach (var user in _context.Users.Where(u => !u.WelcomeLetterSent)) { SendEmailForUser(user); user.WelcomeLetterSent = true; } _context.SaveChanges(); 

There is a race condition where both instances of one and the second instance of your application can evaluate _context.Users.Where(...) at the same time, before either of them can set WelcomeLetterSent = true and call SaveChanges . In this case, two welcome letters can be sent to each user, and not to one.

Concurrency can be an insidious thing. There's a concurrency management guide here with the Entity Framework above here , but that's just the tip of the iceberg.

The answer to your question? It depends on what your application does :)

In addition, I ideally want to create some “additional” support applications that connect to the same database ... and I'm just not sure how EF will handle multiple applications in the same database ....

If your application can tolerate multiple instances of the very access to the same database, then this usually does not stretch these "supporting applications" to play well. This is not much different from whether concurrency exists from multiple instances of the same application or multiple applications with one instance each.

+3
source

Source: https://habr.com/ru/post/1488947/


All Articles