Skip to content

Page the sales API 100 sales at a time - #9202

Merged
gumclaw merged 1 commit into
mainfrom
gumclaw/gp4761-sales-page-size
Nov 17, 2026
Merged

gumclaw merged 1 commit into
mainfrom
gumclaw/gp4761-sales-page-size

Conversation

@gumclaw

@gumclaw gumclaw commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Part of antiwork/gumroad-private#4761

What

GET /v2/sales returns 100 sales per page instead of 10, and the API docs say so.

-  RESULTS_PER_PAGE = 10
+  RESULTS_PER_PAGE = 100

Why

A seller with 59,425 sales walked their own history with gumroad sales list --all and got their IP rate-limited mid-run: Cloudflare 1015, Ray a5bc697d6e41c519, after 6,836 GET /v2/sales requests, every one of them a 200 until the zone rule "6000 requests / 1 hour" tripped. At 10 sales per page that history is ~5,942 requests, so a single walk of a large account spends the entire hourly edge budget on pagination alone. At 100 it is ~594.

100 is what Api::V2::SubscribersController already returns, so this brings the sales endpoint in line with its sibling rather than inventing a new number. The page-key contract does not change: responses still carry next_page_key, and MAX_DEPRECATED_PAGE stays derived from RESULTS_PER_PAGE, so the offset ceiling follows.

Evidence — this branch, lane 0, seeded QA seller with 130 sales

Two requests instead of thirteen (localhost:3000, curl -H "Authorization: Bearer …" /v2/sales):

sales page size demo

Page 1 → sales_on_page: 100, more_pages: true. Page 2 (page_key from the response) → sales_on_page: 30, more_pages: false. On main the same 130 rows take 13 requests at 10 per page.

Docs sentence on the live local page (http://localhost:3000/api#get-/sales):

api docs page_key row

page_key (optional) - A key representing a page of results. It is given in the response as next_page_key. Each page holds up to 100 sales. Before this change the row said nothing about how many sales a page holds.

Tests

  • CI=1 DISABLE_SPRING=1 bin/rspec spec/controllers/api/v2/sales_controller_spec.rb → all examples pass.
  • spec/controllers/api/v2/sales_controller_spec.rb:455 is new: it asserts a full page comes back and that the documented size is the shipped one, so the docs cannot drift from the constant again.
  • The pagination example stubs RESULTS_PER_PAGE to 4 (stub_const) so the fixtures stay cheap; the offset-ceiling example now derives its page number from MAX_DEPRECATED_PAGE instead of hardcoding the value that 10 per page happened to produce.

Risk

  • Responses are up to 10x larger per request: on the seeded seller above, a 100-sale page is 145 KB, about 1.5 KB per sale, so a page was roughly a tenth of that before. That is the intended trade — 10x fewer round trips — and it matches the subscribers endpoint that already returns 100.
  • Additive-compatible for clients: next_page_key, field shapes and ordering are unchanged; only the number of rows per page moves.

The paired CLI change (antiwork/gumroad-cli#243) quotes the new page size in skills/gumroad/SKILL.md and stops the CLI from retrying into a spent budget; it works against the API as deployed today, so the two can land in either order.

Marketing: exempt (changes how many rows one API page returns plus one docs sentence; nothing to announce) @ 8fe8494

Premerge review: clean @ 8fe8494


AI disclosure: produced by an autonomous Gumclaw agent (Hermes; model deepseek-v4.1-flash-1017) from the instruction "Yes fix both", after tracing a seller's Cloudflare 1015 back to the page size. The evidence above was captured from this branch running locally.

A page of /v2/sales costs the caller one HTTP request, so a seller with 60k sales
spent ~6,000 requests reading their own history at 10 per page — enough on its own
to exhaust the edge's 6,000 requests/hour per-IP budget and get the IP blocked with
a 1015 mid-walk. 100 matches the subscribers API and turns that walk into ~600
requests.
@tastelint

tastelint Bot commented Nov 17, 2026

Copy link
Copy Markdown

Tastelint

The shots look fine to ship. The docs row now says each page holds up to 100 sales, and it reads in line with the other parameter rows. The first image is a terminal frame, not UI, so there's nothing to review there.

@gumclaw
gumclaw merged commit 539b1e2 into main Nov 17, 2026
61 checks passed
@gumclaw
gumclaw deleted the gumclaw/gp4761-sales-page-size branch November 17, 2026 19:52
gumclaw added a commit that referenced this pull request Nov 19, 2026
## Symptom

`GET /v2/sales` (`Api::V2::SalesController#index`) issues queries **per
sale** that `SALES_API_PRELOADS` does not cover. Sentry groups
GUMROAD-293 / 29C / 29E / 29F report it as
`performance_n_plus_one_db_queries` on this endpoint; the newest span
tree (production, one `GET /v2/sales`) is 76 of 77 spans in the
database, dominated by two shapes repeated once per sale:

- `SELECT licenses.* FROM licenses WHERE licenses.purchase_id = N LIMIT
1`
- `SELECT 1 AS one FROM base_variants INNER JOIN variant_categories ...
WHERE variant_categories.link_id = N ... LIMIT 1`

`#9202` took `RESULTS_PER_PAGE` from 10 to 100, so each
of these runs ~10x per page.

## Mechanism

`Purchase#as_json(version: 2)` reads these per purchase, and no preload
covered any of them:

| Read | Path |
| --- | --- |
| `license` | `license_json` → `linked_license` → `license_key` →
`uses_license_key?` → `variant_content_permits_license_key?` →
`has_product_level_rich_content?` (`link.alive_variants.empty?`) |
| `merchant_account` | `web_csv_payment_processor` →
`charged_using_stripe_connect_account?` |
| `price` | `subscription_duration` → `price_for_recurrence` (`price`,
and `subscription.price` → `last_payment_option.price`) |
| `subscription.original_purchase` | the subscription walk in
`true_original_purchase` / `original_purchase` |
| `true_original_purchase.product_review` |
`alive_original_product_review` — the existing `:product_review` preload
cannot reach it, because on a subscription sale the review hangs off the
original subscription purchase, not off the charge |

`alive_original_product_review` was also computed twice per sale
(`json[:product_rating]` and the web-CSV `review` column), walking
`subscription → true_original_purchase → product_review` twice.

## Change

- `SALES_API_PRELOADS`: added `:license`, `:merchant_account`, `:price`,
`{ subscription: [{ last_payment_option: :price }, :original_purchase, {
true_original_purchase: :product_review }] }` and `link:
:alive_variants`.
- `Purchase::Reviews#alive_original_product_review` is now memoized per
purchase, so a sale computes it once.

`{ variant_attributes: :prices }` was tried and dropped:
`variant_extra_cost` reads `variant.prices.alive.is_buy`, and a scoped
read bypasses a preloaded collection, so that preload only added a
query.

## Evidence

`CI=1 DISABLE_SPRING=1 bin/rspec
spec/controllers/api/v2/sales_controller_spec.rb` — 116 examples, 0
failures, plus the two new query-budget examples below, which fail on
this change reverted (`licenses` expected 1, got 2; `product_reviews`
expected 2, got 3).

Query counts per request for the same fixtures at two page sizes (the
point is that the fixed column does not move with the page):

| page of licensed digital sales | base, 4 | base, 10 | fixed, 4 |
fixed, 10 |
| --- | --- | --- | --- | --- |
| total queries | 46 | 64 | 36 | 36 |
| `licenses` | 4 | 10 | 1 | 1 |
| `merchant_accounts` | 4 | 10 | 1 | 1 |
| `exists?` probes on `base_variants` | 4 | 10 | 0 | 0 |

| page of subscription sales | base, 4 | base, 10 | fixed, 4 | fixed, 10
|
| --- | --- | --- | --- | --- |
| total queries | 84 | 156 | 71 | 113 |
| `product_reviews` | 5 | 11 | 2 | 2 |
| `payment_options` | 4 | 10 | 1 | 1 |
| `purchases` (`original_purchase` / `true_original_purchase`) | 9 | 21
| 3 | 3 |

## Non-coverage

- Tiered-membership `variant_extra_cost` still reads
`variant.prices.alive.is_buy` once per variant per sale; the scoped read
cannot be served by a preload (measured: `prices` 5 → 11 across the two
subscription page sizes).
- On subscription pages, `users`, `links`, `offer_codes` and
`purchase_offer_code_discounts` still grow with the page (measured) —
different reads from the reported culprit, not addressed here.
- `show`, `mark_as_shipped`, `refund`, `revoke_access` serialize a
single purchase, so they are not N+1 surfaces and were left alone.
- The `link.alive_variants` preload costs one query on pages whose
products are never licensed (measured), which the per-sale probe it
removes pays back on licensed pages.
- The N+1 is not fixed until this deploys; Sentry group state after
deploy is the confirmation.

Fixes antiwork/gumroad-private#4804

Premerge review: clean @ c28c906

Marketing: exempt (bug fix) @ c28c906

Co-authored-by: gumclaw <gumclaw@users.noreply.github.com>

This branch was successfully deployed

1 active deployment
preview/gumclaw-gp4761-sales-page-size — 8fe8494f Deployed Nov 17, 2026 by gumroad-buildkite
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

working Someone is actively working this — don't duplicate effort

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants