Skip to content

5,800 to 53,000 req/s by skipping the filter chain

I made a Spring endpoint nine times faster this week. The way I did it is by skipping most of what Spring does to keep you safe.

A minimal echo endpoint on the standard Spring MVC path gave 5,800 requests per second. The same endpoint on my fast path gave 53,000. The numbers are from a real application with several hundred mappings, not from a hello world project.

Echo endpoint, requests per second. Higher is better. Standard Spring MVC 5,800 Fast path 53,000

So for a simple endpoint, about nine tenths of the request is spent before your code runs. The request goes through every servlet filter, the security filter chain parses the same path again for each pattern it holds, and then the DispatcherServlet finds the handler. With hundreds of mappings and a long list of security patterns, that work is far larger than an echo.

What the library does

spring-mvc-fastpath is a Spring Boot starter. You mark the hot endpoints:

@MvcAccelerator
@GetMapping("/echo")
public String echo(@RequestParam String value)
{
    return value;
}

Requests to those take a short route past the DispatcherServlet, and run only the filters you list:

mvc:
  accelerator:
    fast-filter-chain:
      mode: ANNOTATED
      fail-if-filter-missing: true
      included-filters:
        - com.example.filters.MyCustomLoggingFilter

There is also a request matcher that caches the parsed path for the length of one request, so the security chain does not parse it again for every pattern. That one is safe to use by itself.

A shortcut around security

I am normally the one who says no to shortcuts around security. This library is a shortcut around security, and the first thing in its README is a warning.

A filter you did not list does not run. If that filter was the one doing authentication, the endpoint is open, and nothing will tell you. That is why fail-if-filter-missing is on by default, so a typo in a class name stops the application at startup. It does not help if you forget the filter altogether.

For most endpoints it is not worth it. Once the handler does a database call, the framework overhead is a small part of the request and the gain goes away. It pays off for the few endpoints that are hit all the time and do almost nothing.

It is not in production with us. I am not sure it should be.