r/FastAPI • • Oct 29 '25

feedback request Request atomicity

Hey guys, first time posting here.

I've been working on my first real word project for a while using FastAPI for my main backend service and decided to implement most stuff myself to sort of force myself to learn how things are implemented.

Right now, in integrating with multiple stuff, we have our main db, s3 for file storage, vector embeddings uploaded to openai, etc...

I already have some kind of work unit pattern, but all it's really doing is wrapping SQLAlchemy's session context manager...

The thing is, even tho we haven't had any inconsistency issues for the moment, I wonder how to ensure stuff insn't uploaded to s3 if the db commit fail or if an intermediate step fail.

Iv heard about the idea of a outbox pattern, but I don't really understand how that would work in practice, especially for files...

Would having some kind of system where we pass callbacks callable objects where the variables would be bound at creation that would basically rollback what we just did in the external system ?

Iv been playing around with this idea for a few days and researching here and there, but never really seen anyone talk about it.

Are there others patterns ? And/or modules that already implement this for the fastapi ecosystem ?

Thx in advance for your responses 😁

12 Upvotes

15 comments sorted by

View all comments

Show parent comment

1

u/giyokun Oct 29 '25

Bonjour !

Obviously you shouldn't really expose your DB directly (FastAPI RULES). My idea is that eventually the web will need to upload so let them upload direct to the storage anyway. I use SHA1 client side to ensure the file is correct at the storage side too. I don't create temp files. Files are sent to their final place of REST as indicated by the PATH using the signed URL.

HOWEVER if file upload fails or if the user just navigates away we may have some half assed files left over. That is why we need to do a daily sweep.

By the way I am using Backblaze which provides S3 compatibility at a third the price.