Bi-directional Streams with Multithreads-JAVA

379 views
Skip to first unread message

nitish bhardwaj

unread,
May 27, 2021, 12:55:55 AM5/27/21
to grpc.io
Hi,

I am trying to use bi-directional streams with JAVA. Everything works as expected in a POC of bi-di stream where I have 1 client and server which reads and writes to it.

But, it starts to break when I use the same in a mature way.

UseCase: I am trying to implement gossip using grpc in Java. Whenever any server receives a request, it gossips to the other 2 servers using grpc streams. For an instance, I have 4 servers, server1, server2, server3 and server4. All servers have 1 client configured to connect to every other server. 

whenever any server receives a message, it selects the other two servers and transmits the message and the other server does the same.

Issue: As each request would be served in a new thread by grpc, I get an error 
Cancelling the stream with status Status{code=INTERNAL, description=Too many) responses, cause=null}

That's bcz every request is read and write to stream isn't sync as it might not have got a response from server stream but it has written new data to it on a new request.

What can be done to overcome this problem? 

Thanks for the support!!

nitish bhardwaj

unread,
May 27, 2021, 1:01:22 AM5/27/21
to grpc.io
NOTE: The same code works perfectly if I switch streams to normal GRPC calls. 

But, to avoid extra latency, I need to use streams.

Eric Gribkoff

unread,
May 27, 2021, 1:33:17 AM5/27/21
to nitish bhardwaj, grpc.io
Hi,

It sounds like you are trying to send multiple messages for a unary RPC, which results in the "Too many responses" error. The distinction between unary-response and server-streaming RPC here is about the semantics of the service - namely, how many responses the server can send - and not performance. Both unary and streaming RPCs use streams under the hood. It sounds like you may have other synchronization challenges with your specific service, which would up to your application and not gRPC's stream handler to enforce, but you could also consider just changing your RPC service definition to specify that the server response will be streaming.

There was also a somewhat related discussion that you might find helpful on this old question on the grpc-java repository: https://github.com/grpc/grpc-java/issues/6323

Thanks,

Eric

--
You received this message because you are subscribed to the Google Groups "grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/grpc-io/7fe537c0-ac40-48eb-913c-5c1ad8be066cn%40googlegroups.com.

nitish bhardwaj

unread,
May 27, 2021, 12:31:47 PM5/27/21
to Eric Gribkoff, grpc.io
Hi Eric,

Thanks for sharing this information. Unary calls works fine for me. But, when I switched to streams both client and server side, it starts giving that error message.

I guess bi-di streams required to be used as ping-pong. Therefore, there could be a race condition where client or server's rate of writes is more data read causing out of sync in stream headers.

Please share your thoughts.

Thanks & Regards,
Nitish

Sanjay Pujare

unread,
May 27, 2021, 1:07:06 PM5/27/21
to nitish bhardwaj, Eric Gribkoff, grpc.io
Just want to comment on the following :

> I guess bi-di streams required to be used as ping-pong. Therefore, there could be a race condition where client or server's rate of writes is more data read causing out of sync in stream headers.

Have you taken a look at https://github.com/grpc/grpc-java/tree/master/examples/src/main/java/io/grpc/examples/manualflowcontrol ? It shows that bi-di processing doesn't have to be ping pong.


nitish bhardwaj

unread,
May 28, 2021, 7:09:03 AM5/28/21
to grpc.io
Hi Sanjay,

Thanks for sharing this. I will try using backpressure.

Regards,
Nitish

Reply all
Reply to author
Forward
0 new messages