Socket based application - Performance Issues - Suggestions Needed

Hi All,

We have an application which basically has been developed using core java. Here is a high level information about the application:

a) It opens a serversocket which allows clients to connect to it.

b) For every new client connection, a separate thread is created and this thread deals with requests from clients, processing the data and replying back to clients.

c) Each socket is polled continuously and sockettimeout is 2 seconds. If there is a timeout, we handle the situation and socket is again read. So basically sockets is read every 2 seconds. If number of timeouts reaches a configurable value, we close the connection and thread is dropped as well.

d) In production, three instances of this application are running with the help of a cisco load balancer. It is there for last 5 years.

However there has always been some minor performance isssues and we have sorted them out using different types of garbage collectors, by introducing hardware load balancers, upgrading the code for new Java versions. It is currently running on 1.4.2.

However there has always been some performance issues and today while googling over internet I came across following on the bea website which says that core java sockets are not as efficients as native API. BEA has implemented its own APIs for weblogic. My queries are:

a) Are there any better Java Socket/network API (for solairs, I know Java is plateform independenet but there could be lib which also using native libs) which are much more efficient than Core Java.

b) We are getting the InputStream/OutputStream and creating objects of DataInputStream/DataOutputStream to read the data 'Byte-By-Byte'. Each byte can have different information thats why it is required. Are there any better way of getting info than what we are currently doing.

c) As I mentioned, we are continously polling the socket for read operation with a timeout value of 2 seconds. What is the better among the following from performance point of view: (1) Frequent read operation with a lesser timeout value or (2) Less Frequent read operations with larger timeout value. (3) Any better idea?

Please suggest few things or pointers which I could do to improve the performance of the applcations. Many thanks.

Thanks,Akhil

From BEA website:-

"Although the pure-Java implementation of socket reader threads is a reliable and portable method of peer-to-peer communication, it does not provide the best performance for heavy-duty socket usage in a WebLogic Server cluster. With pure-Java socket readers, threads must actively poll all opened sockets to determine if they contain data to read. In other words, socket reader threads are always "busy" polling sockets, even if the sockets have no data to read. This unnecessary overhead can reduce performance."

[2878 byte] By [akhilnagpala] at [2007-11-15]
# 1

You could look at nio.

You could write your own socket API.

Why are you polling the socket rather than blocking?

If you are reading objects (DataInput/OutputStream) then serialization imparts overhead. Using a custom format can reduce that. That depends on the specifics of the data transfer however.

jschella at 2007-7-12 > top of java,Core,Core APIs...
# 2

My recommendations:

- Always use a BufferedInputStream and BufferedOutputStream around the socket streams

- Increase the socket send and receive buffers to at least 32k if you are on a Windows platform where the default is a ridiculous 8k, which hasn't been enough for about 15 years.

- Your 2-second timeout is far too short. Increase it to at least 10 seconds.

- Your strategy of counting up to N short timeouts of S seconds each is completely pointless. Change it to one single timeout of N*S seconds. There is nothing to be gained by the complication you have introduced to this.

ejpa at 2007-7-12 > top of java,Core,Core APIs...