Skip to content

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.

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:

  1. Open Chaos in the project, and select New profile.

  2. Enter the Profile name Rate limited.

  3. Enter the Endpoint pattern ^/v1/orders. Left empty, the profile throttles every mock in the project.

  4. Turn on Error injection.

  5. Enter 50 in Share of calls (%) and 429 in Status code.

  6. Enter {"error":"rate_limited"} in Response body.

  7. Select Add header, and enter Retry-After and 30.

  8. Select Add header again, and enter Content-Type and application/json.

  9. 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:

Terminal window
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/orders
done
200
429
429
200
429
200

The mix changes on every run. A throttled answer carries the header:

Terminal window
curl -si https://orders-api.mockhub.dev/v1/orders | grep -i -E '^(HTTP|retry-after)'
HTTP/2 429
retry-after: 30

For a test that must see the 429 every time, use a rate of 1, which the app shows as 100.

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:

  1. Open the mock. Under What it answers, enter OK as the Response name of the open response, then select Response and A blank response to add a second one. Name it Rate limited, set its Status code to 429, select Add a response header and enter Retry-After and 30, and replace its body with {"error":"rate_limited"}.

  2. Open How a response is chosen and, beside Answer with, select A sequence.

  3. Choose OK for Step 1, select Add a step and choose OK for Step 2, then select Add a step again and choose Rate limited for Step 3.

  4. 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:

200
200
429
200
200
429

To 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.

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.

  • 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-After does not change the next answer.
  • Retry-After is 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 429 on its own. See Requests get 429 Too Many Requests.

Report a problem with this page