Repository navigation
Page the sales API 100 sales at a time - #9202
Merged
Merged
Conversation
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.
gumclaw
marked this pull request as ready for review
November 17, 2026 19:51
TastelintThe 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
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
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Part of antiwork/gumroad-private#4761
What
GET /v2/salesreturns 100 sales per page instead of 10, and the API docs say so.Why
A seller with 59,425 sales walked their own history with
gumroad sales list --alland got their IP rate-limited mid-run: Cloudflare 1015, Raya5bc697d6e41c519, after 6,836GET /v2/salesrequests, 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::SubscribersControlleralready 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 carrynext_page_key, andMAX_DEPRECATED_PAGEstays derived fromRESULTS_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):Page 1 →
sales_on_page: 100, more_pages: true. Page 2 (page_keyfrom the response) →sales_on_page: 30, more_pages: false. Onmainthe same 130 rows take 13 requests at 10 per page.Docs sentence on the live local page (
http://localhost:3000/api#get-/sales):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:455is 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.RESULTS_PER_PAGEto 4 (stub_const) so the fixtures stay cheap; the offset-ceiling example now derives its page number fromMAX_DEPRECATED_PAGEinstead of hardcoding the value that 10 per page happened to produce.Risk
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.mdand 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.