There are two hard problems in programming: naming things and cache invalidation. I'll cover the latter, in a microservice-based system. Given a fairly standard setup with API Gateway and a backend service with its own database, I'll show how to implement cache that allows us to avoid database queries without modifying API client.
The whole talk is based on live coding.
In the talk I will go through the implementation of both sides of an ETag cache.
First part will be a ASGI middleware (using FastAPI as an example) that captures resources returned by REST endpoints and creates ETags for them.
The middleware will also allow views to affect cache key construction, so that the mechanism can be made aware of e.g. custom headers.
This middleware will be installed in the "backend" service.
On the "backend" side, I will also show how to invalidate the cache when data used to create a given resource is modified.
The second part is an extension of aiohttp's ClientSession that stores ETagged response bodies. This will be installed inside the API Gateway, allowing external clients to utilize the cache without even knowing about it.
Finally, I'll mention how to make it usable in a distributed system, by implementing a simple content-based pubsub mechanism.