Skip to content

FOUR-20929: Implement ETag Caching #7892

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 26 commits into from
Jan 23, 2025
Merged

Conversation

eiresendez
Copy link
Contributor

@eiresendez eiresendez commented Jan 13, 2025

Issue & Reproduction Steps

The server sends large amounts of data with each request, even if the data hasn’t changed. This causes unnecessary server load, increased response times and higher resource consumption, especially for repeated requests.

Solution

  • Added ETag caching headers to API endpoints.
  • The server will now respond with a 304 Not Modified when data remains unchanged, improving performance and reducing bandwidth usage.

How to Test

  • Verify that repeated requests for the same unchanged data return a 304 Not Modified status.
  • Check the docs/etag-caching.md for detailed steps and testing guidelines.

Related Tickets & Packages

Code Review Checklist

  • I have pulled this code locally and tested it on my instance, along with any associated packages.
  • This code adheres to ProcessMaker Coding Guidelines.
  • This code includes a unit test or an E2E test that tests its functionality, or is covered by an existing test.
  • This solution fixes the bug reported in the original ticket.
  • This solution does not alter the expected output of a component in a way that would break existing Processes.
  • This solution does not implement any breaking changes that would invalidate documentation or cause existing Processes to fail.
  • This solution has been tested with enterprise packages that rely on its functionality and does not introduce bugs in those packages.
  • This code does not duplicate functionality that already exists in the framework or in ProcessMaker.
  • This ticket conforms to the PRD associated with this part of ProcessMaker.

eiresendez and others added 25 commits November 26, 2024 14:09
- Introduced IfNoneMatch middleware to handle ETag-based caching:
  - Supports safe HTTP methods (GET, HEAD).
  - Returns "304 Not Modified" for matching ETags.
  - Handles multiple ETags and restores original request methods.

- Added EtagManager for flexible ETag generation:
  - Default ETag generation uses MD5 hash.
  - Supports custom hash algorithms like SHA-256 via callbacks.

- Included test cases:
  - Validate default ETag generation.
  - Test custom callback logic for ETag creation.

This commit adds foundational support for ETag-based caching in the application.
…ware

- Merged `etag.set` and `etag.if-none-match` middlewares into a single `etag.handle` middleware.
- Simplified logic to reduce redundancy and improve maintainability.
- Ensured ETag validation (`If-None-Match`) and generation are handled in the same flow.
- Preserved compatibility with HEAD requests for consistency.

This refactor improves clarity, reduces potential misconfigurations, and keeps the ETag logic centralized.
…d tests

- Created tests for the new `HandleEtag` middleware:
  - Validates ETag generation and correctness.
  - Tests responses for both matching and non-matching `If-None-Match` headers.
  - Ensures proper handling of weak ETags (`W/`).
- Removed old tests for `SetEtag` and `IfNoneMatch` middlewares as they are no longer needed.

This commit improves test clarity and ensures the new ETag middleware behaves as expected.
- Added a test to validate ETag generation for user-specific routes using `etag:user`.
  - Simulates an authenticated user and verifies the ETag includes the user ID.

These tests ensure the ETag middleware behaves correctly for both user-specific and common routes.
FOUR-20933: Implement ETag Caching for Screens Data
- Process only GET and HEAD methods to ensure middleware relevance and avoid unnecessary processing for non-cacheable HTTP methods.
- Add a check to determine if the response is cacheable, filtering out non-cacheable responses (e.g., those with 'no-store' directive or non-cacheable status codes).
- Default ETag generation now includes user-specific data (auth()->id()) to enforce personalized caching by default.
- Removed 'scope' and 'includeUser' logic for simplified and consistent caching behavior.
- Refactored `EtagManager` to support dynamic ETag generation based on configurable sources (`updated_at`).
- Introduced `generateEtagFromTables` with a `source` parameter for flexibility in determining the source of truth.

This update prepares the app for future scalability and allows switching between different ETag generation strategies.
- Applied ETag middleware to the 'startProcesses' route for improved caching and reduced payload size.
- Added default 'etag_tables' parameter set to 'processes' to optimize ETag generation for this route.
FOUR-20944: Implement ETag Caching for Task and Case Data
- Includes an overview of ETag functionality and benefits.
- Provides implementation details for the HandleEtag middleware.
- Covers ETag generation based on tables and response content.
- Adds examples for conditional responses and route configuration.
- Documents unit and manual testing approaches for ETag functionality.
- Suggests future improvements for ETag caching and metrics collection.
- Introduced a new test class `HandleEtagCacheInvalidationTest` to verify ETag behavior upon database updates.
- Implemented tests to ensure ETag changes when the underlying data is modified and that the correct ETag is returned for subsequent requests.
- Updated existing `HandleEtagTest` to include a test for returning 304 Not Modified when the ETag matches the client-provided value.
- Introduced logging for highly dynamic endpoints by tracking ETag history for each endpoint.
- Implemented a mechanism to limit the number of tracked ETags and log when all tracked ETags are unique.
- Updated documentation to reflect the new logging feature and its implications for performance optimization.
- Integrated `config/etag.php` for dynamic configuration of ETag functionality.
- Added `enabled` and `log_dynamic_endpoints` flags to control feature behavior.

This update improves flexibility and allows disabling ETag processing entirely when `enabled` is set to false.
- Added custom Trend metrics to measure and compare durations of 200 OK and 304 Not Modified responses
- Validates that 304 responses are faster using If-None-Match header
- Improved test clarity by focusing on ETag performance under load
- Implemented tracking of ETag values for specified endpoints
- Added logic to identify dynamic endpoints when ETag history shows diff values
FOUR-20954: Monitor ETag Caching Performance
Copy link

Quality Gate passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarQube

@ryancooley ryancooley merged commit 7ecbcd6 into release-2025-winter Jan 23, 2025
11 of 13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants