skip to content

In a large Rails HR monolith, when would you extract billing into a mountable engine, and what would that engine boundary not enforce on its own?

level: principalimportance: should knowfreq 20%

answer

  1. in-process gem, path: engines/billing
  2. own gemspec, own test/dummy
  3. constants stay globally reachable
  4. same database, same transactions
  5. one Gemfile.lock for all

basics

~20 s

Extract billing when it has a stable interface, one owner and few inbound calls; the engine adds a namespace, table prefix, routes, gemspec and dummy-app tests. It does not stop HR calling its constants or sharing its tables and transactions.

solid answer

~40 s

A mountable engine under `engines/billing`, loaded with `gem "billing", path:`, gives billing a `Billing::` namespace, `billing_` tables, its own mounted routes, a gemspec listing its dependencies and a test suite that boots against `test/dummy` without the HR app. That pays off when billing has a stable API, a team that owns it and changes that rarely touch HR code. It costs routing proxies, install:migrations runs, a dummy app to maintain and friction when HR needs to customise billing. What it does not enforce: Ruby constants are global, so HR can still call `Billing::Invoice` and billing can reach `::Employee`; both share one database, connection pool and transaction; and one Gemfile.lock pins shared gems. Dependency direction needs its own check, such as a test or CI rule.

code

ruby · 14 lines
ruby
# engines/billing/lib/billing.rb
require "billing/version"
require "billing/engine"

module Billing
  mattr_accessor :payer_class, default: "Employee"

  def self.payer
    payer_class.constantize
  end
end

# config/initializers/billing.rb (HR host)
Billing.payer_class = "Employee"

go deeper

for a junior

Know that an engine is still part of the same app process and database, even when mounted at its own path.

for a middle

List what the engine adds: namespace, table prefix, routes, gemspec and a dummy-app test suite.

for a senior

Name the gaps: global constants, shared database and transactions, one lockfile, one deploy. Show how an injected host class keeps billing testable.

for a principal

Decide from coupling data, sequence the move behind a public entry point, and pair the engine with a separately enforced dependency rule.

## What a billing engine actually buys Extracting billing into a mountable engine inside the HR repository (`engines/billing`, required with `gem "billing", path: "engines/billing"`) changes the **packaging** of the code, not its runtime. The concrete gains are Rails mechanics: - **Names.** `isolate_namespace Billing` puts every class under `Billing::`, prefixes tables with `billing_` and gives the engine its own helpers and route set, so billing's `InvoicesController` cannot collide with an HR one. - **Routes.** The host mounts it once, `mount Billing::Engine, at: "/billing"`; links cross the seam through the `billing` and `main_app` proxies, which makes every cross-boundary link visible in a grep. - **Dependencies.** The engine's `.gemspec` declares what billing needs (`add_dependency`), which documents billing's third-party surface separately from HR's. - **Tests.** The engine's suite boots `test/dummy`, a minimal host, so billing's tests run without loading the whole HR application, and a dependency on HR code shows up as a failure there. - **Schema ownership.** Billing's migrations live in the engine and reach the host through `billing:install:migrations`, with the `.billing` suffix marking them. ## When it is worth it The extraction pays off when most of these hold: 1. billing has a small, stable interface to HR (an employee reference, a payroll period) rather than reading dozens of HR models; 2. one team owns billing and changes it on its own schedule; 3. you want billing's tests and dependency list to stay honest as the monolith grows; 4. there is a realistic chance billing becomes a shared gem or later a separate service. It is premature when billing's boundaries are still moving: every refactor then crosses the engine seam, pays the proxy and migration overhead, and fights the override rules. A plain namespace in the app (`app/models/billing/`) is the cheaper first step. ## What the engine boundary does not enforce | Assumption | Reality in an engine | |---|---| | HR cannot reach billing internals | `Billing::Invoice` is a global constant; any host class can query it | | Billing does not depend on HR | Engine code can reference `::Employee`; only the dummy-app tests notice | | Data is separated | Same database, same connection pool; cross-table joins and foreign keys still work | | Failures are isolated | Same process: an exception, a slow query or memory growth in billing affects HR requests | | Dependencies are independent | One `Gemfile.lock` resolves both, so billing cannot pin a different version of a shared gem | | Deploys are independent | The engine ships inside the HR app's release | In other words, the engine gives you a place to draw the line, not a guard on it. Teams that rely on `isolate_namespace` alone typically find, a year later, HR controllers calling `Billing::Invoice.where(...)` directly. ## Making the boundary hold - **Inject host classes.** Billing reads `Billing.payer_class` (a `mattr_accessor` set to `"Employee"` by the host) instead of naming `Employee`, so the dummy app can supply its own payer model. - **Publish an entry point.** A small set of public objects, such as a `Billing::Charges` service, is what HR calls; everything else is treated as internal by convention and checked by a test or a CI rule. - **Watch transactions.** Calls across the seam often share one transaction today; if billing may become a service later, avoid relying on that atomicity. - **Keep overrides out.** If HR needs to decorate billing classes, add an extension point to billing instead. ## Costs to budget - **Tooling overhead.** Generators, the console and tests for billing now run from `engines/billing`, and developers switch between two `bin/rails` contexts. - **Cross-seam links.** Every HR page that links into billing uses the `billing` proxy, and every billing layout link to HR uses `main_app`. - **Schema workflow.** Each billing migration takes an extra `billing:install:migrations` step in the host. - **Customisation friction.** HR-specific tweaks to billing screens become view overrides or decorators unless billing offers extension points. ## A decision frame for a lead - Start with the coupling data: how many HR files reference billing constants and the reverse. - If the count is small and shrinking, an engine locks in the shape and gives billing its own test loop. - If it is large, first introduce the public entry point inside the app, then move the code once the references go through it. - Revisit after a few releases: if no one benefits from the separate suite and gemspec, folding the engine back is cheap.

  • How would you detect HR code reaching into billing internals?
    Rails offers nothing for this. Add a test or CI check that scans host code for references to billing constants outside the published entry point, and run billing's own suite against `test/dummy` so any reference from billing to HR classes fails there.
  • Does a billing engine make a later move to a separate service easy?
    Easier, not easy. The namespace, routes and migrations are already grouped, but in-process calls, shared transactions and direct joins to HR tables must all become network calls or copied data first.

  • isolate_namespace stops host code from calling engine classes
  • An engine gives billing its own database connection
  • Engines let billing pin gem versions different from HR
  • Extracting to an engine isolates billing failures from HR requests
  • Engines are always worth it in a large Rails app