Send same message to multiple clients

516 views
Skip to first unread message

Hristo Katsarski

unread,
Jan 30, 2022, 10:43:36 AM1/30/22
to grpc.io
Hi,

We have a bi-di streaming service, the server is in gRPC Java. 

Many of the server-side ProtoBuf messages are immutable, i.e. can be created once and sent to all client channels.  Right now new instance is created and sent to each client.

Is sending the same ProtoBuf message, concurrently,  to multiple outbound streams possible and thread-safe thing to do?


Thanks

zda...@google.com

unread,
Feb 2, 2022, 3:52:17 PM2/2/22
to grpc.io
Yes, all protobuf v3 messages and immutable in Java so it's thread-safe to be used concurrently. (Note that you should not send concurrent messages over the same stream, because that will corrupt the data that grpc is sending out.)

Hristo Katsarski

unread,
Feb 4, 2022, 12:42:55 PM2/4/22
to grpc.io
Hi,

Thank you for your reply. It is very useful, will save CPU and GC times. 

For sending, I already synchronize calling "onNext()" on the outbound stream observer, since sending may happen directly if isReady() is true(by the caller thread) or via the setOnReadyHandler() Runnable, i.e. some thread from Netty's executor service. There were different errors (on the server and clients) without synchronizing onNext().

Regards

黄滨

unread,
Jan 18, 2023, 10:13:33 AM1/18/23
to grpc.io
In async stream programming, when the same message is sent to multiple clients with ServerAsyncWriter::Write(), does it serialize the message for each client? Could we serialize the message only once?
Reply all
Reply to author
Forward
0 new messages