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

Add caching headers to all responses #101

Merged
merged 2 commits into from
Sep 11, 2019
Merged

Conversation

ruflin
Copy link
Contributor

@ruflin ruflin commented Sep 11, 2019

The content of the registry only changes if a new package is added or if the registry is updated. Because of this, all responses can be cached. Caching headers are introduced for all requests and the caching time is currently set to 1h. This could be increased in the future especially for the catchAll endpoint as these assets basically never change.

CHANGELOG.md Outdated
@@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

* Adding Cache-Control max-age headers to all http responses set to 1h.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jfsiii @skh I plan to add now changelog entries for all PR's which affect the API in any way and probably most others too, except some refactoring.

@ruflin ruflin requested review from jfsiii and skh September 11, 2019 11:20
func cacheHeaders(w http.ResponseWriter) {
w.Header().Add("Cache-Control", "max-age="+cacheTime)
w.Header().Add("Cache-Control", "public")
}
Copy link

Choose a reason for hiding this comment

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

These are a good start and Good Enough to merge, IMO.

However, I think adding a time-based (Last-Modified) or content-based (ETag) header would be a good improvement because it allows the client to reuse a response which is older than cacheTime but which hasn't changed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The Last-Modified will be a bit tricky as the data is generated and it is not clear to me what we would set it to. I was thinking about the ETag quite a bit and was not sure if it will be beneficial if we have a very long cache time. We could use the file sha1 as the etag for example.

Copy link

Choose a reason for hiding this comment

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

I think there are reasonable answers to those questions, but let's avoid them for now and focus on the max-age value.

I don't think the registry should use the same caching policy for all responses.

/package seems well-suited for a "far future" expiration date. e.g. a year or more. The assets associated with a package-x@major.minor.patch should never change. AFAIK, the registry won't allow force push/republish, so any change should be a new version. That means the expiration should be able to be well into the future without any issues.

/search & /categories are dynamic and should have the expirations set to whatever we feel is Fast Enough and/or doesn't overwhelm the registry.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

SGTM and agree on the different caching times. I expect we will have in the future a CDN in front so the total number of request on the service itself should become pretty low.

The content of the registry only changes if a new package is added or if the registry is updated. Because of this, all responses can be cached. Caching headers are introduced for all requests and the caching time is currently set to 1h. This could be increased in the future especially for the `catchAll` endpoint as these assets basically never change.
@ruflin
Copy link
Contributor Author

ruflin commented Sep 11, 2019

@jfsiii Going to merge this for now but would like to continue the discussion on the Cache headers. Probably depends quite a bit also on how the Kibana Http Client handles cache headers.

@ruflin ruflin merged commit 1036e16 into elastic:master Sep 11, 2019
@ruflin ruflin deleted the caching-headers branch September 11, 2019 14:16
@jfsiii
Copy link

jfsiii commented Sep 11, 2019

@ruflin I'll follow up but the Registry sets the tone here. There's no special/manual intervention required for clients. It's baked into the protocol, browsers, down the line. They can override if need be but it mostly Just Works (based on however the server responds).

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.

2 participants