I am trying to find handle the akka Payloadsizeexceeded exception. But since there is no way to handle it, I would like to know the size of the message that is being passed. For this I would like to calculate the size of message and this size needs to give the exact size of the clustermessage being passed not just the json size inside my request. Is there a way to know this size?
For example my json size is 31998bytes but when the message is being passed between the actors some amount of encoding is happening and the actual size of the message being passed is increased to 32778. How can I know this final message size?
When you send a message between actor systems there is an envelope around the message payload, which contains the recipient actor ref, sender actor ref, identifies the serializer to use and a few other things. A few of them contain strings (actor path, actor system name, node hostname) which will affect making it hard to say exactly how large the overhead is.
I'd recommend you to add test coverage sending messages remotely between the expected actors and then be conservative given that the hostnames will likely differ when deciding on how big payloads to accept.
Note that if you need many times larger payloads than the default 128k it is a good idea to consider splitting the payloads up into smaller messages instead or use some form of side channel to transfer the data as it will cause head of line blocking for other smaller messages such as the remoting heartbeats making remoting and cluster less stable.
With the new remoting subsystem, which is not yet stable/production ready has support for a separate channel for large messages, and additionally does compression of actor refs so that actors often communicating remotely will be identified by an index in a cache rather than a full serialized actor ref.