Tuesday, 15 July 2014

Java locking on muliple servers -



Java locking on muliple servers -

i have few seats reserved user. @ time, 1 user can participate in reservation process same seat not reserved multiple users. in java code have used "synchronized" keyword done. works.

however, code deployed on 2 servers, s1 , s2. hypothetically, lets there lastly seat available. 2 users, u1 , u2, want reserve lastly seat. might happen load balancer send user u1 server s1 , user u2 server s2. "local" synchronized still work same way not able block other user while making reservation. create lastly seat reserved both users problem.

so question how can create sure multiple users reserve seat without conflict in multiple server environment?

there 4 ways distributed systems can communicate:

shared database. remote procedure calls messaging. shared file-system.

consider shared database approach. . if don't need massive scaling, easiest utilize relational database since of them provide grade of acid (atomocity, consistency, isolation & durability). . .

you can set transaction, , rollback/handle in case of collision - called optimistic locking strategy.

if utilize spring + jpa/hibernate of hard work has been done , need add together @transactional annotation update method. . . great book on spring "spring in action" .

another alternative utilize distributed cache.

java locking synchronized

No comments:

Post a Comment