Skip to content

Cache gateway filters to avoid sorting in every single request #2756

@123liuziming

Description

@123liuziming

Spring Cloud Gateway Server version:

<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gateway-server</artifactId>
<version>4.0.0-SNAPSHOT</version>

In org.springframework.cloud.gateway.handler.FilteringWebHandler#handle. There is a TODO: needed or cached?

image

Every time we handle a request, we need to combine the route filters and global filters, and then sort them. Our test results show that caching the filter corresponding to a certain route can reduce unnecessary sorting, thus reducing CPU usage and improving gateway performance under high concurrency

The QPS and RT is shown as below, after we cache the gateway filters, we can improve the QPS of Spring Cloud Gateweay and reduce the RT of request

image

To solve this problem, We can cache the filters of every single route in a hash map. Every time the routes of Spring Cloud Gateway refreshed, we can update the cached filters of these routes in the hash map we mentioned above.
In Spring Cloud Gateway, we have a listener to monitor the change of routes, which is org.springframework.cloud.gateway.route.RouteRefreshListener. Every time the monitor finds that the routes are changed, it will send an Spring RefreshRoutes Event.

image

We can solve this problem by following several steps:

  1. Add a Spring Event Listener to monitor the RefreshRoutes Event
  2. When the listener in step1 finds that the routes are changed, It will clear the filters cache of every route
  3. When the request is handled by org.springframework.cloud.gateway.handler.FilteringWebHandler#handle, It will lookup in the filters cache of the route. If the filters of the route are in the cache, we use them directly instead of sorting them again, otherwise, we just sort them and put the sorted filters in the cache.

I've made a pull request to solve the issue

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions