# Optimize Java DNS Lookup

### **Prefer IPv4**

Starting with version 4, Java performs both IPv6 and IPv4 DNS lookups. If the DNS server is not set up to properly respond to IPv6 queries, then Java will wait for the IPv6 query to time out. This can manifest in what looks like network slowdowns.

To bypass IPv6 DNS lookups, set the Java System property `java.net.preferIPv4Stack` to true.

You can set it on the command line

`java -Djava.net.preferIPv4Stack=true ...`

Or you can set the propery in the Java Code itself 

```java
System.setProperty("java.net.preferIPv4Stack","true");
```

### `   `I**ncrease cache time**

When a Java 6 security manager is not set, the default DNS cache is 30 seconds.  To reduce network DNS queries, the number of seconds of DNS cache can be increased by setting the `networkaddress.cache.ttl` System property.  Set this property to `-1` to never expire the DNS cache.  
  
Command line:  

`java -Dnetworkaddress.cache.ttl=-1 ...`

Java code:

```java
System.setProperty("networkaddress.cache.ttl","-1");
```
