Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Spartaque07's avatar

Scaling app

Hello guys, i have one interesting question. If i have 3 instances of my application with load balancer, can be the situation when 2 instance edit some info in the database at the same time? (Race condition as i remember correctly). Anybody know about this something?

0 likes
8 replies
Cronix's avatar

Sure, just the same as if it was a single app with no load balancer and 2 requests came in at the same time altering the same record.

Spartaque07's avatar

But if we write code correctly, we will not have race condition with one application instance. But if we have 2 or more app instance that connect to 1 database, we have a risk with this. Did you have this problem when using several app instance?

Spartaque07's avatar

No means that you haven't this problem or you are using pessimistic-locking with all your db query?)

Cronix's avatar

I don't have the problem bc of of the locking

Spartaque07's avatar

Maybe pessimistic-locking must be used for shared data, that can be modify by several users at the same time i think, but i'm not sure.

Cronix's avatar

Yes, just as you said here, which I thought was what you're trying to resolve? Locking the table resolves that.

when 2 instance edit some info in the database at the same time

But whether it's 500 app servers behind a load balancer all using a common database, or a single server using a single database, the problem is the same. The problem is "2 or more requests to edit the same record in the same database at the same time." Where the requests actually come from don't matter. It could be multiple servers, or the same server.

jlrdw's avatar

If i have 3 instances of my application with load balancer

No you only have one, let the (if properly set up) load balancer do it's job, not even something to be concerned about.

Please or to participate in this conversation.