11

I have a .NET core app which I'am running on AWS Elastic Container Services (ECS). - The app runs on two different instances. - Database is SQL server

The app runs the database migrations on startup, which has worked really well. But then i had to migrate a lot of data which meant that the migration took longer time. This resulted in duplicates of the data being moved.

This happens because both apps first checks the database if the migration has been executed, both finds out that it hasn't, then both starts running the migration which takes time. After it is done it adds the migration to the database.

How do people solve this?

Possible solutions I and others have thought of

  1. Start with only one instance of the app, then scale up. this would work, but then I will have to manually scale down and up for each time there is a migration. (It is possible to do it automatically, but it would take time)

  2. Wrap long running migrations in transactions and at the start set the migration as done in the database. Check if it is in the database before commiting the change. If the transaction fails, remove the migration from the database.

  3. Lock the database? EF Core lock the database during migration . Seems weird.

  4. Make the migration a part of the deployment process. This seems to be best practice, but it would mean that the Build server would need to know the Database secrets. I'am not to afraid to give it, but it would mean i would have to maintain a duplicate set.

What does people out there do? Am I missing some obvious solution?

Thanks

1
  • Did you ever come up with a resolution for this? Are you able to run the deployments in serial instead of parallel, i.e. a separate deployment step for each of them, where the first one has to finish before the next will begin? It should be okay as long as you take care how you handle deletions and renames.
    – Trevortni
    Oct 25, 2022 at 22:53

1 Answer 1

6

We also used to have our applications perform the migration, but even Microsoft recommends avoiding this in a multi-instance environment:

We recommend production apps should not call Database.Migrate at application startup. Migrate shouldn't be called from an app in server farm. For example, if the app has been cloud deployed with scale-out (multiple instances of the app are running).

Database migration should be done as part of deployment, and in a controlled way.

Like everything there are different ways to go about solving the problem. Our team is small and so we generate migration scripts through the EF CLI tooling and then run them manually as part of a deployment/maintenance routine. This could of course be automated if your process warrants it.

2
  • 2
    what about when you have 2 nodes of your API (running in a kubernetes cluster for example), and you deploy a new version which will change database schema as part of the deployment, by design, the other node will receive an error from EF stating that there're migrations to run
    – JobaDiniz
    Aug 19, 2021 at 17:53
  • How did you generate the migration scripts? Is your database public?
    – swftowu69
    Jan 6 at 18:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.