Skip to content
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

⚡ feat: Static File Caching #3455

Merged
merged 4 commits into from
Aug 5, 2024

Conversation

mawburn
Copy link
Contributor

@mawburn mawburn commented Jul 26, 2024

Summary

Related documentation PR: LibreChat-AI/librechat.ai#95

This pull request introduces static file caching and Gzip compression to the project, significantly enhancing its performance and efficiency. By implementing static file caching, we enable frequently accessed files like images, CSS, and JavaScript to be stored locally on the user's device, reducing the need for repeated server requests and decreasing load times. This is only triggered when NODE_ENV is set to production.

MDN Cache-Control

Additionally, the inclusion of compression reduces the size of these files before transmission, minimizing bandwidth usage and further speeding up page load times. Combined with the Cache-Control headers this should be a very negligible load on the server.

These optimizations collectively lead to a faster, more responsive user experience, lower server load, and improved scalability, making the application more robust and user-friendly.

On my instance here are the results:

Using compression → 2.4mb transferred, from 6.2mb total
Screenshot 2024-07-26 at 11 03 08 AM

Using compression + local caching on a refresh → 664kb transferred, from 6.2mb total
Screenshot 2024-07-26 at 11 04 34 AM (1)

With out these, the full 6.2mb was transferred on page load/refresh or were getting 304 responses from the API, causing extra load on the API server that isn't needed.

Update:

I have this deployed in our production for thousands of users and it seems to have also fixed our issues with memory usage.

This is with 8gb of memory and it was using 70-85% memory all the time. Where now it's about 15-18%.

Screenshot 2024-07-29 at 12 08 20 PM (1) (1)

Change Type

  • New feature (non-breaking change which adds functionality)

Checklist

Please delete any irrelevant options.

  • My code adheres to this project's style guidelines
  • I have performed a self-review of my own code
  • I have made pertinent documentation changes

Copilot Summary

This pull request includes several changes to enhance the server's performance and improve caching mechanisms. The most important changes include adding the compression middleware, implementing a static file cache control utility, and updating the relevant configurations and routes.

Performance Improvements:

Cache Control Enhancements:

  • .env.example: Added configuration options for static file cache control, including STATIC_CACHE_MAX_AGE and STATIC_CACHE_S_MAX_AGE.
  • api/server/utils/staticCache.js: Created a new utility to handle static file caching with configurable cache headers based on environment variables.
  • api/server/index.js: Replaced express.static with the new staticCache utility for serving static files. [1] [2]
  • api/server/routes/static.js: Updated the static routes to use the new staticCache utility.

@mawburn mawburn force-pushed the mawburn/static-cache branch 7 times, most recently from 1e96e2a to 66cec19 Compare July 26, 2024 11:55
@lidonius1122
Copy link
Contributor

I see a potential issue with the compression implementation. For users like me who use Traefik with Brotli or Zstd compression, having Gzip at the application level prevents these more efficient methods from being applied.

It would be better if we had something like a DISABLE_COMPRESSION environment variable. This way, users could choose to handle compression at the reverse proxy level if needed.

@mawburn
Copy link
Contributor Author

mawburn commented Jul 29, 2024

That's a good point @lidonius1122, I can add that.

@danny-avila
Copy link
Owner

thanks!

@mawburn
Copy link
Contributor Author

mawburn commented Jul 29, 2024

Done @lidonius1122

@lidonius1122
Copy link
Contributor

Seems to be working as it should so far @mawburn

@danny-avila danny-avila changed the title Static file caching ⚡ feat: Static File Caching Aug 5, 2024
@danny-avila
Copy link
Owner

I see a potential issue with the compression implementation. For users like me who use Traefik with Brotli or Zstd compression, having Gzip at the application level prevents these more efficient methods from being applied.

It would be better if we had something like a DISABLE_COMPRESSION environment variable. This way, users could choose to handle compression at the reverse proxy level if needed.

I've added notes in docs to mention this

LibreChat-AI/librechat.ai#98

@danny-avila danny-avila merged commit e4ac42f into danny-avila:main Aug 5, 2024
1 check passed
danny-avila pushed a commit that referenced this pull request Aug 5, 2024
* add static file cache

* disable compression env variable
@mawburn mawburn deleted the mawburn/static-cache branch August 7, 2024 13:41
danny-avila pushed a commit that referenced this pull request Aug 17, 2024
* add static file cache

* disable compression env variable
kenshinsamue pushed a commit to intelequia/LibreChat that referenced this pull request Sep 17, 2024
* add static file cache

* disable compression env variable
BertKiv pushed a commit to BertKiv/LibreChat that referenced this pull request Dec 10, 2024
* add static file cache

* disable compression env variable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants