Suppose I have a Java Spring application having a synchronized method (thread-safe). If my application is deployed on a single instance, then I can say that it's thread safe. However, to scale, we must deploy on multiple instances. How to achieve inter-instance synchronization?
-
3There are many ways. This question is not specific enough. Bottom line, there needs to be some communication/coordination between the processes. – Michael May 14 '21 at 13:03
1 Answers
The question doesn't seem to be about thread-safety. A single application can have multiple threads, and certain methods in an application can be thread-safe or -unsafe.
With that, if you have multiple instances of a given application that needs access to communicate, then its as @Michael in the comments said: it is a form of inter-process communication (IPC). There are two ways to achieve this: message passing and shared memory. Some examples of this can be found at this answer: How to have 2 JVMs talk to one another
However, since you are using Java Spring, a web framework, you are likely already working with network based services. Why not have multiple instances of the same service share state through an external shared service? This sounds like a great use-case for Message Brokers like Redis or RabbitMQ. As Reddis states as their homepage states:
Chat, messaging, and queues
Redis supports Pub/Sub with pattern matching and a variety of data structures such as lists, sorted sets, and hashes. This allows Redis to support high performance chat rooms, real-time comment streams, social media feeds and server intercommunication. The Redis List data structure makes it easy to implement a lightweight queue. Lists offer atomic operations as well as blocking capabilities, making them suitable for a variety of applications that require a reliable message broker or a circular list.

- 100,966
- 191
- 140
- 197

- 1,074
- 1
- 9
- 17