-
Notifications
You must be signed in to change notification settings - Fork 2
rfc: space-diff table refactoring #78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
BravoNatalie
wants to merge
4
commits into
main
Choose a base branch
from
rfc/refactor-space-diff
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+104
−0
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
29db332
rfc: initial proposal for refactoring the space-diff table
BravoNatalie d988b3c
fix: add adjustments based on the comments
BravoNatalie dae4e99
fix: adjust usage calculation timeout solution
BravoNatalie 36c2c21
fix: remove usage timeout discussion and clarify different timeframe …
BravoNatalie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| # RFC: Space Diff Deduplication | ||
|
|
||
| ## Authors | ||
|
|
||
| - [Natalie Bravo](https://github.com/bravonatalie), [Storacha Network](https://storacha.network/) | ||
|
|
||
| ## Language | ||
|
|
||
| The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in [RFC2119](https://datatracker.ietf.org/doc/html/rfc2119). | ||
|
|
||
| ## Introduction | ||
|
|
||
| The current `space-diff` table has accumulated structural issues that impact billing and system reliability due to duplicate entries. This RFC proposes changes to prevent duplicate diffs and simplify long-term maintenance. | ||
|
|
||
| ### Problem Statement | ||
|
|
||
| Past bugs caused multiple diffs to be written for the same cause (e.g. failed uploads). This resulted in duplicated diffs that inflate usage, slow down queries and create "ghost" usage for spaces that should be empty after deletion. | ||
|
|
||
| This behavior should be **structurally impossible** going forward. | ||
|
|
||
| ## Current `space-diff` usage model | ||
|
|
||
| The `space-diff` table is the single **source of truth** for billing. It is written to by different sources depending on the protocol. | ||
|
|
||
| ### Source A: Modern Blob Protocol | ||
|
|
||
| ``` | ||
| blob/accept OR blob/remove → blob-registry.register() | ||
| 1. allocation table entry (legacy compatibility) | ||
| → TransactWrite { | ||
| 2. blob-registry table entry (primary storage) | ||
| 3. space-diff table entry (billing) | ||
| } | ||
|
|
||
| ``` | ||
|
|
||
| - **Location**: `upload-api/stores/blob-registry.js` | ||
|
|
||
| ### Source B: Legacy Store Protocol | ||
|
|
||
| Deprecated, but still operational for existing clients. | ||
|
|
||
| ``` | ||
| store/add OR store/remove receipt → UCAN stream → ucan-stream-handler → space-diff table | ||
|
|
||
| ``` | ||
|
|
||
| - **Location**: `billing/functions/ucan-stream.js` | ||
|
|
||
| ## Proposal | ||
|
|
||
| ### Short-term solution: Deduplicate on write using a GSI | ||
|
|
||
| Add a **GSI on `cause`** to the existing `space-diff` table. Before inserting a new diff, query the GSI to check whether a diff with the same `cause` already exists. If it does, skip the write. | ||
|
|
||
| This approach: | ||
|
|
||
| * Prevents new duplicates without changing the table schema | ||
| * Can be deployed quickly with minimal risk | ||
| * Does not require a migration or dual-write strategy | ||
|
|
||
| **Limitation:** This is a best-effort guard — it adds a read-before-write cost and does not structurally prevent duplicates (a race condition is still theoretically possible). | ||
|
|
||
| ### Medium-term solution: TTL-based archival to Glacier | ||
|
|
||
| Add a **TTL attribute** to the `space-diff` table so that items older than 1 year are automatically expired by DynamoDB. Before expiration, use a **DynamoDB Streams + Lambda** pipeline to archive expired items to S3 Glacier. | ||
|
|
||
| This approach: | ||
|
|
||
| * Keeps the table lean over time, improving query performance | ||
| * Reduces storage costs for historical data | ||
| * Supports retention policies without manual cleanup | ||
|
|
||
| ### Long-term solution: New table with structural uniqueness | ||
|
|
||
| To guarantee uniqueness and prevent future duplication at the schema level: | ||
|
|
||
| * Use **`cause` as the sort key (SK)** of the `space-diff` table | ||
| * This makes it impossible to insert two diffs for the same `(space, cause)` pair | ||
|
|
||
| #### Open design concern | ||
|
|
||
| Using `cause` as the SK removes natural chronological ordering. | ||
|
|
||
| **Proposed solution** | ||
|
|
||
| * Add a **GSI with a timestamp-based sort key** | ||
|
|
||
| This enables: | ||
|
|
||
| * Efficient chronological queries | ||
| * Time-based pagination | ||
| * Retention policies (e.g. deleting data older than 1 year) | ||
|
|
||
| The additional cost is acceptable, especially since older diffs can be safely deleted after the retention window. | ||
|
|
||
| #### Migration plan (high level) | ||
|
|
||
| 1. Create a **new `space-diff` table** with: | ||
| * Correct PK design | ||
| * `cause` as SK | ||
| * GSI for timestamp-based queries | ||
| 2. Enable dual-writes: on each diff event, write to both the existing table and the new table. Keep all readers (usage, reporting, billing) pointed at the existing table during January. | ||
| 3. Cut over later: switch usage reporting and billing reads to the new table; keep the existing table as read-only historical storage. | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉