Menu

Cache-aside with FastAPI and Redis: invalidation and concurrent requests
📰

Cache-aside with FastAPI and Redis: invalidation and concurrent requests

DEV Community: docker·Eduardo Teixeira·about 12 hours ago
#zVurNGRh
#devtodocker#devto#dev#cache#redis
Reading 0:00
15s threshold

This post was originally published in Portuguese on my blog: Cache-aside com FastAPI e Redis: invalidação e requisições simultâneas .

Anyone who has put an API in production knows the story: the read endpoint is simple, but the query behind it is expensive, and the same row gets requested hundreds of times per minute. Then someone suggests "put a Redis in front of it". It works, but the hard part isn't writing to the cache. It's deciding when to read, when to invalidate, and what to do when ten requests arrive together and the cache is empty.

In this post I built a small example with FastAPI and Redis using the cache-aside pattern, paying attention to those two annoying points: invalidation and concurrent requests. Everything runs with docker compose, and the tests don't need a real Redis.

The idea: cache-aside

In cache-aside, the application is in charge, not the cache.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.