Home > Back-end >  How Multi Threading works in Distributed systems?
How Multi Threading works in Distributed systems?

Time:08-21

When mulitple instances of a java microservice are running, how multithreading works? Eg: suppose in Instance 1 some thread modified the state, how that is communicated across different instances? Or it's not possible?

CodePudding user response:

I'm assuming you are talking about separate machines, but then the question doesn't really make sense. Your state is stored in a different place (database, cache, key-value store etc.) and the other instances can read from there. If you are talking about in memory state, that is not shared with anyone else implicitly, it has to be written out somewhere. So if you have multithreading question, that only makes sense in the context of a single process.

CodePudding user response:

The key is a proper state sharing...

In general you should carefully design the state sharing even if you have a single instance with multiple threads because if you lock the access it might slow down the system from the performance standpoint. It doesnt mean that the state has to be always kept in memory only (what happens when your single instance terminates and gets restarted later again)?

When there are many instances the situation becomes even more complicates because to orchestate all these instance you might want to use tools likd kubernates that in general might take a decision to "move" one of the working instances to another server, or, to start more instances of the same server when the load increases.

So how do you manage the state in all that mess? :) Usually you should:

  • First of all define/understand the requirements: should the state be in-sync always or the system should be eventually consistent and it ok producd wise.
  • Try to keep the system stateless as much as you can at the level of code at least, leave the state sharing to the tools that can deal with that properly (databases)
  • If you have to use some kind of shared state, again don't try to implement that by yourself, use battle-tested solutions (redis, hazelcast, infibispan, etc.)
  • If you have to implement the lock - estimate the performance impact and then again, use known solutions rather than try to implement distributed lock by yourself.

CodePudding user response:

When multiple instances of a Java micro-service are running, how does multi-threading work? For example, suppose in Instance 1 some thread modified the state, how that is communicated across different instances?

It works the same as in any other application with multiple instances.

Assuming that each instance is a distinct process, and the processes don't share memory, threads in different instances don't share Java objects.

If you want state sharing between the micro-service instances, they need to implement that state sharing by doing things like:

  • externalizing the state to a (shared) database, or
  • passing the state by message passing.

Or it's not possible?

It is not possible for threads in different address spaces to directly share Java objects.

It is possible to implement a framework where a thread in one address space can use IPC to "call" methods on a Java object in another address space. But this requires a framework like RMI or CORBA or JAX-WS. And this is not strictly state sharing.

  • Related