Cache Filter
All incoming requests are routed via the front Envoy, which acts as a reverse proxy sitting on the edge of the network. Ports 8000
and 8001
are exposed by docker compose (see /examples/cache/docker-compose.yaml) to handle HTTP
calls to the services, and requests to /admin
respectively. Two backend services are deployed behind the front Envoy, each with a sidecar Envoy.
The front Envoy is configured to run the Cache Filter, which stores cacheable responses in an in-memory cache, and serves it to subsequent requests. In this demo, the responses that are served by the deployed services are stored in . This file is mounted to both services’ containers, so any changes made to the stored responses while the services are running should be instantly effective (no need to rebuild or rerun).
For the purposes of the demo, a response’s date of creation is appended to its body before being served. An Etag is computed for every response for validation purposes, which only depends on the response body in the yaml file (i.e. the appended date is not taken into account). Cached responses can be identified by having an age
header. Validated responses can be identified by having a generation date older than the date
header; as when a response is validated the date
header is updated, while the body stays the same. Validated responses do not have an age
header. Responses served from the backend service have no age
header, and their date
header is the same as their generation date.
The following documentation runs through the setup of Envoy described above.
Ensure that you have a recent versions of docker
and docker-compose
installed.
A simple way to achieve this is via the Docker Desktop.
If you have not cloned the Envoy repo, clone it with:
SSH
HTTPS
git clone https://github.com/envoyproxy/envoy.git
$ pwd
envoy/examples/cache
$ docker-compose build --pull
$ docker-compose up -d
$ docker-compose ps
Name Command State Ports
------------------------------------------------------------------------------------------------------------------------
cache_front-envoy_1 /docker-entrypoint.sh /bin ... Up 10000/tcp, 0.0.0.0:8000->8000/tcp, 0.0.0.0:8001->8001/tcp
cache_service1_1 /bin/sh -c /usr/local/bin/ ... Up 10000/tcp, 8000/tcp
cache_service2_1 /bin/sh -c /usr/local/bin/ ... Up 10000/tcp, 8000/tcp
You can now send a request to both services via the front-envoy
. Note that since the two services have different routes, identical requests to different services have different cache entries (i.e. a request sent to service 2 will not be served by a cached response produced by service 1).
curl -i localhost:8000/service/<service_no>/<response>
service_no
: The service to send the request to, 1 or 2.
response
: The response that is being requested. The responses are found in .
The provided example responses are:
valid-for-minute
This response remains fresh in the cache for a minute. After which, the response gets validated by the backend service before being served from the cache. If found to be updated, the new response is served (and cached). Otherwise, the cached response is served and refreshed.
private
This response is private; it cannot be stored by shared caches (such as proxies). It will always be served from the backend service.
-
This response has to be validated every time before being served.
Example responses
1. valid-for-minute
Naturally, response date
header is the same time as the generated time. Sending the same request after 30 seconds gives the same exact response with the same generation date, but with an age
header as it was served from cache:
HTTP/1.1 200 OK
content-type: text/html; charset=utf-8
content-length: 103
cache-control: max-age=60
custom-header: any value
etag: "172ae25df822c3299cf2248694b4ce23"
date: Fri, 11 Sep 2020 03:20:40 GMT
server: envoy
x-envoy-upstream-service-time: 11
age: 30
This response will stay fresh for one minute
Response body generated at: Fri, 11 Sep 2020 03:20:40 GMT
After 1 minute and 1 second:
$ curl -i localhost:8000/service/1/valid-for-minute
HTTP/1.1 200 OK
cache-control: max-age=60
custom-header: any value
etag: "172ae25df822c3299cf2248694b4ce23"
date: Fri, 11 Sep 2020 03:21:41 GMT
server: envoy
x-envoy-upstream-service-time: 8
content-length: 103
content-type: text/html; charset=utf-8
Response body generated at: Fri, 11 Sep 2020 03:20:40 GMT
The same response was served after being validated with the backend service. You can verify this as the response generation time is the same, but the response date
header was updated with the validation response date. Also, no header.
Every time the response is validated, it stays fresh for another minute. If the response body changes while the cached response is still fresh, the cached response will still be served. The cached response will only be updated when it is no longer fresh.
2. private
No matter how many times you make this request, you will always receive a new response; new date of generation, new date
header, and no age
header.
3. no-cache
$ curl -i localhost:8000/service/1/no-cache
HTTP/1.1 200 OK
content-type: text/html; charset=utf-8
content-length: 130
cache-control: max-age=0, no-cache
etag: "ce39a53bd6bb8abdb2488a5a375397e4"
date: Fri, 11 Sep 2020 03:23:07 GMT
server: envoy
x-envoy-upstream-service-time: 7
This response can be cached, but it has to be validated on each request
Response body generated at: Fri, 11 Sep 2020 03:23:07 GMT
After a few seconds:
$ curl -i localhost:8000/service/1/no-cache
HTTP/1.1 200 OK
cache-control: max-age=0, no-cache
etag: "ce39a53bd6bb8abdb2488a5a375397e4"
date: Fri, 11 Sep 2020 03:23:12 GMT
server: envoy
x-envoy-upstream-service-time: 7
content-length: 130
content-type: text/html; charset=utf-8
This response can be cached, but it has to be validated on each request
You will receive a cached response that has the same generation time. However, the date
header will always be updated as this response will always be validated first. Also, no age
header.
If you change the response body in the yaml file:
You will receive a new response that’s served from the backend service. The new response will be cached for subsequent requests.
You can also add new responses to the yaml file with different cache-control
headers and start experimenting! To learn more about caching and cache-control
headers visit the MDN Web Docs.