Contents Lesson 11 of 16

10 min read · professional

Quota in production

By the end of this lesson you will have a proxy that gives up your allowance in the order this lesson writes down — hidden panels first, a manual refresh last — read from the one count every server instance shares, with tests that walk the count from empty to the limit.

Build a Dashboard gave you a refresh budget for a cockpit you were sitting in front of. Deployed, it spends whether or not you are there — and things you did not think of spend it too.

What consumes quota when nobody is looking

Four sources, roughly in order of how surprising they are:

  • A tab left open. The most common by far. Someone opens the cockpit on Friday and leaves it. Build a Dashboard's pause-when-hidden rule is what stops this, and it stops being optional here.
  • Your own monitoring, if the health check touches the upstream API. Design it not to; that is why the previous unit's endpoint costs nothing.
  • Crawlers. A public URL gets bots within days. They will not run your JavaScript, but they will hit any endpoint they can find, and your proxy is an endpoint.
  • Anyone you shared the link with, spending your allowance at whatever rate they like.

Every one of these is spending a currency you priced in Build a Dashboard at 1 call per live symbol, 5 per screen, 10 per fundamentals request.

Budget in three parts

Split the daily allowance deliberately:

Interactive (you using it)   60%
Background (alerts, health)  30%
Reserve (manual refresh)     10%

The reserve is the part people omit and then regret. When you hit the limit, you want a deliberate "refresh now" to still work, because that is the moment you actually need the number.

Enforce the split at the proxy, not in the page. The page is a suggestion; the server decides. Same rule as clamping limit in Build a Screener, applied to a budget.

It holds only while the page is yours. What a request is for comes from the browser, so anyone who can reach the proxy can call every request a manual refresh and walk past every stage, and a store scoped by user guards the documents, not the proxy. If you added a sign-in, the proxy should answer 401 to a visitor without the owner's session and send nothing upstream; ours does that in a commit of its own, found by the final review. Without a sign-in, the URL stays private.

Degrade in a fixed order

Decide once, while calm, what stops first:

  1. Background refresh of panels nobody is looking at.
  2. Automatic refresh entirely; manual only.
  3. Alerts, with a notice saying so.
  4. Serve cached data, labelled, and refuse new fetches.

Written down, this is a policy. Undecided, it is whatever the code happens to do at 3pm on a busy day, and it will not be what you would have chosen.

Cache is the cheapest thing you own

A quote cached for a minute costs one call instead of sixty for a page refreshed every second. A daily series cached until tomorrow costs one call a day.

You built the cache in Build a Dashboard for consistency. In production it is also the largest single lever on cost, and the two motivations point the same way — which is usually the sign of a good design.

Try it now

Leave your deployed cockpit open in a background tab overnight and read the meter in the morning. Whatever that number is, it was spent on a page nobody looked at — and if it is not zero, your pause-when-hidden rule is not working.

The prompt

Nothing counts calls per kind of request, so the split becomes stopping points on the one shared count: each kind stops at its own share of the day, and the last tenth is left for a refresh you ask for by hand. Mock mode on, plan mode on, and paste:

Turn this degradation order into code. In src/lib/quota-policy.ts,
admit(kind, used, limit, cost) for four kinds of request: background
(a panel nobody is looking at) stops at 50% of the daily limit, auto
(a visible panel's timer) at 75%, alert at 90%, and manual (a "refresh
now" the person pressed) at the limit itself. A request goes out only
if the count after it, priced with estimateCalls, stays within its
kind's share. An unknown count admits only manual. Tests first. The
page marks each request with an x-budget header; the proxy treats a
missing or unknown one as background and refuses with 402, a sentence
naming the stage, and the stage in the body, so the page can tell it
from EODHD's own 402 for a spent day and still send a refresh I press.
Reuse the cached count the daily ceiling already reads, and let this
policy replace that ceiling. Done means npm test, npm run typecheck,
npm run lint and npm run check:leak green; show me the diff.

A good answer adds src/lib/quota-policy.ts and its test, changes src/lib/proxy.ts, the market store, which sends a batch as the most urgent kind among the panels that asked for it, and the one function every browser request goes through (ours is getJson in src/lib/proxy-fetch.ts), and adds no request: the count it reads is the one the proxy already holds. The page reads the proxy's 402 as a failure of its own kind, held, not as the day spent, or Build a Dashboard's wall would block the refresh by hand as well.

Check it yourself

  1. The tests walk the count from empty to the limit and show each kind refused in the lesson's order, with a manual refresh still served after every other kind has stopped.
  2. In mock mode, set apiRequests in fixtures/user.json to 16 of its 20 and wait a minute, because the proxy reads the count through a one-minute cache and until then still sees the old one: a panel's timer is refused with a sentence naming the stage, and a refresh by hand that fits in the four calls left, such as Retry on the quote panel, is served. Refresh for the whole cockpit stays off: Build a Dashboard's wall already holds back a refresh that does not fit.
  3. With the fixture still at 16 and the minute passed, curl -s -i localhost:3000/api/proxy/eod/X carries no header and is refused as background. Put the fixture back to 0.
  4. With the network panel open, hide the tab for a few minutes: no request leaves while it is hidden.
Compare with ours
// src/lib/quota-policy.ts: the degradation order, written down as data
export const STOP_AT = { background: 0.5, auto: 0.75, alert: 0.9, manual: 1 } as const;
export type Kind = keyof typeof STOP_AT;

export function kindOf(header: string | null): Kind {
  return header !== null && Object.hasOwn(STOP_AT, header) ? (header as Kind) : "background";
}

export function admit(kind: Kind, used: number | null, limit: number | null, cost = 1): boolean {
  if (used === null || limit === null) return kind === "manual";   // spending blind is how the day goes
  return used + cost <= limit * STOP_AT[kind];   // priced: one 500-ticker request cannot skip every stage
}

The full state at the end of this unit is on the starter's branch ref/ship-your-tool/u3.

Checkpoint

npm test && npm run typecheck
git status          # the files you expect, and no .env file
git add -A
git commit -m "Quota gives way in a written order: hidden panels first, a manual refresh last"

Then answer this lesson's questions.