Loading…
Loading…
Explains the mechanism of the runtime route cache, including resolution logic, TTL configuration, multi-tenancy, and SOAPAction disambiguation.
The Runtime Route Cache Service is an in-memory caching layer that accelerates API request routing. It solves the problem of high latency and database load by caching the mapping between an incoming request's attributes (such as its HTTP method and path) and the configured backend route.
This service acts as an intermediary between the primary request handler and the route definition database. When a request arrives, the system queries this service first. If a valid cached route exists, it is returned immediately, avoiding a database query. If not, the service fetches candidate routes from the database, determines the correct match, and stores the result in the cache for subsequent requests.
The service maintains two levels of caching:
This component is essential for system performance, especially in high-traffic environments.
The route resolution process follows a specific sequence of operations to find a matching route for an incoming request, using an in-memory cache to optimize performance.
flowchart TD
subgraph "Route Resolution Flow"
A[Request Handler] -- "resolveRoute()" --> B{Check Resolved Route Cache};
B -- "Hit" --> G[Return Cached Route];
B -- "Miss" --> C{Check Candidate List Cache};
C -- "Hit" --> E[Use Cached Candidate List];
C -- "Miss" --> D[Query Database for Candidates];
D -- "Routes" --> F[Sort & Store Candidate List];
F --> E;
E -- "Candidates" --> H[Perform Path & SOAPAction Matching];
H -- "Resolved Route" --> I[Store in Resolved Route Cache];
I --> G;
end
G -- "RouteMetadata" --> J[Request Processing Continues];Cache Key Generation: The service first constructs a unique cache key for the request. The key is a composite of the current tenant ID, direction (PUBLISH or CONSUME), environment, HTTP method, request path, and the normalized SOAPAction header value (if present).
IMPORTANT
The cache is tenant-aware. Each tenant's routes are cached separately to ensure strict data isolation. Requests without a tenant context are assigned a special unbound scope and cannot access tenant-specific cache entries.
Primary Cache Lookup: The service checks for a matching entry in the primary resolved route cache (cache). If a non-expired entry is found, it is returned immediately, and the process ends.
Candidate List Lookup: On a primary cache miss, the service checks a secondary cache (routeListCache) that stores lists of potential candidate routes. This cache is keyed by tenant, direction, environment, and HTTP method. If a valid list is found, it is used for the next step.
Database Query: If the candidate list is not cached, the service queries the database via RuntimeRouteRepository.findRoutes() to load all routes matching the request's environment, method, and direction. It then attaches the associated backend target configurations to each route.
Candidate Sorting: The loaded candidates are sorted to ensure that the most specific routes are evaluated first. The sorting order is:
/a/b/{id} comes before /a/{id1}/{id2}).restPath).Candidate Caching: The sorted, immutable list of candidates is stored in the routeListCache with the configured Time-to-Live (TTL).
Final Resolution: The service iterates through the sorted candidate list to find a match.
restPath template. It supports path parameters (e.g., {userId}).CONSUME services, it attempts a secondary match using the SOAPAction header from the request against the soapAction field of the candidate routes. If a unique match is found, that route is chosen.SOAPAction match is found, or if the direction is PUBLISH, the system defaults to the first route in the sorted list. A warning is logged in ambiguous CONSUME scenarios.Result Caching: The final ResolvedRoute (or an empty result if no match was found) is stored in the primary cache with the configured TTL.
The service provides a mechanism to clear all its internal caches.
invalidateAll() method completely clears both the resolved route cache and the candidate list cache.This is necessary to force the system to reload route definitions from the database after configuration changes are made.
To ensure consistent matching, the value of the SOAPAction HTTP header is normalized before being used in cache keys or comparisons. The normalization process involves:
") characters.null.The cache's Time-to-Live (TTL) is configurable. The system uses the first value it finds in the following order of precedence:
runtime_route_cache_ttl_seconds.S2R_RUNTIME_ROUTE_CACHE_TTL_SECONDS environment variable.The service periodically refreshes the TTL from the database every 60 seconds, allowing for dynamic adjustments without requiring a service restart.
| Parameter | Type | Default | Description |
|---|---|---|---|
S2R_RUNTIME_ROUTE_CACHE_TTL_SECONDS | Environment Variable (long) | 15 | The cache TTL in seconds. This is used as a fallback if the database setting is not present. The minimum effective value is 1. |
runtime_route_cache_ttl_seconds | Database Setting (long) | — | The cache TTL in seconds, read from the settings repository. This value takes precedence over the environment variable. The minimum effective value is 1. |
The RuntimeRouteCacheService exposes the following core methods for use by other internal components.
resolveRouteResolves an incoming request to a configured route. There are two overloads for this method.
resolveRoute(String direction, String environment, String method, String path)Resolves a route without considering the SOAPAction header.
| Parameter | Type | Description |
|---|---|---|
direction | String | The service direction, e.g., PUBLISH or CONSUME. |
environment | String | The target environment for the request. |
method | String | The HTTP method of the request (e.g., POST). |
path | String | The request path. The caller must strip any leading /direction segment. |
Optional<ResolvedRoute> containing the matched route and extracted path parameters, or an empty Optional if no match is found.resolveRoute(String direction, String environment, String method, String path, String soapAction)Resolves a route with optional disambiguation using the SOAPAction header. This is primarily used for CONSUME services where multiple operations might share the same endpoint URL.
| Parameter | Type | Description |
|---|---|---|
direction | String | The service direction, e.g., PUBLISH or CONSUME. |
environment | String | The target environment for the request. |
method | String | The HTTP method of the request (e.g., POST). |
path | String | The request path. The caller must strip any leading /direction segment. |
soapAction | String | The value of the SOAPAction HTTP header, or null if not present. |
Optional<ResolvedRoute> containing the matched route and extracted path parameters, or an empty Optional if no match is found.invalidateAllpublic void invalidateAll()
Immediately purges all entries from all internal caches, including the resolved route cache, the candidate list cache, and the linked OpenAPI validator cache. Subsequent requests will trigger a full reload from the database.
CONSUME ServicesThe system logs a warning when it cannot unambiguously resolve a route for a CONSUME service.
S2R-RUN-0412ambiguous-route-soapaction direction={} path={} soapAction={} candidates={} — ...CONSUME service matches the path template (restPath) of multiple configured routes.SOAPAction HTTP header is missing from the request.SOAPAction header is present, but its value does not match the soapAction field of any of the matching candidate routes.PUBLISH and CONSUME directions is fundamental to how routes are fetched and cached.