When a code boundary becomes a service
When building an app, we often separate responsibilities to make the code easier to understand and change.
As those boundaries become clearer, creating separate services can feel like the natural next step. But a useful boundary in code does not automatically need to become a separate service.
Consider a small shop. When a customer places an order, we need to record the order and reserve the stock. Both changes can happen in one database transaction:
await db.transaction(async (tx) => {
await orders.create(tx, { orderId, productId, quantity });
await inventory.reserve(tx, { orderId, productId, quantity });
}); Orders and inventory have separate interfaces and implementations. They share a transaction for this operation. If there isn’t enough stock, reserve throws, and the transaction rolls back the order too.
Now consider extracting inventory into a service that owns its own data:
await inventoryClient.reserve({ orderId, productId, quantity });
await orders.create(db, { orderId, productId, quantity }); The code is still short. But a successful reservation followed by a failed order leaves stock reserved without an order. We need a way to recover. If the reservation’s response is lost, we also need to retry without reserving twice.
In the first example, the order and reservation commit together or neither does. Orders and inventory still own separate responsibilities; sharing a transaction lets us combine them safely for this workflow.
Now suppose the business grows. A second app needs to reserve inventory, and perhaps a third. Each has its own workflow, but the reservation rules need to stay consistent across them.
A shared library can provide one implementation while keeping reservations within each app’s transaction. The apps need to be able to consume the same code, though, and changes reach each app when it updates the library. If the apps use different languages, sharing that implementation becomes less straightforward.
If these apps already talk to the same database (say Postgres), a database function offers another common interface. Each app can call the same reservation implementation within its own transaction, combining it with the changes its workflow requires.
An inventory schema can hold the stock and reservation tables. A PostgreSQL function can check the available stock, reduce it, and record the reservation. Our original inventory.reserve(tx, ...) becomes a wrapper around this call:
SELECT inventory.reserve($1, $2, $3); The arguments are the order ID, product ID, and quantity. The function runs within the caller’s transaction. If there isn’t enough stock, it raises an error. If the transaction rolls back, the reservation rolls back with it.
For this business, the function builds on infrastructure the apps already use. We get shared rules across languages and keep the ability to compose operations in one transaction, without deploying another application service.
There is still a shared contract to maintain. Changing the function changes behavior for every caller, so its interface and migrations need to remain compatible with the apps that use it.
If the business needs inventory to serve external partners or operate independently of the shop, those requirements could justify a service and the coordination it brings.
Separate responsibilities first. Choose where they run based on what the business already has and the independence it actually needs.