Skip to content

Lesson 9 of 9 · 23m

Fitness Functions and Operational Feedback Loops

Turn architectural constraints into automated checks, production signals, and review habits that prevent slow structural decay.

Architecture rarely collapses in a dramatic moment. It decays through reasonable local decisions: one shared table here, one retry there, one temporary feature flag that becomes permanent. Fitness functions are how you make decay visible. They are automated checks or measurements that tell you whether the system still satisfies the properties you claimed mattered.

Architectural intent must become executable

"Services should be independent" is not a check. "No service may query another service's relational tables" is. "The system should be fast" is not a check. "Checkout confirmation p95 must remain under 900 ms at current load" is.

Useful categories include dependency rules, data ownership, latency budgets, error budgets, schema compatibility, security invariants, event contract compatibility, and operational coupling.

Static checks catch boundary erosion early

Run these in CI because architecture feedback is cheapest before merge:

# architecture/rules.py
RULES = [
    ForbiddenImport(source="domain", targets=["django", "sqlalchemy", "requests"]),
    ForbiddenImport(source="billing", targets=["identity.storage"]),
    MaxFanIn(package="shared_kernel", limit=8),
]

Even a simple dependency report is useful if the team reviews it. The goal is not to build a cathedral of custom tooling. It is to make violations noisy while they are still small.

Runtime checks catch emergent behavior

Some properties only exist in production: consumer lag, duplicate side effects, retry storms, schema drift, and cross-service call depth. Expose measurements with clear ownership.

histogram_quantile(
  0.95,
  sum(rate(event_consumer_lag_seconds_bucket{consumer="provisioning"}[5m])) by (le)
)

Alerting on lag is not enough. Connect the signal to the architectural consequence: if provisioning lag exceeds five minutes, checkout promises become false and support sees phantom failures. That is a business degradation, not merely a queue metric.

Contract tests protect distributed change

Provider and consumer contracts should test what the consumer actually relies on, not a ceremonial generated schema. For events, verify required fields, type stability, nullability, enum evolution, and compatibility windows. For HTTP APIs, test auth behavior and semantic errors as well as shape.

def test_invoice_closed_contract_v1(event_schema):
    sample = load_json("contracts/billing/invoice_closed.v1.json")
    assert event_schema.validate(sample)
    assert sample["type"] == "billing.invoice_closed.v1"
    assert isinstance(sample["data"]["amount_minor"], int)

Run consumer expectations against producer changes before deployment. Contract tests that live in a repository nobody runs are decorative.

Review drift on a cadence

Automation cannot judge every tradeoff. Hold a short architecture review when signals move: incident trends, change coupling, slow tests, deployment coordination, repeated rollback, rising cloud cost, or developer onboarding pain. Bring evidence, not opinions.

A practical review table:

signal                         threshold                 current   owner action
cross-module forbidden imports 0                         2         billing lead
checkout p95                   <900ms                    1.2s      checkout team
projection lag p95             <120s                     45s       data team
temporary flags older than 90d 0                         7         platform lead
shared kernel fan-in           <=8 packages              11        architecture

The output should be a decision: accept the risk, fund the fix, change the threshold, or update the architecture because reality changed.

Do not game the metric

Any fitness function can become a target and then stop measuring the thing you cared about. Pair metrics: speed with correctness, independence with duplication, delivery frequency with rollback rate. When people optimize the number while damaging the property, change the measurement rather than blaming the team for responding to incentives.

Field notes

  • Start with three checks tied to painful incidents, not twenty checks copied from a book.
  • Architectural metrics need owners. An unowned dashboard is background noise.
  • Put architecture checks in the same CI path as tests; optional jobs become ignored jobs.
  • Measure build and test feedback loops. Slow developer feedback is an architectural force because it encourages unsafe batching.
  • When a check fires constantly but nobody acts, either fix the system or admit the rule is wrong. Alarm decay is architecture decay.

Was this lesson helpful?

Sign in to rate