Simulate rate limiting with 429 and Retry-After
Before you begin
- Role
- Admin or higher to define a chaos profile and Editor or higher to start one or build a response sequence.
- You need
- A project with a mock for the endpoint to throttle, such as GET /v1/orders
To a client, a rate limit is a 429 Too Many Requests with a Retry-After header saying when to try again. MockHub has no separate rate-limit setting for mocks; it sends that answer in one of two ways:
| Approach | Which calls get the 429 | Suits |
|---|---|---|
| A chaos profile, error injection | A share of calls you choose, at random, on every mock it covers, for a set time | Checking that a client backs off and recovers |
| A response sequence | A fixed position in one mock’s sequence of responses, such as every third call | A repeatable test that expects the 429 on a known call |
The examples throttle GET /v1/orders on https://orders-api.mockhub.dev.
With a chaos profile
Section titled “With a chaos profile”In the project’s configuration document, the profile is one entry in chaosProfiles. rate is the share of calls, between 0 and 1:
{ "name": "Rate limited", "endpointPattern": "^/v1/orders", "errorInjection": { "rate": 0.5, "statusCode": 429, "body": "{\"error\":\"rate_limited\"}", "headers": { "Content-Type": "application/json", "Retry-After": "30" } }}To add it to a project’s document, see Edit a project’s configuration in the browser. Add the entry to the exported document rather than applying it on its own: applying a document replaces the project’s whole configuration.
To create the same profile in the app:
-
Open Chaos in the project, and select New profile.
-
Enter the Profile name
Rate limited. -
Enter the Endpoint pattern
^/v1/orders. Left empty, the profile throttles every mock in the project. -
Turn on Error injection.
-
Enter
50in Share of calls (%) and429in Status code. -
Enter
{"error":"rate_limited"}in Response body. -
Select Add header, and enter
Retry-Afterand30. -
Select Add header again, and enter
Content-Typeandapplication/json. -
Select Create profile. The profile reads HTTP 429 on 50% of calls.
Either way, a profile acts only while it runs. Select Start on it, choose how long in Run for, and select Start chaos; the profile is marked Live until the time runs out or you select Stop. The configuration document never starts or stops a profile.
Call the endpoint a few times:
for i in 1 2 3 4 5 6; do curl -s -o /dev/null -w '%{http_code}\n' https://orders-api.mockhub.dev/v1/ordersdone200429429200429200The mix changes on every run. A throttled answer carries the header:
curl -si https://orders-api.mockhub.dev/v1/orders | grep -i -E '^(HTTP|retry-after)'HTTP/2 429retry-after: 30For a test that must see the 429 every time, use a rate of 1, which the app shows as 100.
With a response sequence
Section titled “With a response sequence”A sequence makes the throttling predictable. In the configuration document, the mock holds two responses and a responseSelection whose steps name them (bodies shortened here):
{ "name": "List orders", "request": { "method": "GET", "url": "/v1/orders" }, "responses": [ { "key": "ok", "name": "OK", "response": { "statusCode": 200, "headers": { "Content-Type": "application/json" }, "body": "{\"orders\":[]}" } }, { "key": "limited", "name": "Rate limited", "response": { "statusCode": 429, "headers": { "Content-Type": "application/json", "Retry-After": "30" }, "body": "{\"error\":\"rate_limited\"}" } } ], "responseSelection": { "mode": "sequence", "steps": ["ok", "ok", "limited"], "onEnd": "repeat" }}To build it in the app:
-
Open the mock. Under What it answers, enter
OKas the Response name of the open response, then select Response and A blank response to add a second one. Name itRate limited, set its Status code to429, select Add a response header and enterRetry-Afterand30, and replace its body with{"error":"rate_limited"}. -
Open How a response is chosen and, beside Answer with, select A sequence.
-
Choose
OKfor Step 1, select Add a step and chooseOKfor Step 2, then select Add a step again and chooseRate limitedfor Step 3. -
Leave After the last step at Start over from the first, and select Save mock.
The same loop now answers the same way every time, counting from the start of the sequence:
200200429200200429To throttle only the first call and let every retry through, make the steps Rate limited then OK, and choose Keep answering with the last.
A sequence remembers its position between test runs. To start it over, open Scenarios and select Reset all, or run mockhub scenarios reset at the start of each run; an AI assistant does the same with reset_scenarios.
Result
Section titled “Result”Your client receives 429 with Retry-After: 30: on a random share of calls while the chaos profile runs, or on every third call to the mock. Each one is in the request logs with the status the client received.
Limits
Section titled “Limits”- Neither approach counts calls per client or per period. A chaos profile decides afresh on each call, and a sequence counts every call to its mock, from any caller. Waiting for
Retry-Afterdoes not change the next answer. Retry-Afteris sent as the text you entered. MockHub does not compute it or count it down.- A chaos profile affects requests a mock matched and, in Proxy unmatched, requests forwarded to the upstream; it never affects Record everything. A project runs one profile at a time, so starting this one stops any other.
- An injected answer carries only the profile’s body and headers, not the mock’s, and its body is sent as written, without templating.
- Endpoint pattern is compared with the path each mock is written with, not with the request, and matches anywhere in it unless it starts with
^. - Try it shows neither approach: it ignores chaos profiles and plays only the response you have open, not the sequence.
- A simulated 429 is a real request: it counts against the workspace’s plan limits, and insights count it among 4xx answers. Error-rate alert rules count only 5xx answers, so they ignore it.
- When the workspace itself is over a limit, MockHub answers
429on its own. See Requests get 429 Too Many Requests.