← Technical notes
Illustrative2 diagrams

Multi-level revenue splits with a dry-run preview

Example domain: a brand sells through distributors, dealers and retailers. Each level keeps the difference between its rate and the rate of the level below it.

Based on patterns I've built in production (details under NDA). The example domain below is illustrative.

TL;DR

Illustrative note: percent-plus-fixed fees walked down a partner chain, with the same function used for a read-only preview and for the final write.

live trace · Preview vs commitfrom the original diagram
Ops Preview Split function Commit DB 01draft rates 02compute levels 03share per level and leftover, no write 04mark sale finalised when thestate allows it 05compute with live rates 06insert split lines, uniqueper sale and partner
  1. 01
    Ops → Previewdraft rates
  2. 02
    Preview → Split functioncompute levels
  3. 03
    Preview → Opsshare per level and leftover, no write
  4. 04
    Commit → DBmark sale finalised when the state allows it
  5. 05
    Commit → Split functioncompute with live rates
  6. 06
    Commit → DBinsert split lines, unique per sale and partner
01

The problem

Fee rules changed from one flat rate to percent + fixed, split down a chain of levels. Ops needed to change rates safely, without surprises in real partner shares.

02

How it works

  1. Walk the chain from the top rate downward. Each level's own rate is clamped between 0 and the level above; the level earns the difference.
  2. A missing or inactive level earns nothing and does not shift its gap to others.
  3. Percent and fixed parts are split the same way; money is computed cumulatively and rounded once per level, so rounding never overpays.
  4. Whatever is left belongs to the platform; negative leftovers are clamped to zero.
  5. Preview runs the same split function with draft rates and returns every level's share without writing anything.
  6. Commit only runs if the sale is still in a finalisable state (a guarded update), then inserts split rows backed by a unique constraint, so a duplicate commit is rejected.
03

Migration approach

Added percent and fixed columns beside the legacy rate columns, backfilled old rows into one part, dual-wrote both during transition, and never recalculated already-finalised records.

All diagrams

Other views

Tap a table to see what it links to.

PARTNER
PARTNER_RATEdecimal rate_percentdecimal rate_fixedtimestamp effective_from
SALE
SPLIT_LINEdecimal basis_percentdecimal own_percentdecimal diff_amount
  • PARTNER parent of PARTNER 1 → many
  • PARTNER has PARTNER_RATE 1 → many
  • SALE produces SPLIT_LINE 1 → many
  • PARTNER earns SPLIT_LINE 1 → many

← Technical notes on the home page