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

Cache responses for unauthenticated users #75

Closed
wants to merge 3 commits into from
Closed

Conversation

Nutomic
Copy link
Member

@Nutomic Nutomic commented Jun 6, 2023

This can potentially reduce server load a lot because unauthenticated users trigger much fewer database queries. It will only be effective once lemmy-ui switches from websocket and uses the http api (Lemmy 0.18).

It works fine in a quick test, but will need more testing to make sure it doesnt accidentally leak private data. For now I deployed it on ds9.lemmy.ml

Copy link
Member

@dessalines dessalines left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder what the best way to test this is, to make sure nginx is properly doing caching.

Our isomorphic server, doing local requests, makes this a little complicated to think about.

@miyurusankalpa
Copy link

miyurusankalpa commented Jun 6, 2023

maybe caching static content like images for all users with a larger valid period will also help reduce the load.

Also noticed that the cache control header needs tuning too.

curl -I  https://ds9.lemmy.ml/css/themes/darkly.css
HTTP/2 200 
server: nginx
date: Tue, 06 Jun 2023 17:46:28 GMT
content-type: text/css; charset=utf-8
content-length: 143945
vary: Accept-Encoding
x-powered-by: Express
content-security-policy: default-src 'self'; manifest-src *; connect-src *; img-src * data:; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; form-action 'self'; base-uri 'self'; frame-src *
accept-ranges: bytes
cache-control: public, max-age=0
last-modified: Mon, 27 Mar 2023 00:21:59 GMT
etag: W/"23249-18720717458"
strict-transport-security: max-age=63072000
referrer-policy: same-origin
x-content-type-options: nosniff
x-frame-options: DENY
x-xss-protection: 1; mode=block

@Nutomic
Copy link
Member Author

Nutomic commented Jun 6, 2023

@dessalines This PR sets the header x-cache-status which you can check. See the nginx docs for $upstream_cache_status for details.

@Nutomic Nutomic mentioned this pull request Jun 7, 2023

# Response cache for unauthenticated users
proxy_cache lemmy_cache_{{domain}};
proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adding updating can also help here to reduce requests to upstream, when an object expires out of the cache.

# Response cache for unauthenticated users
proxy_cache lemmy_cache_{{domain}};
proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
proxy_cache_valid 1m;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be up for increasing this also.

@Nutomic anything preventing merging this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I deployed it on https://voyager.lemmy.ml/ just now, and it seems to work well. Data is getting cached as expected when user is logged out, and not cached when user is logged in. I also didnt find any leakage of private data.

Though it would be good to update it for using LemmyNet/lemmy-ui#1641 once that is deployed as its a bit cleaner.

@Nutomic
Copy link
Member Author

Nutomic commented Jun 30, 2023

I had this deployed on lemmy.ml but its not working. The site was logging me out whenever I opened a new tab, and sometimes there were generic error messages which wouldnt go away. We have to wait until there is proper caching support in the projects themselves.

Edit: generic error messages are still happening so those are unrelated to caching, but Im not getting logged out anymore.

@Nutomic Nutomic closed this Jun 30, 2023
@ticoombs ticoombs deleted the cache-responses branch December 2, 2023 23:32
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.

None yet

4 participants