Spring / Spring gRPC Interview Questions
What thread-safety considerations apply to StreamObserver in gRPC?
A given StreamObserver instance's onNext/onError/onCompleted methods are not guaranteed thread-safe for concurrent invocation from multiple threads - grpc-java expects calls on one observer to be serialized (one at a time).
If multiple threads (say, worker threads reacting to events from different sources) need to write to the same response observer, the common pattern is to wrap it in a synchronized wrapper class, or route all writes through a single dedicated executor/queue so that only one thread ever calls into the observer at a given moment - otherwise messages can interleave incorrectly or corrupt internal state.
More Related questions...