Solving: Request header is too large

Recently, I faced a production issue where some of our requests were failing with the following exception

org.apache.coyote.http11.Http11Processor.service Error parsing HTTP request header
 Note: further occurrences of HTTP header parsing errors will be logged at DEBUG level.
 java.lang.IllegalArgumentException: Request header is too large
    at org.apache.coyote.http11.Http11InputBuffer.parseHeaders(
    at org.apache.coyote.http11.Http11Processor.service(
    at org.apache.coyote.AbstractProcessorLight.process(
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(
    at java.util.concurrent.ThreadPoolExecutor.runWorker(
    at java.util.concurrent.ThreadPoolExecutor$
    at org.apache.tomcat.util.threads.TaskThread$

Interestingly, the occurrence of these errors in the logs were very few but I was seeing lot of failures in our availability dashboards. That’s where I observed this note in the exception:

` Note: further occurrences of HTTP header parsing errors will be logged at DEBUG level.`

The way tomcat works is that for certain type of errors such as above, it will only log the error once with the INFO level, and then further occurrences are logged only at the DEBUG level.

This change in log level is an in memory change and hence is retained until the tomcat is restarted.

You can see that in the source code here on line 1144:

To get this error log emitted for each event, DEBUG log needs to be enabled. For this add org.apache.coyote.level = FINE in tomcat/conf/

Why is this problem caused?

HTTP specification does not enforce any size limit on the size of headers. However, modern browsers and application servers such as Tomcat enforce this limit. By default 4 KB is the limit on Tomcat, which is more than enough. However, if you are adding lot of headers or using cookies extensively, this limit can be breached.

To solve this, I needed to add maxHttpHeaderSize in $TOMCAT_HOME/conf/server.xml

<Connector port="8080" maxHttpHeaderSize="65536" protocol="HTTP/1.1" ... />

In case, you are using Spring Boot, add the following in