Rate Limiting GraphQL APIs by Calculating Query Complexity
Shopify's GraphQL Admin API rate-limits by calculated query cost instead of request count: clients get 50 points per second up to a 1,000-point bucket, and each query is priced by static analysis before execution. Objects, interfaces and unions cost 1 point, and scalars and enums are free. Connections cost 2 plus the number of requested objects, and mutations cost 10 to reflect side effects. Responses report requested and actual cost in an extensions object, and any unused requested cost is refunded. Shopify found calculated cost correlates linearly with execution time, which makes server load predictable and rewards clients for requesting only the data they need.
Related posts
- Shopify’s journey to faster breadth-first GraphQL execution
Shopify Engineering hardMasterShopify built GraphQL Cardinal, a breadth-first GraphQL execution engine that calls each field resolver once with the full set of objects at that level instead of once per object. Depth-first execution, used by graphql-ruby and graphql-js, multiplies per-field overhead from tracing hooks, authorization and dataloader promises by response breadth. Cardinal builds a typed execution tree, runs a bottom-up lookbehind planning pass inspired by Grafast, and executes generation by generation with flat object/result sets in a queue-driven loop rather than recursion. Large list queries ran up to 15x faster with 90% less memory and saved over 4s at P50. The tradeoffs are that errors are inlined and located by a final depth traversal, and that migrating a monolith from one-in/one-out resolvers requires an incremental bridge.
- Querying Strategies for GraphQL Clients
Shopify Engineering mediumAdeptGraphQL client queries tend to grow slower and harder to roll out as features accumulate, and Shopify's Orders & Fulfillments web and mobile teams work through a progression of strategies against a sub-one-second mobile page load goal. Starting from a single products query, they add cursor-style pagination with page-size parameters and hasNextPage, and gate new fields behind feature flags using @include/@skip directives. Chaining queries on remote flags is costly because it serializes round-trips, so it should be avoided or moved app-wide. Splitting non-paginated parts such as filters, permissions and banners into parallel queries enables faster loads, partial rendering and fewer merge conflicts, provided performance tripwires and server-side monitoring are in place.
- What today's software owes to Bronze Age clocks
PlanetScale mediumAdeptLeaky buckets, descended from Bronze Age water clocks, remain a cheap admission-control mechanism: a bucket stores only its last level and timestamp, so its current level can be computed lazily as max(0, level_then − drain_rate × elapsed). Shopify's GraphQL API shows the pattern in practice, charging an estimated query cost up front and refunding or charging the difference afterward. Postgres and MySQL lack built-in admission control, unlike Oracle Resource Manager and SQL Server Resource Governor, so PlanetScale's Database Traffic Control adds token-bucket resource budgets to Postgres. Each budget has a server-share refill rate, a burst limit in whole-server seconds, a per-query limit and a maximum concurrent workers setting, with costs estimated from query plans and history. PlanetScale recommends running a new budget in warning mode before switching it to blocking.
- pg_graphql: A GraphQL extension for PostgreSQL
Supabase mediumAdeptpg_graphql is an open-source PostgreSQL extension, written in C and SQL, that reflects a GraphQL schema from existing tables and resolves queries entirely inside the database. Supabase built it because its 1 GB free-tier VMs had no memory left for a separate GraphQL process like Graphile or Hasura. Each GraphQL request is transpiled into a single SQL statement that aggregates results as JSON. This avoids N+1 queries, respects row level security, and gives multi-operation requests transactional semantics. Pagination follows the Relay connection spec using keyset pagination, the API is one SQL function (gql.resolve) exposed over HTTP via PostgREST, and a benchmark shows about 2,205 requests/second on a 4-core machine.
- 10 Tips for Building Resilient Payment Systems
Shopify Engineering mediumAdeptTen lessons from Shopify's payment infrastructure for keeping payment systems reliable at scale. Start with low timeouts everywhere: about 1s to open a connection and 5s to read, write or query. Use circuit breakers via Semian, keyed per merchant country so one local acquirer outage stays isolated, and reason about capacity with Little's Law. Track the four golden signals, use structured logs with correlation IDs, and make retries safe with idempotency keys. ULID keys cut INSERT time by 50% versus UUIDv4. The rest covers reconciliation anomalies, load testing against a simulated benchmark gateway, checkout throttling, and incident management and retrospectives.