I’ve been working with web applications for quite some time now. However, I’ve never worked on web applications that are hosted as multiple instances and are load balanced.
My question: How do web applications manage writes across several instances of same application?
For eg: a ticket booking site. Imagine I block seats in a certain row for a movie and submit my request. At the same time another user (served by other instance by virtue of application being clustered and load balanced) also blocked one of the seats that I have selected, how do web applications manage this scenario? Since, they are different processes running on different machines where does locking happen? How do they maintain cache consistency? Are there any solutions readily available?
Well, you still get the same issue with concurrent writes in a single-node application, it’s just easier to manage them since it’s one JVM.
Database, being single point shared by all instances, is the easiest goal. Depending on your expected load and use cases, optimistic locking is very easy to achieve (example in JPA). Combined with database transactions, you achieve certain level of atomicity without trading performance.
Caching is hard, especially in distributed environment. For instance ehcache can communicate between instances and once cache changes in one instance, it broadcasts this event to other nodes. There are plenty of other products like terracotta, hazelcast, etc.