Split an existing app
A monolith is hard to split because you decide everything at once: the boundary, the new service and the network. Here you draw the boundary first, and the network can wait.
Say OrderService calls PricingService directly, and pricing should be able to leave.
1. Give pricing its own frond. Move the service and its entities to fronds/pricing/,
then write a handler for what other fronds may call. It is a handler like any other: it says
what goes out, and it checks what it has to check.
// fronds/pricing/handlers/PricingHandler.ts
import PricingService from '../services/PricingService.js';
export default class PricingHandler {
constructor(private pricing: PricingService) {}
/** What this cart comes to. */
async total(cartId: string): Promise<number> {
return this.pricing.total(cartId);
}
}
2. Call it through its facade. The caller asks for the type and
adds an await. Everything still runs in one process, with no network.
// fronds/order/services/OrderService.ts
import type { Facade } from '@fougere/core';
import type PricingHandler from '@fronds/pricing/handlers/PricingHandler.js';
export default class OrderService {
constructor(private pricing: Facade<PricingHandler>) {}
async checkout(cartId: string): Promise<number> {
return this.pricing.total(cartId);
}
}
3. Move it when it is worth it. One line in fronds: is the whole move,
and the code above does not change:
fronds: { pricing: { remote: 'http://pricing:4610' } },
From then on, each call costs a network call. If you never take this step, you still have a
monolith with cleaner boundaries, and it cost you almost nothing. demos/crossing-cost runs
the same code with 0 hops or 2.
Three things show you where a boundary does not hold:
- What crosses must be plain data. A class instance with methods, or a function, is refused at the call, in one process as across two, so pass an id or plain fields.
- A join between two fronds' tables stops working once the rows leave. Read through the facade, or keep a copy.
- Two writes that must stand or fall as one stay in one process.
Togetherholds them across two fronds, and refuses a frond placed elsewhere, so those two move together or not at all.
What changes when a frond moves
The code does not change, but a call can now time out, and after a timeout you do not know
whether a write happened. It is the question a database commit already asks, and the answer is
the same: say which operations belong together, rather than catching network errors one by one.
Two writes in one process go in Together. Writes that span processes
should be safe to repeat, so that finishing them is just doing them again.
Before you move a frond, look at who calls it and writes elsewhere in the same operation. Fougere will point at those callers for you; that check is being built.