Caching in microservices
2023-07-21 , North Hall

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.


Expected audience expertise:

intermediate

I work mostly on the backend, but also embedded devices. My primary focus are IoT and other network-oriented applications. That's one of the reasons I am not allowed to touch the UI.

I think the industry is going in circles and we invent LISP every 10 years. I don't know why I code in Python, since real programmers code in C.

Currently working as a System Architect at Silvair.