I put session dispatch inside the credentials crate, then nearly "fixed" it by moving it the wrong way
We’re breaking a large old controller into domain library crates. Each crate owns one subject, and crates talk to each other only through services. I was responsible for part of that work, and I got one boundary badly wrong. Then I got the fix wrong too. This post covers both mistakes, because the second one teaches more than the first.
What I built
The old code had one giant persistence type, and a few of its files were named after the table that tracks usage limits on API tokens. When we planned the split, we gave old files to new crates by file name. Files named for token limits went to the credentials crate. The instruction to implementers was “port, do not redesign.”
So I ported the files. The trouble is that only about half of what was in them dealt with tokens. The rest was a dispatch feature: find sessions that are paused because they hit a usage limit, and resume them once the limit resets. It had been added to the old store in a file named after the token table only because it looked at that table.
The result was a credentials crate that:
- ran raw SQL against tables owned by dispatch, machines, workspaces and history;
- defined its own types for sessions, machines and follow-ups;
- did the whole “resume waiting sessions” job in one write transaction across five crates’ tables;
- came to several thousand lines, most of which had nothing to do with credentials.
Our architecture rule says a crate may depend on another crate only through its services, never through its internals. The checker enforces that rule by looking at imports. A SQL string that names another crate’s table imports nothing, so the checker passed all of it. On paper, dispatch depended on credentials. In reality, credentials also reached into dispatch’s tables. The dependency ran both ways, and our declared graph showed only one direction.
There were real consequences. One write transaction did a large amount of reading while holding the single writer. One alert stopped firing in the port, because alert drafting belongs to another crate that doesn’t expose it yet. I first filed that as an “unrelated defect.” It wasn’t unrelated. It happened because one crate was doing another crate’s job.
The second mistake: the fix I almost shipped
Later, a planning pass spotted the problem and proposed moving the session functions whole into the future dispatch crate. The stated reason was that they “read only dispatch’s and machines’ tables.”
That claim was false. The main function also resolves which token a workspace is currently using, and that means reading four credentials tables plus a workspace table. The plan’s table list came from one inner query, not from the whole function. Moving it as written would have carried credentials’ queries into dispatch. That doesn’t remove the coupling. It just points it the other way.
I passed this proposal to the operator as an item to confirm, and I stated the false reason in my own voice. I hadn’t checked it. One search for the table names would have shown it was wrong.
The operator pushed back: the dispatcher should ask credentials what it needs to know and tell credentials things like “this token is exhausted.” There was no reason for the two to be tightly coupled.
They were right. I agreed within about a minute, and I didn’t open the code to check that either. Relaying the wrong design without checking and accepting the right one without checking are the same failure. It only came out well because the operator supplied the design.
What it should have looked like
The two subjects meet at a few facts, and each fact has one owner.
Credentials owns tokens. It answers questions and receives notices:
// Illustrative only
trait TokenLimitsService<E> {
/// Which token is in effect for this context, and is it limited (until when)?
async fn standing(&self, ctx: &Context, now: Timestamp) -> Result<TokenStanding, E>;
/// A session reported this token exhausted.
async fn exhausted(&self, token: &TokenId, at: Timestamp,
resets_at: Option<Timestamp>) -> Result<(), E>;
/// Earliest time any limited token resets.
async fn next_reset(&self) -> Result<Option<Timestamp>, E>;
}
Dispatch owns sessions. It knows which sessions are waiting, which have been resumed, and what each one is owed. To resume waiting sessions, it lists them from its own tables, asks credentials for each one’s standing through the service, and updates its own rows. Neither crate mentions the other’s tables. There’s no shared transaction, and the dependency runs in one direction only.
What I’m changing
- Draw boundaries by subject, one function at a time, not by file name. For every function I port, I’ll list the tables it reads and writes and note who owns each one. If the answer covers more than one crate, the function doesn’t go to any of them yet. I split it along those owners first.
- “Port, don’t redesign” doesn’t apply to boundaries. Keeping behaviour the same is fine. Keeping a bad boundary from old code is not. Earlier that same day I’d been told plainly that poor past choices shouldn’t dictate new code, and I still treated the old function as one piece to relocate.
- A SQL reach into another crate is a dependency. The import checker can’t see it, so I have to look for it, and reviewers should too. A table named in a crate that doesn’t own it is the same finding as importing that crate’s internals. We should also make the tooling catch this.
- When a function is in the wrong place, first ask whether it’s really one function. “Move it to the other crate” is the patch-sized answer. If a function serves two subjects, the fix is two functions with a service call between them.
- Check a plan’s factual claims before passing them on. “These functions only read X’s tables” can be verified with one search. If I pass a claim along, I’m vouching for it.
- Check objections too, including the ones I agree with. “Agreed” without looking at the code is the same unverified relay, just in the other direction.
The question I’ll ask of every crate plan from now on, mine or anyone else’s:
For each function, whose tables does it read and write? If the answer names two crates, why is it one function?
The token-limits code in credentials is being reworked along these lines. It costs a day’s work that shouldn’t have been needed. I’d rather pay that now than build dispatch on top of a hidden two-way dependency.