Rate limits and errors
How requests are limited, what protects the server, and every error code.
The API is built so that clients cannot slow the game down. Requests are limited, queued with a cap, cached, and kept off the game's own database threads.
Rate limits
Limits use token buckets. A bucket refills at the per-second rate and holds up to the burst size. A request takes one token. With no token left the answer is 429.
| Bucket | Applies to | Per second | Burst | Setting |
|---|---|---|---|---|
| Address | Every request, before the key is checked | 30 | 60 | limits.per-ip |
| Read | status, servers, kits, arenas, queues, parties, tournaments, drain | 20 | 40 | limits.read |
| Heavy | players/*, leaderboards/*, matches*, config reads | 5 | 10 | limits.heavy |
| Write | drain/start, drain/stop, reload, config PUT and PATCH | 2 | 6 | limits.write |
| Wrong keys | Failed authentication, per address | 1 per 5 s | 10 | fixed |
The read, heavy and write buckets are per key and separate from each other. Many reads do not use up the write bucket. The address bucket is shared by every key behind one address.
A limited request:
HTTP/1.1 429 Too Many Requests
Retry-After: 1
Content-Type: application/json; charset=utf-8
{"ok":false,"error":{"code":"rate_limited","message":"this key is over its heavy limit"}}Retry-After is in seconds. Wait that long, then retry.
Behind a reverse proxy every request comes from 127.0.0.1. Set trust-forwarded-for: true so the limit follows the real client address.
Other protections
| Protection | Value | Result when exceeded |
|---|---|---|
| Worker threads and queue | threads 4, queue 64 | 503 busy, Retry-After: 1 |
| Database reads at once | database-concurrency 3, 2 second wait | 503 busy |
| Request body | max-body-kb 256 | 413 too_large |
| Address length | 512 characters, query 2048 | 414 uri_too_long |
| Query parameters | 32 | extra ones are ignored |
| Config batch | 200 changes | 400 bad_request |
| Wait for the game thread | 5 seconds (30 for a reload) | 503 unavailable |
| Wait for the database | 8 seconds | 503 unavailable |
Other things that keep it stable:
- The API has its own small database pool. It never uses the threads that save player data.
- Game state is read on the server's main thread (the global region on Folia), never from a worker, so reads cannot corrupt it.
- Lists, leaderboards, stats and match queries are cached for
cache-seconds(5 by default). Fifty clients asking for the same leaderboard cost one query. Setcache-seconds: 0for live data, at the cost of more queries. Reload, config edits and remote drain requests clear the cache. - A failing request answers
500with a generic message and never shows internals. The server keeps running. - If the port cannot be opened, MineDuels logs a warning and carries on.
Handling 429 and 503 in a client
Retry after the time in Retry-After, with a limit on attempts:
async function call(path, options = {}, attempts = 4) {
for (let i = 0; i < attempts; i++) {
const res = await fetch(process.env.MD_URL + path, {
...options,
headers: { Authorization: `Bearer ${process.env.MD_KEY}`, ...(options.headers || {}) },
});
if (res.status !== 429 && res.status !== 503) return res;
const wait = Number(res.headers.get('retry-after') || 1);
await new Promise((r) => setTimeout(r, wait * 1000));
}
throw new Error('still limited after ' + attempts + ' attempts');
}import time, requests
def call(method, path, **kw):
for _ in range(4):
r = requests.request(method, BASE + path, headers=HEADERS, **kw)
if r.status_code not in (429, 503):
return r
time.sleep(int(r.headers.get("Retry-After", 1)))
raise RuntimeError("still limited")To stay under the limits, poll at most once per cache-seconds. Data changes no faster than that anyway.
Error format
{ "ok": false, "error": { "code": "not_found", "message": "no player Nobody is known" } }The code is stable and meant for programs. The message is for people and may change.
Error codes
| Status | code | Meaning |
|---|---|---|
| 400 | bad_request | A parameter or the body is invalid. The message names it. |
| 401 | unauthorized | No key, or a key that does not exist. |
| 403 | forbidden | The key's scope is too low. |
| 403 | protected | The setting or file cannot be changed through the API. |
| 404 | not_found | No such endpoint, player, kit, match, server or file. |
| 405 | method_not_allowed | The path exists but not for this method. The Allow header lists the right ones. |
| 413 | too_large | The body is larger than max-body-kb. |
| 414 | uri_too_long | The address is too long. |
| 422 | invalid_changes | A config batch has an invalid item. Nothing was written. |
| 422 | invalid_file | A config file on disk is not valid YAML. |
| 429 | rate_limited | Over a limit. Wait Retry-After seconds. |
| 500 | internal | An unexpected error. Details are in the server console. |
| 500 | reload_failed | A config edit was written but the reload failed, so the files were put back. |
| 503 | busy | The server is handling as much as it should. Retry after Retry-After. |
| 503 | unavailable | The game or the database did not answer in time. |
Examples:
curl -s -H "Authorization: Bearer $MD_KEY" "$MD_URL/matches?limit=500"{"ok":false,"error":{"code":"bad_request","message":"limit must be from 1 to 100"}}curl -s -X DELETE -H "Authorization: Bearer $MD_KEY" "$MD_URL/status"{"ok":false,"error":{"code":"method_not_allowed","message":"DELETE is not allowed here; use GET"}}Response headers
| Header | Value |
|---|---|
Content-Type | application/json; charset=utf-8 |
Cache-Control | no-store |
X-Content-Type-Options | nosniff |
Retry-After | Seconds, on 429 and 503 |
Allow | Allowed methods, on 405 |
WWW-Authenticate | Bearer, on 401 |
Access-Control-Allow-* | Only when cors-origin is set |