Loading page...Загружаем страницу...正在加载页面...
AtlasRepo

How we bill each event exactly once

说明与用途

通过将计费计数器更新和幂等检查点合并为单个 DynamoDB 事务,并使用首写者机制防止重复计费,系统保证每个事件仅计费一次。

问题

Ensuring idempotent billing for each event in a distributed system where the meter, ledger, and storage live in separate databases, so that crashes or retries do not cause double‑charging or lost charges.

结果

By consolidating the meter update and idempotency checkpoint into a single DynamoDB transaction and using a first‑writer‑wins claim, the system guarantees exactly‑once billing even in the face of crashes or retries, eliminating double‑charges and lost charges.

工作流

  • 1. Event arrives at the front door and is enqueued (returns 202).
  • 2. Queue worker claims the event with a conditional PutItem (attribute_not_exists(pk)).
  • 3. Worker performs a TransactWriteItemsCommand that: a) updates the free‑usage counter with a conditional expression, and b) sets billingAcceptedAtMs on the ledger item if status is INSERTING.
  • 4. If the transaction succeeds, the event is archived to S3 and the analytics row is written to ClickHouse.
  • 5. On retry, the worker reads the ledger; if billingAcceptedAtMs exists, it skips re‑billing and recovers the event data.
  • 6. All steps are idempotent and recoverable, ensuring exactly‑once billing.

证据

The meter and the checkpoint cannot diverge because they were never two writes.

来源

attribute_not_exists(pk) makes this a first-writer-wins gate for all time.

来源

  • 记录日期: 2026-09-05T19:00:07.566Z