
When a system grows, understanding it often becomes harder than changing it. Adding a product to a cart sounds simple, until the rules are scattered across a controller, a service, a repository, and a SQL query. Before I can change anything, I have to put the operation back together in my head.
That is what I want architecture to help me with. Not just where to put a file, but where to look when something needs to change.
There are well-known ways to approach this: Clean Architecture, hexagonal architecture, services, repositories, and dependency injection. I had worked with those ideas in Java and C#, so when I started building backends in Rust, I brought familiar implementations with me.
That was where I started getting frustrated.
Where the abstractions became the work
The problem was not that Rust could not express those designs. It was that reproducing them kept introducing decisions I had not needed to think about in quite the same way before.
Dependency injection was one example. I would introduce a trait for a service, a concrete implementation, and somewhere to connect them. Then I had to decide who owned each dependency, how it was borrowed, and how those references behaved across asynchronous calls.
Rust gives us tools for this. Generics can handle dependency injection without dyn or Arc. Dynamic dispatch and shared ownership are also useful when the problem calls for them. But choosing a tool and needing that tool are different things.
In my projects, I was sometimes spending more time connecting abstractions than describing what the operation actually did.
I do not think that makes Clean or hexagonal architecture wrong. Their boundaries solve real problems. What I needed to question was whether the particular structure I was copying solved a problem in front of me.
So I stopped asking how to reproduce the implementation and started asking what I wanted to preserve from it.
The business rules still needed a place of their own. Database access still needed to be recognizable. And I wanted to follow an operation without opening a series of files that mostly forwarded calls.
A cart was enough to make the problem visible
Consider adding a product to a shopping cart.
I need to obtain the cart and the product, check availability, apply the cart’s rules, and save the result. As the operation grows, it may also need quantity limits, promotions, or other conditions.
The difficulty is not that there are several steps. It is losing track of which part decides what.
Does the cart decide whether a quantity is valid? Does the database function decide whether stock is sufficient? Does the handler calculate the discount? If those responsibilities drift, a small change becomes an investigation.
I wanted to open the operation and recognize its sequence. I also wanted the rules to remain business rules, rather than turning them into conditions hidden inside database access.
That became the starting point for Domain–Flow–Effects, or DFE.
Why I chose different names
I could have kept calling the pieces services, usecases, and repositories. Those are valid names. But for me, they came with habits.
When I wrote “service,” I tended to think about an interface and its implementation before I had established whether I needed either. When I wrote “repository,” I was already thinking about the next abstraction.
Calling an operation a flow helped me ask a more useful question: what happens here?
Calling external access fx helped me keep the concrete work visible: this function reads a cart; that one updates it; another publishes an event.
The names do not enforce a good design. They help me avoid repeating a design by habit.
DFE gives those responsibilities four recognizable places:
domain/holds business concepts and rules, without I/O.flows/obtains data throughfx/db, applies rules, and returns a result in memory.fx/contains concrete functions for databases and other external systems.api/adapts the input, controls the transaction, saves the result, and coordinates external actions.
For the cart, the sequence is:
API opens the transaction
→ flow reads the data and applies the rules
→ API saves the returned cart through fx/db
→ API commits
→ API coordinates any subsequent external actions
Changing the cart in memory is not the same as persisting it. The flow prepares the result; API saves it. Keeping that distinction visible matters more to me than making every request pass through an identical chain of components.
A technical read with no business decision may go straight from API to fx/db. A calculation may need domain rules without any database access. I do not create four empty folders just to say that a project follows DFE.
The dependency is still there
A flow can call a concrete database function directly. I am not pretending that this removes coupling.
It means the flow knows how it obtains its data. If an incompatible function signature changes, the compiler helps me find the affected calls. It does not tell me whether the query returns the right data or whether the business rule is correct.
That choice also affects testing. Domain rules can be tested on their own. Flows that query a database need integration tests against a real database; testing the complete operation also means checking the write performed by API.
If I later need interchangeable implementations or a stronger boundary, a trait may be the right solution. DFE does not forbid it. I just want the abstraction to address a concrete need rather than complete a structure I chose in advance.
What I want to keep discussing
DFE comes from backend work: receiving requests or jobs, obtaining information, applying rules, and persisting changes. I am not proposing it as a structure for every Rust program, or suggesting that a team should migrate a working system just to adopt it.
What I want is simpler: to find the rule, follow the operation, and see where the system interacts with the outside world.
I am still refining the proposal. If a case shows that a separation makes the code harder to understand, I want to examine that case, not defend the folders.
I have written a DFE manifesto and a practical guide that explain the principles and how to apply them, with code examples and the limits of the approach. This article tells the story behind those decisions; the guide shows how they fit together in code.
The proposal is open to discussion. I will also publish the documentation and examples on GitHub, where you can follow the discussions, ask questions, and suggest changes. If you try DFE, I would like to hear where it helps and where it gets in your way.
Adapted and translated from my original Spanish article with AI assistance.