Serialize GRPC stub objects to cache in redis

23 views
Skip to first unread message

george...@gmail.com

unread,
May 6, 2020, 3:06:11 PM5/6/20
to grpc.io
Hello,

I would like to cache a GRPC stub into redis. Is this possible? When I try I get serialzation errors. I've tried adding serialization methods onto the generated stub classes but still get serialization errors. I am using java but open to using other implementations.

Thanks in advance for the insight!

Here is my serialization code. 

ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 9002)
.usePlaintext()
.build();
MyGrpc.MyStub myStub = MyGrpc.newStub(channel);
StreamObserver<MessageResponse> responseObserver = new ServerCallStreamObserver<MessageResponse>() {
...
};
myStub.doubleStream(responseObserver);

ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bos);
out.writeObject(myStub);
out.flush();
byte[] value = bos.toByteArray();
bos.close();

Jedis jedis = new Jedis("localhost", 6379);
byte[] key = "key".getBytes();
jedis.setex(key, 10, value);

byte[] bytes = jedis.get(key);
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
ObjectInput in = new ObjectInputStream(bis);
MyGrpc.myStub cachedStub = (MyGrpc.MyStub) in.readObject();

When I try to deserialize the object I get the following error.

Exception in thread "main" java.io.NotSerializableException: ai.com.proxy.generated.MyGrpc$MyStub
	at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
	at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
	at ai.com.proxy.ProxyApplication.main(ProxyApplication.java:83)

Christian Rivasseau

unread,
May 6, 2020, 3:16:19 PM5/6/20
to george...@gmail.com, grpc.io
It doesnt really make sense to cache a stub in redis, just like it wouldn't make sense to serialize a socket,
or a thread pool. Those kind of objects only have an use in a given process.

--
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/26c472dd-8066-4477-9166-7c1e3fccb6f0%40googlegroups.com.


--
Christian Rivasseau
Co-founder and CTO @ Lefty
+33 6 67 35 26 74

george...@gmail.com

unread,
May 6, 2020, 3:25:02 PM5/6/20
to grpc.io
I think I see where you're coming from. Can we assume my paradigm is slightly different and I have a set of servers servicing streaming requests and I want to establish session affinity to those servers when a request is made to connect. Is this possible? 

In my current implementation the server handling the request is loading all of the necessary data into memory and I'd like to have that same server handle requests on subsequent calls.
To unsubscribe from this group and stop receiving emails from it, send an email to grp...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages