If speed makes sense, you should make the call in the same thread. You won’t be able to do it as fast as using the network.
However, assuming speed is not that important, you can make Java RMI calls in about 500 microseconds and use a custom RPC code that you can make loopback calls in about 24 microseconds. Even transferring data between threads in the same JVM can take 8 microseconds.
You need to decide how much time you are willing to allow to place a network call. You also need to decide whether the time to start the call is important or the time to return the result. (Often the latter has double overhead)
Note: I say microsecond here, not milliseconds. I would ignore any options that take a few milliseconds for your purposes.
source share