System Design Atlas

Subscription billing, usage and service entitlements

04 / 10Worked example

Change commands and retries

How does usage-event identity prevent duplicate billing or changed content?

Scale alone does not ensure consistent retry behaviour. This proposed API binds the role, business object, change payload and expected state to each new intent. An idempotency key identifies the same intent after a response is lost.

Starting point Use 27.78 invoices/s as a separate test profile.

Assumptions for the example case

Original fictional example design: one monthly contract in EUR, positive integer usage units, a fixed tariff version per period, one invoice and one payment order, and separate correction records. Figures and quality targets are illustrative test assumptions. Tax law, multiple currencies, prorated tariff changes and debt collection are outside the scope.

Retries and new intent

Usage and payment identity

Usage envelopeN-161 · 100 · Timestamp
Nutzungsumschlag → EreignisregisterIdentical / changed / new
Event registerPeriod or correction
Identical N-161 → 100N-161 with 101 → conflictIdentical Z-161 → same collection

N-161 deduplicates 100 units; Z-161 binds 1,200 cents; changed content under the same key produces a conflict.

N-161 arrives again after a timeout. Compare its identifier and the 100-unit payload together. The retry still represents 100 units.

Proposed domain API contracts

  1. POST/usage-events

    Accept positive usage once.

    Input
    Authenticated usage source, N-161, V-161, event time, 100 integer units and a key.
    Result
    Assign the event once to a half-open billing period. Reject zero or negative units; changed content under the same identity produces a conflict.
    Retry behaviour
    An identical operation by the same authorised role on the same object, using the same key and canonical payload, returns the stored original result. A changed payload under that key is rejected. External result identifiers are durably deduplicated after authentication; new intents check the current version again.
  2. POST/billing-periods/{id}/close

    Bill a precisely defined usage snapshot.

    Input
    Billing operations, P-10, V-161, T-2, expected period version and finalisation key.
    Result
    Atomically fixed ingestion watermark and immutable R-161; late usage follows a separate correction path.
    Retry behaviour
    An identical operation by the same authorised role on the same object, using the same key and canonical payload, returns the stored original result. A changed payload under that key is rejected. External result identifiers are durably deduplicated after authentication; new intents check the current version again.
  3. POST/invoices/{id}/collect

    Collect payment for a finalised invoice.

    Input
    Authorised billing operations, R-161 and a key; the server reads 1200 cents and EUR from the invoice.
    Result
    202 with Z-161 and zahlung_offen; a retry creates no new payment collection.
    Retry behaviour
    An identical operation by the same authorised role on the same object, using the same key and canonical payload, returns the stored original result. A changed payload under that key is rejected. External result identifiers are durably deduplicated after authentication; new intents check the current version again.
  4. POST/billing-payment-results

    Accept an evidenced payment-collection outcome.

    Input
    Authenticated payment service, result identifier, Z-161, R-161, 1200 cents, EUR and final status.
    Result
    Persist in the inbox before acknowledging. Success yields bezahlt; definite rejection is separate; uncertainty remains open.
    Retry behaviour
    An identical operation by the same authorised role on the same object, using the same key and canonical payload, returns the stored original result. A changed payload under that key is rejected. External result identifiers are durably deduplicated after authentication; new intents check the current version again.
  5. POST/usage-corrections

    Bill late usage separately.

    Input
    Billing operations, N-161, finalised P-10, a reason and correction key.
    Result
    One referenced 200-cent correction record when finalisation wins; R-161 remains unchanged.
    Retry behaviour
    An identical operation by the same authorised role on the same object, using the same key and canonical payload, returns the stored original result. A changed payload under that key is rejected. External result identifiers are durably deduplicated after authentication; new intents check the current version again.
  6. GET/subscriptions/{id}/entitlement

    Read the contract, invoice and entitlement.

    Input
    The subscriber for V-161 or authorised customer service.
    Result
    Tariff version, invoice reference, payment evidence and explained entitlement with its agreed deadline.
    Retry behaviour
    Repeating a read creates no business effect; the returned current state may change between requests.

Recognise repeated usage events

Recognise repeated usage events

Compare the event identifier and the 100 units together.

N-161 is delivered again after a timeout.

A retry still represents 100 units.

Alternative considered
Counting arrivals measures transmissions rather than usage.
Trade-off
Sources need stable identifiers.
Reassess
New sources have other event contracts.

Terms in this step

Recognise repeated usage events
A retry still represents 100 units.

Try it yourself

N-161 is sent with 100 units, another 100, then 101. Predict the total and each response.

A hint for your attempt

Use the specific entities and conditions from this step. The two solution parts distinguish the decision from its evidenced outcome.

Compare with a possible solution
First part of the solution
The two identical messages together result in 100 units.
Second part of the solution
101 units under the same identifier produces a conflict. Correcting the usage requires a separate business record.

Apply to your project

Which corresponding assumption in your project would you check first when new sources have different event contracts?

Step 5 examines how to establish an atomic finalisation boundary.