Stop Recounting the Last Hour of Payments on Every API Request

Why the merchant total looked right until the refundA dashboard refresh asks a busy database for the same last-hour sum at every till. A cac

profile_img

Why the merchant total looked right until the refund

A dashboard refresh asks a busy database for the same last-hour sum at every till. A cached result avoids one query, but the first refund reveals the real requirement: the number must explain which payments contributed and when it was last correct.

A merchant opens its dashboard during a sale. Every refresh runs the same rolling total against the payments database. Caching the last query helps until the next reversal arrives, when its answer becomes wrong.

The turning point

Keep PostgreSQL authoritative. Stream committed payment changes and compute a merchant-level read model once, rather than making each client scan the ledger. This is a derived aggregate, not a row-for-row copy of the database.

Use signed movements, not a sum of every after-image. A sale changing from pending to captured contributes once. If a correction changes a captured amount from 100 to 80, apply a delta of minus 20 or recompute the affected bucket. A merchant moving between regions may also change the grouping key.

What actually happens to one record?

 Illustrative event or assumption Expected consequence
10:00 capture INR 100Merchant total +100
10:05 refund INR 30Merchant total 70
10:06 replay refund IDTotal remains 70
Window expiresAdvance buckets, even on idle merchants

Read models must be cheap to discard

A reversal requires a business rule. For a net total, reverse a previously counted payment once, in the correct currency and time bucket. A late reversal may change an earlier bucket. Summing every CDC after-image as a new payment would inflate revenue.

Valkey is the fast serving copy, not the financial book of record. When freshness falls outside the agreed limit, display the last calculation time or decline a live estimate. Do not silently route every reader back to the primary during a lag incident.

The architecture behind the decision

Operating architecture

Figure 1. Data travels across the top; the management plane observes and governs changes separately.

Build per-merchant, per-currency aggregates over bounded time buckets. Emit a versioned absolute total rather than a blind increment. Schedule bucket expiry even for merchants with no new events so old sales leave the rolling window.

The design trade-off

Publish the value with its calculation time and window boundary. An API can show a stale indicator when the stream is behind. If the value is needed for settlement, use the reconciled ledger instead of treating a fast dashboard as the system of record.

With 60 one-minute buckets per merchant and currency, estimate active merchant-currency pairs rather than total accounts. Frequent writes to one merchant key can dominate sink traffic. Benchmark skew, serialization cost and downstream version comparisons.

What OSS Manager changes, and what it does not

The ledger team owns posting and reversal semantics. The API team owns acceptable staleness, cache misses and stale-result display. Data engineering owns bucket revision order and the rebuild boundary.

Use a compatible sink or consumer to conditionally replace older versions. Rebuild into a new key namespace, compare it with a ledger query at the same cutoff, then switch readers; do not flush the live namespace.

Use the reviewed service configuration to identify endpoints and operating roles. The business rule belongs to the job and its downstream contract, not to a green infrastructure status.

The failure modes worth rehearsing

 Exercise Observable pass condition
Sale 100, reversal 30 in same currencyNet displayed is 70
Replay the earlier result versionOlder result cannot overwrite the correction
Advance beyond the reporting windowOld bucket disappears even without a new sale

Replay into a versioned Valkey namespace. Record the database snapshot position used as the baseline, catch up to a known cutoff, and reconcile by merchant, currency and bucket. Keep the previous namespace available until readers are verified.

A human operating model

Monitor CDC lag, Flink watermark age and the freshness actually returned by the API. A healthy Valkey ping proves nothing about the age of the number customers see.

Where this pattern earns its place

Retail: net sales after returns. Marketplaces: seller activity across channels. Banking: provisional merchant activity, explicitly differentiated from settled balances.

Companion technical solution: Serving Rolling Merchant Activity with PostgreSQL, Flink and Valkey.

Technical reference and scope

Apache Flink documentation:

https://nightlies.apache.org/flink/flink-docs-stable/docs/dev/table/concepts/dynamic_tables/

The timelines are illustrative, not customer measurements. Confirm connectors, state compatibility, metrics and available OSS Manager actions against the release selected for the pilot.