« Way To Go Chris | Main | Down Goes SavaJe »

Java IP Caching

There was an interesting piece in a recent Java Lobby Newsletter on Java IP Caching. The basic story goes something like this. Two servers each running a Java Process are communicating with each other. Server #1's IP Address changes and Server #2 will only reconnect after a JVM restart.

Digging into the javadoc for InetAddress makes it real easy to explain this behavior. And I'll be the first to admit that I had no idea this was the default behavior. Java's default is to cache IP addresses!

Java's IP caching behavior can be controlled by two system properties:

networkaddress.cache.ttl (default: -1) Indicates the caching policy for successful name lookups from the name service. The value is specified as as integer to indicate the number of seconds to cache the successful lookup. A value of -1 indicates "cache forever".

networkaddress.cache.negative.ttl (default: 10) Indicates the caching policy for un-successful name lookups from the name service. The value is specified as as integer to indicate the number of seconds to cache the failure for un-successful lookups. A value of 0 indicates "never cache". A value of -1 indicates "cache forever".

Remember this the next time you are playing Java Trivia, or if you want to be really devious, put this on a interview questionairre.