Skip to content

feat: Add gzip compression for large cache entries to reduce memory footprint#2099

Merged
nimrod-teich merged 2 commits into
mainfrom
cache-compression-optimization
Nov 23, 2025
Merged

feat: Add gzip compression for large cache entries to reduce memory footprint#2099
nimrod-teich merged 2 commits into
mainfrom
cache-compression-optimization

Conversation

@nimrod-teich

Copy link
Copy Markdown
Contributor

Description

This PR implements gzip compression for large cache entries to significantly reduce memory footprint in the cache service.

Problem

Cache entries for large debug queries (30+ MB JSON responses) were consuming excessive memory, leading to potential OOM issues in high-throughput scenarios.

Solution

  • Automatically compress cache responses larger than 1MB using gzip
  • Transparently decompress on cache retrieval
  • Track compression status with IsCompressed flag in CacheValue
  • JSON responses typically achieve 70-90% compression (tested at 99.53% for realistic payloads)

Changes

  • ecosystem/cache/handlers.go:

    • Added CompressionThreshold constant (1MB)
    • Added IsCompressed field to CacheValue struct
    • Implemented compressData() and decompressData() helper functions
    • Modified formatCacheValue() to compress large responses
    • Modified ToCacheReply() to automatically decompress on retrieval
    • Updated Cost() to reflect actual stored size (compressed if applicable)
  • ecosystem/cache/handlers_test.go (new file):

    • Comprehensive test suite with 21 subtests covering:
      • Compress/decompress roundtrip verification
      • Error handling for invalid gzip data
      • Threshold-based compression logic
      • Automatic decompression on retrieval
      • Memory cost calculation
      • Real-world compression ratio validation (99.53% for repeated JSON)

Impact

  • Memory savings: 70-90% reduction for large JSON responses
  • No API changes: Compression/decompression is transparent
  • Performance: Minimal CPU overhead (gzip is highly optimized for JSON)
  • Backward compatible: Existing cache entries continue to work

Closes: #XXXX


Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • read the contribution guide
  • included the correct type prefix in the PR title (perf)
  • confirmed ! in the type prefix if API or client breaking change (N/A - no breaking changes)
  • targeted the main branch
  • provided a link to the relevant issue or specification
  • reviewed "Files changed" and left comments if necessary
  • included the necessary unit and integration tests (comprehensive test suite with 21 subtests)
  • updated the relevant documentation or specification, including comments for documenting Go code
  • confirmed all CI checks have passed

Reviewers Checklist

All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic, API design and naming, documentation is accurate, tests and test coverage

@github-actions

github-actions Bot commented Nov 6, 2025

Copy link
Copy Markdown

Test Results

3 049 tests  +30   3 048 ✅ +30   34m 29s ⏱️ + 12m 12s
  126 suites + 1       1 💤 ± 0 
    7 files   + 1       0 ❌ ± 0 

Results for commit d782655. ± Comparison against base commit f3fd615.

♻️ This comment has been updated with latest results.

@nimrod-teich nimrod-teich force-pushed the cache-compression-optimization branch from 2f8fac5 to cc12879 Compare November 6, 2025 14:12
@nimrod-teich nimrod-teich force-pushed the cache-compression-optimization branch from f0f4be9 to 1cfd581 Compare November 20, 2025 16:30
- Compress cache responses larger than 1MB to save memory
- JSON responses typically compress 70-90%, significantly reducing memory footprint
- Automatically decompress on cache retrieval
- Track compression status in CacheValue struct
- Use centralized protocol/common compression module to avoid code duplication
- Remove duplicate compression functions from cache handlers

Test coverage:
- Test compress/decompress roundtrip with various payloads
- Test error handling for invalid gzip data
- Test formatCacheValue compression logic with threshold
- Test automatic decompression on cache retrieval
- Test Cost() method returns compressed size
- Test compression ratio for realistic JSON payloads (98.88% for repeated JSON)
- All tests pass successfully
@nimrod-teich nimrod-teich force-pushed the cache-compression-optimization branch from 1cfd581 to 60e8bb9 Compare November 20, 2025 16:41
This error occurs when providers forward node errors (like 'Internal error')
back to consumers. This is expected behavior, especially during E2E tests
where unsupported methods or node issues can trigger these errors.

Related to commit ca8987c which refactored unsupported method error handling.
@nimrod-teich nimrod-teich merged commit bfc3ac4 into main Nov 23, 2025
30 checks passed
@nimrod-teich nimrod-teich deleted the cache-compression-optimization branch November 23, 2025 09:03
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.

2 participants