telescope icon indicating copy to clipboard operation
telescope copied to clipboard

Add appropriate cache headers to microservices, cache them in nginx

Open humphd opened this issue 3 years ago • 8 comments

In production we have our services running behind nginx, and it's amazing at caching. If we set the appropriate cache headers (e.g., cache-control, etag, etc), nginx and the users' browsers will improve performance by caching everything.

Some resources:

  • https://restfulapi.net/caching/
  • https://devcenter.heroku.com/articles/increasing-application-performance-with-http-cache-headers

We can either do this manually in each service, or we could add some helper middleware in Satellite to allow each microservice to opt-in to various types of caching.

You can see how we do the caching for various things in nginx now: https://github.com/Seneca-CDOT/telescope/blob/master/config/nginx.conf.template#L173-L230.

See also https://github.com/Seneca-CDOT/telescope/blob/master/config/nginx.conf.template#L127-L133 and https://github.com/Seneca-CDOT/telescope/blob/master/config/nginx.conf.template#L233-L244 for how we do the nginx caching for the Telescope 1.0 backend. We'd need something similar for the API server definition https://github.com/Seneca-CDOT/telescope/blob/master/config/nginx.conf.template#L152-L164.

humphd avatar Mar 13 '21 19:03 humphd

#1978 adds cache headers for the status service.

humphd avatar Mar 18 '21 21:03 humphd

I might just make a module in Satellite that will add headers onto the passed Response object, I'll add in a test specifically for Cache-Control

Metropass avatar Mar 19 '21 20:03 Metropass

@Metropass there are npm modules that will do this, we could research one and use it.

humphd avatar Mar 19 '21 20:03 humphd

I found this, it might be useful specifically for Cache-Control, I'm currently researching a module that will work for any type of CORS headers.

https://www.npmjs.com/package/http-cache-semantics

Metropass avatar Mar 19 '21 20:03 Metropass

I don't think that module will help us here. I'm also not 100% sure we need to do anything special in Satellite for this: we just need to specify headers on a per-route basis in each microservice.

If we later find out that there are common patterns, we can extract them into something generic in Satellite; but let's not start there.

humphd avatar Mar 21 '21 15:03 humphd

@aserputov, what is your progress related to this?

JerryHue avatar Mar 22 '22 23:03 JerryHue

@JerryHue work in progress.

aserputov avatar Mar 22 '22 23:03 aserputov

@JerryHue I think we should move this one too.

aserputov avatar Mar 22 '22 23:03 aserputov