├── .github └── workflows │ ├── ghpages.yml │ ├── publish.yaml │ └── update.yaml ├── .gitignore ├── CONTRIBUTING.md ├── LICENSE.md ├── Makefile ├── README.md ├── docker-compose.yml ├── draft-ietf-httpapi-ratelimit-headers.md ├── issues.html ├── issues.js └── test_ratelimit.py /.github/workflows/ghpages.yml: -------------------------------------------------------------------------------- 1 | name: "Update Editor's Copy" 2 | 3 | on: 4 | push: 5 | paths-ignore: 6 | - README.md 7 | - CONTRIBUTING.md 8 | - LICENSE.md 9 | - .gitignore 10 | pull_request: 11 | paths-ignore: 12 | - README.md 13 | - CONTRIBUTING.md 14 | - LICENSE.md 15 | - .gitignore 16 | 17 | jobs: 18 | build: 19 | name: "Update Editor's Copy" 20 | runs-on: ubuntu-latest 21 | steps: 22 | - name: "Checkout" 23 | uses: actions/checkout@v4 24 | 25 | - name: "Setup" 26 | id: setup 27 | run: date -u "+date=%FT%T" >>"$GITHUB_OUTPUT" 28 | 29 | - name: "Caching" 30 | uses: actions/cache@v4 31 | with: 32 | path: | 33 | .refcache 34 | .venv 35 | .gems 36 | node_modules 37 | .targets.mk 38 | key: i-d-${{ steps.setup.outputs.date }} 39 | restore-keys: i-d- 40 | 41 | - name: "Build Drafts" 42 | uses: martinthomson/i-d-template@v1 43 | with: 44 | token: ${{ github.token }} 45 | 46 | - name: "Update GitHub Pages" 47 | uses: martinthomson/i-d-template@v1 48 | if: ${{ github.event_name == 'push' }} 49 | with: 50 | make: gh-pages 51 | token: ${{ github.token }} 52 | 53 | - name: "Archive Built Drafts" 54 | uses: actions/upload-artifact@v4 55 | with: 56 | path: | 57 | draft-*.html 58 | draft-*.txt 59 | -------------------------------------------------------------------------------- /.github/workflows/publish.yaml: -------------------------------------------------------------------------------- 1 | name: "Publish New Draft Version" 2 | 3 | on: 4 | push: 5 | tags: 6 | - "draft-*" 7 | workflow_dispatch: 8 | inputs: 9 | email: 10 | description: "Submitter email" 11 | default: "" 12 | type: string 13 | 14 | jobs: 15 | build: 16 | name: "Publish New Draft Version" 17 | runs-on: ubuntu-latest 18 | steps: 19 | - name: "Checkout" 20 | uses: actions/checkout@v4 21 | 22 | # See https://github.com/actions/checkout/issues/290 23 | - name: "Get Tag Annotations" 24 | run: git fetch -f origin ${{ github.ref }}:${{ github.ref }} 25 | 26 | - name: "Setup" 27 | id: setup 28 | run: date -u "+date=%FT%T" >>"$GITHUB_OUTPUT" 29 | 30 | - name: "Caching" 31 | uses: actions/cache@v4 32 | with: 33 | path: | 34 | .refcache 35 | .venv 36 | .gems 37 | node_modules 38 | .targets.mk 39 | key: i-d-${{ steps.setup.outputs.date }} 40 | restore-keys: i-d- 41 | 42 | - name: "Build Drafts" 43 | uses: martinthomson/i-d-template@v1 44 | with: 45 | token: ${{ github.token }} 46 | 47 | - name: "Upload to Datatracker" 48 | uses: martinthomson/i-d-template@v1 49 | with: 50 | make: upload 51 | env: 52 | UPLOAD_EMAIL: ${{ inputs.email }} 53 | 54 | - name: "Archive Submitted Drafts" 55 | uses: actions/upload-artifact@v4 56 | with: 57 | path: "versioned/draft-*-[0-9][0-9].*" -------------------------------------------------------------------------------- /.github/workflows/update.yaml: -------------------------------------------------------------------------------- 1 | name: "Update Generated Files" 2 | # This rule is not run automatically. 3 | # It can be run manually to update all of the files that are part 4 | # of the template, specifically: 5 | # - README.md 6 | # - CONTRIBUTING.md 7 | # - .note.xml 8 | # - .github/CODEOWNERS 9 | # - Makefile 10 | # 11 | # 12 | # This might be useful if you have: 13 | # - added, removed, or renamed drafts (including after adoption) 14 | # - added, removed, or changed draft editors 15 | # - changed the title of drafts 16 | # 17 | # Note that this removes any customizations you have made to 18 | # the affected files. 19 | on: workflow_dispatch 20 | 21 | jobs: 22 | build: 23 | name: "Update Files" 24 | runs-on: ubuntu-latest 25 | steps: 26 | - name: "Checkout" 27 | uses: actions/checkout@v4 28 | 29 | - name: "Update Generated Files" 30 | uses: martinthomson/i-d-template@v1 31 | with: 32 | make: update-files 33 | token: ${{ github.token }} 34 | 35 | - name: "Push Update" 36 | run: git push -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | lib -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | This repository relates to activities in the Internet Engineering Task Force 4 | ([IETF](https://www.ietf.org/)). All material in this repository is considered 5 | Contributions to the IETF Standards Process, as defined in the intellectual 6 | property policies of IETF currently designated as 7 | [BCP 78](https://www.rfc-editor.org/info/bcp78), 8 | [BCP 79](https://www.rfc-editor.org/info/bcp79) and the 9 | [IETF Trust Legal Provisions (TLP) Relating to IETF Documents](http://trustee.ietf.org/trust-legal-provisions.html). 10 | 11 | Any edit, commit, pull request, issue, comment or other change made to this 12 | repository is considered to be a "Contributions to the IETF Standards Process" 13 | (https://www.ietf.org/). 14 | 15 | You agree to comply with all applicable IETF policies and procedures, including, 16 | BCP 78, 79, the TLP, and the TLP rules regarding code components (e.g. being 17 | subject to a Simplified BSD License) in Contributions. 18 | 19 | 20 | ## Other Resources 21 | 22 | Discussion of this work occurs on the 23 | [HTTPAPI working group mailing list](https://mailarchive.ietf.org/arch/browse/httpapi/) 24 | ([subscribe](https://www.ietf.org/mailman/listinfo/httpapi)). In addition to 25 | contributions on GitHub, you are encouraged to participate in discussions there. 26 | 27 | You might also like to familiarize yourself with other 28 | [working group documents](https://datatracker.ietf.org/wg/httpapi/documents/). 29 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # License 2 | 3 | See the 4 | [guidelines for contributions](https://github.com/ietf-wg-httpapi/ratelimit-headers/blob/main/CONTRIBUTING.md). 5 | 6 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | LIBDIR := lib 2 | include $(LIBDIR)/main.mk 3 | 4 | $(LIBDIR)/main.mk: 5 | ifneq (,$(shell grep "path *= *$(LIBDIR)" .gitmodules 2>/dev/null)) 6 | git submodule sync 7 | git submodule update $(CLONE_ARGS) --init 8 | else 9 | git clone -q --depth 10 $(CLONE_ARGS) \ 10 | -b main https://github.com/martinthomson/i-d-template $(LIBDIR) 11 | endif 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Rate Limit fields for HTTP 2 | 3 | This is the working area for the HTTPAPI Internet-Draft, "Rate Limit header fields for HTTP". 4 | 5 | 6 | * [Editor's Copy](https://ietf-wg-httpapi.github.io/ratelimit-headers/#go.ratelimit-headers.html) 7 | * [Individual Draft](https://tools.ietf.org/html/draft-ietf-httpapi-ratelimit-headers) 8 | * [Compare Editor's Copy to Individual Draft](https://ietf-wg-httpapi.github.io/ratelimit-headers/#go.draft-ietf-httpapi-ratelimit-headers.diff) 9 | 10 | 11 | ## Building the Draft 12 | 13 | Formatted text and HTML versions of the draft can be built using `make`. 14 | 15 | ```sh 16 | $ make 17 | ``` 18 | 19 | This requires that you have the necessary software installed. See 20 | [the instructions](https://github.com/martinthomson/i-d-template/blob/master/doc/SETUP.md). 21 | 22 | 23 | ## Contributing 24 | 25 | See the 26 | [guidelines for contributions](https://github.com/ietf-wg-httpapi/ratelimit-headers/blob/master/CONTRIBUTING.md). 27 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | # This docker-compose allows testing the I-D 2 | # without installing all the tools locally. 3 | # 4 | # Just run: 5 | # docker-compose up test 6 | # 7 | # NB: the `user` parameter is useful to avoid 8 | # that docker runs as root and create files 9 | # with root privileges in your local folder. 10 | # This may not work outside Linux. 11 | version: "3.0" 12 | services: 13 | test: 14 | image: martinthomson/i-d-template 15 | volumes: 16 | - .:/code 17 | working_dir: /code 18 | user: >- 19 | $UID:$GID 20 | entrypoint: ["make"] 21 | -------------------------------------------------------------------------------- /draft-ietf-httpapi-ratelimit-headers.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: RateLimit header fields for HTTP 3 | abbrev: 4 | docname: draft-ietf-httpapi-ratelimit-headers-latest 5 | category: std 6 | 7 | ipr: trust200902 8 | area: Applications and Real-Time 9 | workgroup: HTTPAPI 10 | keyword: Internet-Draft 11 | 12 | stand_alone: yes 13 | pi: [toc, tocindent, sortrefs, symrefs, strict, compact, comments, inline, docmapping] 14 | 15 | venue: 16 | group: HTTPAPI 17 | type: Working Group 18 | home: https://datatracker.ietf.org/wg/httpapi/about/ 19 | mail: httpapi@ietf.org 20 | arch: https://mailarchive.ietf.org/arch/browse/httpapi/ 21 | repo: https://github.com/ietf-wg-httpapi/ratelimit-headers 22 | 23 | author: 24 | - 25 | ins: R. Polli 26 | name: Roberto Polli 27 | org: Team Digitale, Italian Government 28 | email: robipolli@gmail.com 29 | country: Italy 30 | - 31 | ins: A. Martinez 32 | name: Alejandro Martinez Ruiz 33 | org: Red Hat 34 | email: alex@flawedcode.org 35 | - 36 | ins: D. Miller 37 | name: Darrel Miller 38 | org: Microsoft 39 | email: darrel@tavis.ca 40 | 41 | entity: 42 | SELF: "RFC nnnn" 43 | 44 | normative: 45 | IANA: RFC8126 46 | HTTP: RFC9110 47 | PROBLEM: RFC9457 48 | 49 | informative: 50 | PRIVACY: RFC6973 51 | UNIX: 52 | title: The Single UNIX Specification, Version 2 - 6 Vol Set for UNIX 98 53 | author: 54 | name: The Open Group 55 | ins: The Open Group 56 | date: 1997-02 57 | --- abstract 58 | 59 | This document defines the RateLimit-Policy and RateLimit HTTP header fields for servers to advertise their quota policies and the current service limits, thereby allowing clients to avoid being throttled. 60 | 61 | --- middle 62 | 63 | # Introduction 64 | 65 | Rate limiting of HTTP clients has become a widespread practice, especially for HTTP APIs. Typically, servers who do so limit the number of acceptable requests in a given time window (e.g. 10 requests per second). See {{rate-limiting}} for further information on the current usage of rate limiting in HTTP. 66 | 67 | Currently, there is no standard way for servers to communicate quotas so that clients can throttle their requests to prevent errors. This document defines a set of standard HTTP header fields to enable rate limiting: 68 | 69 | - RateLimit-Policy: a quota policy, defined by the server, that client HTTP requests will consume. 70 | - RateLimit: the currently remaining quota available for a specific policy. 71 | 72 | These fields enable establishing complex rate limiting policies, including using multiple and variable time windows and dynamic quotas, and implementing concurrency limits. 73 | 74 | ## Goals {#goals} 75 | 76 | The goals of this document are: 77 | 78 | Interoperability: 79 | : Standardize the names and semantics of rate-limit headers to ease their enforcement and adoption. 80 | 81 | Resiliency: 82 | : Improve resiliency of HTTP infrastructure by providing clients with information useful to throttle their requests and prevent 4xx or 5xx responses. 83 | 84 | Documentation: 85 | : Simplify API documentation by eliminating the need to include detailed quota limits and related fields in API documentation. 86 | 87 | The following features are out of the scope of this document: 88 | 89 | Authorization: 90 | : RateLimit header fields are not meant to support authorization or other kinds of access controls. 91 | 92 | Response status code: 93 | : RateLimit header fields may be returned in both successful (see {{Section 15.3 of HTTP}}) and non-successful responses. This specification does not cover whether non Successful responses count on quota usage, nor does it mandates any correlation between the RateLimit values and the returned status code. 94 | 95 | Throttling algorithm: 96 | : This specification does not mandate a specific throttling algorithm. The values published in the fields, including the window size, can be statically or dynamically evaluated. 97 | 98 | Service Level Agreement: 99 | : Conveyed quota hints do not imply any service guarantee. Server is free to throttle respectful clients under certain circumstances. 100 | 101 | ## Notational Conventions 102 | 103 | {::boilerplate bcp14} 104 | 105 | The term Origin is to be interpreted as described in Section 7 of{{!WEB-ORIGIN=RFC6454}}. 106 | 107 | This document uses the terms List, Item and Integer from {{Section 3 of !STRUCTURED-FIELDS=RFC8941}} to specify syntax and parsing, along with the concept of "bare item". 108 | 109 | The term "problem type" in this document is to be interpreted as described in [PROBLEM]. 110 | 111 | # Terminology 112 | 113 | Quota: 114 | : A quota is an allocation of capacity used by a resource server to limit client requests. That capacity is measured in quota units and may be reallocated at the end of a time window. 115 | 116 | Quota Unit: 117 | : A quota unit is the unit of measurement used to measure the activity of a client. 118 | 119 | Quota Partition: 120 | : A quota partition is a division of a server's capacity across different clients, users and owned resources. 121 | 122 | Time Window: 123 | : A time window indicates a period of time associated to the allocated quota. 124 | 125 | Quota Policy: 126 | : A quota policy is implemented by the server to regulate the activity within a specified quota partition, quantified in quota units, over a defined time window. This activity is restricted to a predefined limit, known as the quota. Quota policies can be advertised by servers, but they are not required to be, and more than one quota policy can affect a given request from a client to a server. 127 | 128 | Service Limit: 129 | : A service limit is the currently remaining quota from a specific quota policy and, if defined, the remaining time before quota is reallocated. 130 | 131 | List: 132 | : A {{!STRUCTURED-FIELDS=RFC8941}} list of Items 133 | 134 | Item: 135 | : A {{!STRUCTURED-FIELDS=RFC8941}} item with a set of associated parameters 136 | 137 | # RateLimit-Policy Field {#ratelimit-policy-field} 138 | 139 | The "RateLimit-Policy" response header field is a non-empty List{{!RFC8941}} of Quota Policy Items ({{quotapolicy-item}}). The Item{{!RFC8941}} value MUST be a String{{!RFC8941}}. 140 | 141 | The field value SHOULD remain consistent over a sequence of HTTP responses. It is this characteristic that differentiates it from the [RateLimit](#ratelimit-field) field that contains information that MAY change on every request. The "RateLimit-Policy" field enables clients to control their own flow of requests based on policy information provided by the server. Situations where throttling constraints are highly dynamic are better served using the (RateLimit field)[{#ratelimit-field}] that communicates the latest service information a client can react to. Both fields can be communicated by the server when appropriate. 142 | 143 | Lists of Quota Policy Items ({{quotapolicy-item}}) can be split over multiple "RateLimit-Policy" fields in the same HTTP response as described in {{Section 3.1 of !STRUCTURED-FIELDS=RFC8941}}. 144 | 145 | ~~~ 146 | RateLimit-Policy: "burst";q=100;w=60,"daily";q=1000;w=86400 147 | ~~~ 148 | 149 | ## Quota Policy Item {#quotapolicy-item} 150 | A quota policy Item contains an identifier for the policy and a set of Parameters{{!RFC8941}} that contain information about a server's capacity allocation for the policy. 151 | 152 | The following parameters are defined: 153 | 154 | q: 155 | : The REQUIRED "q" parameter indicates the quota allocated by this policy measured in quota units. 156 | 157 | qu: 158 | : The OPTIONAL "qu" parameter value conveys the quota units associated to the "q" parameter. The default quota unit is "requests". 159 | 160 | w: 161 | : The OPTIONAL "w" parameter value conveys a time window. 162 | 163 | pk: 164 | : The OPTIONAL "pk" parameter value conveys the partition key associated to the corresponding request. 165 | 166 | Other parameters are allowed and can be regarded as comments. 167 | 168 | Implementation- or service-specific parameters SHOULD be prefixed parameters with a vendor identifier, e.g. `acme-policy`, `acme-burst`. 169 | 170 | This field MUST NOT appear in a trailer section. 171 | 172 | ### Quota Parameter {#ratelimitpolicy-quota} 173 | 174 | The "q" parameter value MUST be a non-negative Integer. The value indicates the quota allocated for client activity (measured in quota units) for a given quota partition. 175 | 176 | ### Quota Unit Parameter {#ratelimitpolicy-quotaunit} 177 | 178 | The "qu" parameter value conveys the quota units applicable to the quota ({{ratelimitpolicy-quota}}). The value MUST be a String. Allowed values are listed in the [RateLimit Quota Units registry](#ratelimit-quota-unit-registry). This specification defines three quota units: 179 | 180 | requests: 181 | : This value indicates the quota is based on the number of requests processed by the resource server. Whether a specific request actually consumes a quota unit is implementation-specific. 182 | 183 | content-bytes: 184 | : This value indicates the quota is based on the number of content bytes processed by the resource server. 185 | 186 | concurrent-requests: 187 | : This value indicates the quota is based on the number of concurrent requests processed by the resource server. 188 | 189 | ### Window Parameter {#ratelimitpolicy-window} 190 | 191 | The "w" parameter value conveys a time window applicable to the quota ({{ratelimitpolicy-quota}}). The time window MUST be a non-negative, non-zero, Integer value expressing an interval in seconds, similar to the "delay-seconds" rule defined in {{Section 10.2.3 of HTTP}}. Sub-second precision is not supported. 192 | 193 | ### Partition Key Parameter {#ratelimitpolicy-partitionkey} 194 | 195 | The "pk" parameter value conveys the partition key associated to the request. The value MUST be a Byte Sequence. Servers MAY use the partition key to divide server capacity across different clients and resources. Quotas are allocated per partition key. 196 | 197 | ## RateLimit Policy Field Examples 198 | 199 | This field MAY convey the time window associated with the quota, as shown in this example: 200 | 201 | ~~~ 202 | RateLimit-Policy: "default";q=100;w=10 203 | ~~~ 204 | 205 | These examples show multiple policies being returned: 206 | 207 | ~~~ 208 | RateLimit-Policy: "permin";q=50;w=60,"perhr";q=1000;w=3600 209 | ~~~ 210 | 211 | The following example shows a policy with a partition key: 212 | 213 | ~~~ 214 | RateLimit-Policy: "peruser";q=100;w=60;pk=:cHsdsRa894==: 215 | ~~~ 216 | 217 | The following example shows a policy with a partition key and a quota unit: 218 | 219 | ~~~ 220 | RateLimit-Policy: "peruser";q=65535;qu="content-bytes";w=10;pk=:sdfjLJUOUH==: 221 | ~~~ 222 | 223 | # RateLimit Field {#ratelimit-field} 224 | 225 | A server uses the "RateLimit" response header field to communicate the current service limit for a quota policy for a particular partition key. 226 | 227 | The field is expressed as a List{{!RFC8941}} of Service Limit Items ({{servicelimit-item}}). 228 | 229 | Lists of Service Limit Items can be split over multiple "RateLimit" fields in the same HTTP response as described in {{Section 3.1 of !STRUCTURED-FIELDS=RFC8941}}. 230 | 231 | ~~~ 232 | RateLimit: "default";r=50;t=30 233 | ~~~ 234 | 235 | ## Service Limit Item {#servicelimit-item} 236 | 237 | Each service limit Item{{!RFC8941}} identifies the quota policy ({{quotapolicy-item}}) associated with the request and contains Parameters{{!RFC8941}} with information about the current service limit. 238 | 239 | The following parameters are defined in this specification: 240 | 241 | r: 242 | : This REQUIRED parameter value conveys the remaining quota units for the identified policy ({{ratelimit-remaining-parameter}}). 243 | 244 | t: 245 | : This OPTIONAL parameter value conveys the time window reset time for the identified policy ({{ratelimit-reset-parameter}}). 246 | 247 | pk: 248 | : The OPTIONAL "pk" parameter value conveys the partition key associated to the corresponding request. 249 | 250 | This field cannot appear in a trailer section. Other parameters are allowed and can be regarded as comments. 251 | 252 | Implementation- or service-specific parameters SHOULD be prefixed parameters with a vendor identifier, e.g. `acme-policy`, `acme-burst`. 253 | 254 | 255 | ### Remaining Parameter {#ratelimit-remaining-parameter} 256 | 257 | The "r" parameter indicates the remaining quota units for the identified policy ({{ratelimit-remaining-parameter}}). 258 | 259 | It is a non-negative Integer expressed in quota units. 260 | Clients MUST NOT assume that a positive remaining value is a guarantee that further requests will be served. 261 | When the remaining parameter value is low, it indicates that the server may soon throttle the client (see {{providing-ratelimit-fields}}). 262 | 263 | ### Reset Parameter {#ratelimit-reset-parameter} 264 | 265 | The "t" parameter indicates the number of seconds until the quota associated with the quota policy resets. 266 | 267 | It is a non-negative Integer compatible with the delay-seconds rule, because: 268 | 269 | - it does not rely on clock synchronization and is resilient to clock adjustment 270 | and clock skew between client and server (see {{Section 5.6.7 of HTTP}}); 271 | - it mitigates the risk related to thundering herd when too many clients are serviced with the same timestamp. 272 | 273 | The client MUST NOT assume that all its service limit will be reset at the moment indicated by the reset parameter. The server MAY arbitrarily alter the reset parameter value between subsequent requests; for example, in case of resource saturation or to implement sliding window policies. 274 | 275 | ### Partition Key Parameter {#ratelimit-partitionkey} 276 | 277 | The "pk" parameter value conveys the partition key associated to the request. The value MUST be a Byte Sequence. Servers MAY use the partition key to divide server capacity across different clients and resources. Quotas are allocated per partition key. 278 | 279 | 280 | ## RateLimit Field Examples 281 | 282 | This example shows a RateLimit field with a remaining quota of 50 units and a time window reset in 30 seconds: 283 | 284 | ~~~ 285 | RateLimit: "default";r=50;t=30 286 | ~~~ 287 | 288 | This example shows a remaining quota of 999 requests for a partition key that has no time window reset: 289 | 290 | ~~~ 291 | RateLimit: "default";r=999;pk=:dHJpYWwxMjEzMjM=: 292 | ~~~ 293 | 294 | This example shows a 300MB remaining quota for an application in the next 60 seconds: 295 | 296 | ~~~ 297 | RateLimit: "default";r=300000000;t=60;pk=:QXBwLTk5OQ==: 298 | ~~~ 299 | 300 | # Problem Types {#problem-types} 301 | 302 | ## Quota Exceeded 303 | 304 | This section defines the "https://iana.org/assignments/http-problem-types#quota-exceeded" problem type. A server MAY use this problem type if it wants to communicate to the client that the requests sent by the client exceed one or more Quota Policies. This problem type defines the extension member "violated-policies" as an array of strings, whose value is the names of policies where the quota was exceeded. 305 | 306 | ~~~ http-message 307 | HTTP/1.1 429 Bad Request 308 | Content-Type: application/problem+json 309 | 310 | { 311 | "type": "https://iana.org/assignments/http-problem-types#quota-exceeded", 312 | "title": "Request cannot be satisifed as assigned quota has been exceeded", 313 | "violated-policies": ["daily","bandwidth"] 314 | } 315 | ~~~ 316 | 317 | ## Temporary Reduced Capacity 318 | 319 | This section defines the "https://iana.org/assignments/http-problem-types#temporary-reduced-capacity" problem type. A server MAY use this problem type if it wants to communicate to the client that the requests sent by the client exceed cannot currently be satisfied due to a temporary reduction in capacity due to service limitations. The server MAY chose to include a RateLimit-Policy field indicating the new temporarily lower quota. This problem type defines the extension member "violated-policies" as an array of strings, whose value is the names of policies where the quota was exceeded. 320 | 321 | ~~~ http-message 322 | HTTP/1.1 503 Server Unavailable 323 | Content-Type: application/problem+json 324 | 325 | { 326 | "type": "https://iana.org/assignments/http-problem-types#temporary-reduced-capacity", 327 | "title": "Request cannot be satisifed due to temporary server capacity constraints", 328 | "violated-policies": ["hourly"] 329 | } 330 | ~~~ 331 | 332 | ## Abnormal Usage Detected 333 | 334 | This section defines the "https://iana.org/assignments/http-problem-types#abnormal-usage-detected" problem type. A server MAY use this problem type to communicate to the client that it has detected a pattern of requests that suggest unintentional or malicous behaviour on the part of the client. This problem type defines the extension member "violated-policies" as an array of strings, whose value is the names of policies where the quota was exceeded. 335 | 336 | ~~~ http-message 337 | HTTP/1.1 429 Too Many Requests 338 | Content-Type: application/problem+json 339 | 340 | { 341 | "type": "https://iana.org/assignments/http-problem-types#abnormal-usage-detected", 342 | "title": "Request not satisifed due to detection of abnormal request pattern", 343 | "violated-policies": ["hourly"] 344 | } 345 | ~~~ 346 | 347 | 348 | # Server Behavior {#providing-ratelimit-fields} 349 | 350 | A server MAY return RateLimit header fields independently of the response status code. This includes throttled responses. This document does not mandate any correlation between the RateLimit header field values and the returned status code. 351 | 352 | Servers should be careful when returning RateLimit header fields in redirection responses (i.e., responses with 3xx status codes) because a low remaining parameter value could prevent the client from issuing requests. For example, given the RateLimit header fields below, a client could decide to wait 10 seconds before following the "Location" header field (see {{Section 10.2.2 of HTTP}}), because the remaining parameter value is 0. 353 | 354 | ~~~ http-message 355 | HTTP/1.1 301 Moved Permanently 356 | Location: /foo/123 357 | RateLimit: "problemPolicy";r=0;t=10 358 | 359 | ~~~ 360 | 361 | If a response contains both the Retry-After and the RateLimit header fields, the reset parameter value SHOULD reference the same point in time as the Retry-After field value. 362 | 363 | A service using RateLimit header fields MUST NOT convey values exposing an unwanted volume of requests and SHOULD implement mechanisms to cap the ratio between the remaining and the reset parameter values (see {{sec-resource-exhaustion}}); this is especially important when a quota policy uses a large time window. 364 | 365 | Under certain conditions, a server MAY artificially lower RateLimit header field values between subsequent requests, e.g. to respond to Denial of Service attacks or in case of resource saturation. 366 | 367 | ## Generating Partition Keys 368 | 369 | Servers MAY choose to return partition keys that distinguish between quota allocated to different consumers or different resources. There are a wide range of strategies for partitioning server capacity, including per user, per application, per HTTP method, per resource, or some combination of those values. The server SHOULD document how the partition key is generated so that clients can predict the key value for a future request and determine if there is sufficient quota remaining to execute the request. Servers should avoid returning partition keys that contain sensitive information. Servers SHOULD only use information that is present in the request to generate the partition key. 370 | 371 | ## Performance Considerations 372 | 373 | Servers are not required to return RateLimit header fields in every response, and clients need to take this into account. For example, an implementer concerned with performance might provide RateLimit header fields only when a given quota is close to exhaustion. 374 | 375 | Implementers concerned with response fields' size, might take into account their ratio with respect to the content length, or use header-compression HTTP features such as {{?HPACK=RFC7541}}. 376 | 377 | # Client Behavior {#receiving-fields} 378 | 379 | The RateLimit header fields can be used by clients to determine whether the associated request respected the server's quota policy, and as an indication of whether subsequent requests will. However, the server might apply other criteria when servicing future requests, and so the quota policy may not completely reflect whether requests will succeed. 380 | 381 | For example, a successful response with the following fields: 382 | 383 | ~~~ 384 | RateLimit: "default";r=1;t=7 385 | ~~~ 386 | 387 | does not guarantee that the next request will be successful. Servers' behavior may be subject to other conditions. 388 | 389 | A client is responsible for ensuring that RateLimit header field values returned 390 | cause reasonable client behavior with respect to throughput and latency 391 | (see {{sec-resource-exhaustion}} and {{sec-dos}}). 392 | 393 | A client receiving RateLimit header fields MUST NOT assume that future responses will contain the same RateLimit header fields, or any RateLimit header fields at all. 394 | 395 | Malformed RateLimit header fields MUST be ignored. 396 | 397 | A client SHOULD NOT exceed the quota units conveyed by the remaining parameter before the time window expressed in the reset parameter. 398 | 399 | The value of the reset parameter is generated at response time: a client aware of a significant network latency MAY behave accordingly and use other information (e.g. the "Date" response header field, or otherwise gathered metrics) to better estimate the reset parameter moment intended by the server. 400 | 401 | The details provided in the RateLimit-Policy header field are informative and MAY be ignored. 402 | 403 | If a response contains both the RateLimit and Retry-After fields, the Retry-After field MUST take precedence and the reset parameter MAY be ignored. 404 | 405 | This specification does not mandate a specific throttling behavior and implementers can adopt their preferred policies, including: 406 | 407 | - slowing down or pre-emptively back-off their request rate when 408 | approaching quota limits; 409 | - consuming all the quota according to the exposed limits and then wait. 410 | 411 | ## Consuming Partition Keys 412 | 413 | Partition keys are useful for a client if it is likely that single client will make requests that consume different quota allocations. E.g. a client making requests on behalf of different users or for different resources that have independent quota allocations. 414 | 415 | If a server documents the partition key generation algorithm, clients MAY generate a partition key for a future request. Using this key, and comparing to the key returned by the server, the client can determine if there is sufficient quota remaining to execute the request. 416 | 417 | For cases where the partition key generation algorithm of a server is unknown, clients MAY use heuristics to guess if a future request will be successful based on its similarity to previous requests. 418 | 419 | ## Intermediaries {#intermediaries} 420 | 421 | This section documents the considerations advised in {{Section 16.3.2 of HTTP}}. 422 | 423 | An intermediary that is not part of the originating service infrastructure and is not aware of the quota policy semantic used by the Origin Server SHOULD NOT alter the RateLimit header fields' values in such a way as to communicate a more permissive quota policy; this includes removing the RateLimit header fields. 424 | 425 | An intermediary MAY alter the RateLimit header fields in such a way as to communicate a more restrictive quota policy when: 426 | 427 | - it is aware of the quota unit semantic used by the Origin Server; 428 | - it implements this specification and enforces a quota policy which 429 | is more restrictive than the one conveyed in the fields. 430 | 431 | An intermediary SHOULD forward a request even when presuming that it might not be serviced; the service returning the RateLimit header fields is the sole responsible of enforcing the communicated quota policy, and it is always free to service incoming requests. 432 | 433 | This specification does not mandate any behavior on intermediaries respect to retries, nor requires that intermediaries have any role in respecting quota policies. For example, it is legitimate for a proxy to retransmit a request without notifying the client, and thus consuming quota units. 434 | 435 | [Privacy considerations](#privacy) provide further guidance on intermediaries. 436 | 437 | ## Caching 438 | 439 | {{?HTTP-CACHING=RFC9111}} defines how responses can be stored and reused for subsequent requests, 440 | including those with RateLimit header fields. 441 | Because the information in RateLimit header fields on a cached response may not be current, they SHOULD be ignored on responses that come from cache 442 | (i.e., those with a positive current_age; see {{Section 4.2.3 of HTTP-CACHING}}). 443 | 444 | # Security Considerations 445 | 446 | ## Throttling does not prevent clients from issuing requests {#sec-throttling-does-not-prevent} 447 | 448 | This specification does not prevent clients from making requests. 449 | Servers should always implement mechanisms to prevent resource exhaustion. 450 | 451 | ## Information disclosure {#sec-information-disclosure} 452 | 453 | Servers should not disclose to untrusted parties operational capacity information 454 | that can be used to saturate its infrastructural resources. 455 | 456 | While this specification does not mandate whether non-successful responses consume quota, 457 | if error responses (such as 401 (Unauthorized) and 403 (Forbidden)) count against quota, 458 | a malicious client could probe the endpoint to get traffic information of another user. 459 | 460 | As intermediaries might retransmit requests and consume 461 | quota units without prior knowledge of the user agent, 462 | RateLimit header fields might reveal the existence of an intermediary 463 | to the user agent. 464 | 465 | Where partition keys contain identifying information, either of the client application or the user, servers should be aware of the potential for impersonation and apply the appropriate security mechanisms. 466 | 467 | ## Remaining quota units are not granted requests {#sec-remaining-not-granted} 468 | 469 | RateLimit header fields convey hints from the server 470 | to the clients in order to help them avoid being throttled out. 471 | 472 | Clients MUST NOT consider the quota returned in the [remaining parameter](#ratelimit-remaining-parameter) as a service level agreement. 473 | 474 | In case of resource saturation, the server MAY artificially lower the returned values 475 | or not serve the request regardless of the advertised quotas. 476 | 477 | ## Reliability of the reset parameter {#sec-reset-reliability} 478 | 479 | Consider that quota might not be restored after the moment referenced by the [reset parameter](#ratelimit-reset-parameter), 480 | and the reset parameter value may not be constant. 481 | 482 | Subsequent requests might return a higher reset parameter value 483 | to limit concurrency or implement dynamic or adaptive throttling policies. 484 | 485 | ## Resource exhaustion {#sec-resource-exhaustion} 486 | 487 | When returning reset values, servers must be aware that 488 | many throttled clients may come back at the very moment specified. 489 | 490 | This is true for Retry-After too. 491 | 492 | For example, if the quota resets every day at `18:00:00` 493 | and your server returns the reset parameter accordingly 494 | 495 | ~~~ 496 | Date: Tue, 15 Nov 1994 18:00:00 GMT 497 | RateLimit: "daily";r=1;t=36400 498 | ~~~ 499 | 500 | there's a high probability that all clients will show up at `18:00:00`. 501 | 502 | This could be mitigated by adding some jitter to the reset value. 503 | 504 | Resource exhaustion issues can be associated with quota policies using a 505 | large time window, because a user agent by chance or on purpose 506 | might consume most of its quota units in a significantly shorter interval. 507 | 508 | This behavior can be even triggered by the provided RateLimit header fields. 509 | The following example describes a service 510 | with an unconsumed quota policy of 10000 quota units per 1000 seconds. 511 | 512 | ~~~ 513 | RateLimit-Policy: "somepolicy";q=10000;w=1000 514 | RateLimit: "somepolicy";r=10000;t=10 515 | ~~~ 516 | 517 | A client implementing a simple ratio between remaining parameter and reset parameter could infer an average throughput of 1000 quota units per second, while the quota parameter conveys a quota-policy with an average of 10 quota units per second. 518 | If the service cannot handle such load, it should return either a lower remaining parameter value or a higher reset parameter value. 519 | Moreover, complementing large time window quota policies with a short time window one mitigates those risks. 520 | 521 | 522 | ### Denial of Service {#sec-dos} 523 | 524 | RateLimit header fields may contain unexpected values by chance or on purpose. 525 | For example, an excessively high remaining parameter value may be: 526 | 527 | - used by a malicious intermediary to trigger a Denial of Service attack 528 | or consume client resources boosting its requests; 529 | - passed by a misconfigured server; 530 | 531 | or a high reset parameter value could inhibit clients to contact the server (e.g. similarly to receiving "Retry-after: 1000000"). 532 | 533 | To mitigate this risk, clients can set thresholds that they consider reasonable in terms of quota units, time window, concurrent requests or throughput, and define a consistent behavior when the RateLimit exceed those thresholds. 534 | For example this means capping the maximum number of request per second, or implementing retries when the reset parameter exceeds ten minutes. 535 | 536 | The considerations above are not limited to RateLimit header fields, but apply to all fields affecting how clients behave in subsequent requests (e.g. Retry-After). 537 | 538 | 539 | # Privacy Considerations {#privacy} 540 | 541 | Clients that act upon a request to rate limit 542 | are potentially re-identifiable (see {{Section 5.2.1 of PRIVACY}}) 543 | because they react to information that might only be given to them. 544 | Note that this might apply to other fields too (e.g. Retry-After). 545 | 546 | Since rate limiting is usually implemented in contexts where 547 | clients are either identified or profiled 548 | (e.g. assigning different quota units to different users), 549 | this is rarely a concern. 550 | 551 | Privacy enhancing infrastructures using RateLimit header fields 552 | can define specific techniques to mitigate the risks of re-identification. 553 | 554 | # IANA Considerations 555 | 556 | IANA is requested to update two registries and create one new registry. 557 | 558 | ## Update HTTP Field Name Registry 559 | 560 | Please add the following entries to the 561 | "Hypertext Transfer Protocol (HTTP) Field Name Registry" registry ({{HTTP}}): 562 | 563 | |---------------------|-----------|---------------| 564 | | Field Name | Status | Specification | 565 | |---------------------|-----------|---------------| 566 | | RateLimit | permanent | {{ratelimit-field}} of {{&SELF}} | 567 | | RateLimit-Policy | permanent | {{ratelimit-policy-field}} of {{&SELF}} | 568 | |---------------------|-----------|---------------| 569 | 570 | 571 | ## Update HTTP Problem Type registry 572 | 573 | IANA is asked to register the following entries in the "HTTP Problem Types" registry at https://www.iana.org/assignments/http-problem-types. 574 | 575 | ### Registration of "quota-exceeded" Problem Type 576 | 577 | Type URI: https://iana.org/assignments/http-problem-types#quota-exceeded 578 | 579 | Title: Quota Exceeded 580 | 581 | Recommended HTTP status code: 429 582 | 583 | Reference: {{quota-exceeded}} of this document 584 | 585 | ### Registration of "temporary-reduced-capacity" Problem Type 586 | 587 | Type URI: https://iana.org/assignments/http-problem-types#temporary-reduced-capacity 588 | 589 | Title: Temporary Reduced Capacity 590 | 591 | Recommended HTTP status code: 503 592 | 593 | Reference: {{temporary-reduced-capacity}} of this document 594 | 595 | ### Registration of "abnormal-usage-detected" Problem Type 596 | 597 | Type URI: https://iana.org/assignments/http-problem-types#abnormal-usage-detected 598 | 599 | Title: Abnormal Usage Detected 600 | 601 | Recommended HTTP status code: 429 602 | 603 | Reference: {{abnormal-usage-detected}} of this document 604 | 605 | 606 | ## RateLimit quota unit registry {#ratelimit-quota-unit-registry} 607 | 608 | This specification establishes the registry "Hypertext Transfer Protocol (HTTP) RateLimit Quota Units" registry to be located at https://www.iana.org/assignments/http-ratelimit-quota-units. Registration is done on the advice of a Designated Expert, appointed by the IESG or their delegate. All entries are Specification Required ([IANA], Section 4.6). 609 | 610 | The registry has the following initial content: 611 | 612 | |---------------------|-----------|---------------| 613 | | Quota Unit | Reference | Notes | 614 | |---------------------|-----------|---------------| 615 | | request | {{&SELF}} | | 616 | | content-bytes | {{&SELF}} | | 617 | | concurrent-requests | {{&SELF}} | | 618 | |---------------------|-----------|---------------| 619 | 620 | ### Registration Template 621 | 622 | The registration template for the RateLimit Quota Units registry is as follows: 623 | 624 | - Quota Unit: The name of the quota unit. 625 | - Reference: A reference to the document that specifies the quota unit. 626 | - Notes: Any additional notes about the quota unit. 627 | 628 | 629 | --- back 630 | 631 | # Rate-limiting and quotas {#rate-limiting} 632 | 633 | Servers use quota mechanisms to avoid systems overload, to ensure an equitable distribution of computational resources or to enforce other policies - e.g. monetization. 634 | 635 | A basic quota mechanism limits the number of acceptable requests in a given time window, e.g. 10 requests per second. 636 | 637 | When quota is exceeded, servers usually do not serve the request replying instead with a 4xx HTTP status code (e.g. 429 or 403) or adopt more aggressive policies like dropping connections. 638 | 639 | Quotas may be enforced on different basis (e.g. per user, per IP, per geographic area, etc.) and at different levels. For example, an user may be allowed to issue: 640 | 641 | - 10 requests per second; 642 | - limited to 60 requests per minute; 643 | - limited to 1000 requests per hour. 644 | 645 | Moreover system metrics, statistics and heuristics can be used to implement more complex policies, 646 | where the number of acceptable requests and the time window are computed dynamically. 647 | 648 | To help clients throttling their requests, 649 | servers may expose the counters used to evaluate quota policies via HTTP header fields. 650 | 651 | Those response headers may be added by HTTP intermediaries such as API gateways and reverse proxies. 652 | 653 | On the web we can find many different rate-limit headers, 654 | usually containing the number of allowed requests in a given time window, and when the window is reset. 655 | 656 | The common choice is to return three headers containing: 657 | 658 | - the maximum number of allowed requests in the time window; 659 | - the number of remaining requests in the current window; 660 | - the time remaining in the current window expressed in seconds or 661 | as a timestamp; 662 | 663 | ## Interoperability issues 664 | 665 | A major interoperability issue in throttling is the lack of standard headers, because: 666 | 667 | - each implementation associates different semantics to the 668 | same header field names; 669 | - header field names proliferates. 670 | 671 | User agents interfacing with different servers may thus need to process different headers, 672 | or the very same application interface that sits behind different reverse proxies 673 | may reply with different throttling headers. 674 | 675 | # Examples 676 | 677 | ## Responses without defining policies 678 | 679 | Some servers may not expose the policy limits in the RateLimit-Policy header field. Clients can still use the RateLimit header field to throttle their requests. 680 | 681 | ### Throttling information in responses 682 | 683 | The client exhausted its quota for the next 50 seconds. 684 | The limit and time-window is communicated out-of-band. 685 | 686 | Request: 687 | 688 | ~~~ http-message 689 | GET /items/123 HTTP/1.1 690 | Host: api.example 691 | 692 | ~~~ 693 | 694 | Response: 695 | 696 | ~~~ http-message 697 | HTTP/1.1 200 Ok 698 | Content-Type: application/json 699 | RateLimit: "default";r=0;t=50 700 | 701 | {"hello": "world"} 702 | ~~~ 703 | 704 | Since the field values are not necessarily correlated with 705 | the response status code, 706 | a subsequent request is not required to fail. 707 | The example below shows that the server decided to serve the request 708 | even if remaining parameter value is 0. 709 | Another server, or the same server under other load conditions, could have decided to throttle the request instead. 710 | 711 | Request: 712 | 713 | ~~~ http-message 714 | GET /items/456 HTTP/1.1 715 | Host: api.example 716 | 717 | ~~~ 718 | 719 | Response: 720 | 721 | ~~~ http-message 722 | HTTP/1.1 200 Ok 723 | Content-Type: application/json 724 | RateLimit: "default";r=0;t=48 725 | 726 | {"still": "successful"} 727 | ~~~ 728 | 729 | ### Multiple policies in response {#use-with-multiple-policies} 730 | 731 | The server uses two different policies to limit the client's requests: 732 | 733 | - 5000 daily quota units; 734 | - 1000 hourly quota units. 735 | 736 | The client consumed 4900 quota units in the first 14 hours. 737 | 738 | Despite the next hourly limit of 1000 quota units, 739 | the closest limit to reach is the daily one. 740 | 741 | The server then exposes the RateLimit header fields to 742 | inform the client that: 743 | 744 | - it has only 100 quota units left in the daily quota and the window will reset in 10 hours; 745 | 746 | The server MAY choose to omit returning the hourly policy as it uses the same quota units as the daily policy and the daily policy is the one that is closest to being exhausted. 747 | 748 | Request: 749 | 750 | ~~~ http-message 751 | GET /items/123 HTTP/1.1 752 | Host: api.example 753 | 754 | ~~~ 755 | 756 | Response: 757 | 758 | ~~~ http-message 759 | HTTP/1.1 200 Ok 760 | Content-Type: application/json 761 | RateLimit: "dayLimit";r=100;t=36000 762 | 763 | {"hello": "world"} 764 | ~~~ 765 | 766 | ### Use for limiting concurrency {#use-for-limiting-concurrency} 767 | 768 | RateLimit header fields may be used to limit concurrency, 769 | advertising limits that are lower than the usual ones 770 | in case of saturation, thus increasing availability. 771 | 772 | The server adopted a basic policy of 100 quota units per minute, 773 | and in case of resource exhaustion adapts the returned values 774 | reducing both limit and remaining parameter values. 775 | 776 | After 2 seconds the client consumed 40 quota units 777 | 778 | Request: 779 | 780 | ~~~ http-message 781 | GET /items/123 HTTP/1.1 782 | Host: api.example 783 | 784 | ~~~ 785 | 786 | Response: 787 | 788 | ~~~ http-message 789 | HTTP/1.1 200 Ok 790 | Content-Type: application/json 791 | RateLimit-Policy: "basic";q=100;w=60 792 | RateLimit: "basic";r=60;t=58 793 | 794 | {"elapsed": 2, "issued": 40} 795 | ~~~ 796 | 797 | At the subsequent request - due to resource exhaustion - 798 | the server advertises only `r=20`. 799 | 800 | Request: 801 | 802 | ~~~ http-message 803 | GET /items/123 HTTP/1.1 804 | Host: api.example 805 | 806 | ~~~ 807 | 808 | Response: 809 | 810 | ~~~ http-message 811 | HTTP/1.1 200 Ok 812 | Content-Type: application/json 813 | RateLimit-Policy: "basic";q=100;w=60 814 | RateLimit: "basic";r=20;t=56 815 | 816 | {"elapsed": 4, "issued": 41} 817 | ~~~ 818 | 819 | ### Use in throttled responses 820 | 821 | A client exhausted its quota and the server throttles it 822 | sending Retry-After. 823 | 824 | In this example, the values of Retry-After and RateLimit header field reference the same moment, 825 | but this is not a requirement. 826 | 827 | The 429 (Too Many Request) HTTP status code is just used as an example. 828 | 829 | Request: 830 | 831 | ~~~ http-message 832 | GET /items/123 HTTP/1.1 833 | Host: api.example 834 | 835 | ~~~ 836 | 837 | Response: 838 | 839 | ~~~ http-message 840 | HTTP/1.1 429 Too Many Requests 841 | Content-Type: application/problem+json 842 | Date: Mon, 05 Aug 2019 09:27:00 GMT 843 | Retry-After: Mon, 05 Aug 2019 09:27:05 GMT 844 | RateLimit: "default";r=0;t=5 845 | 846 | { 847 | "type": "https://iana.org/assignments/http-problem-types#quota-exceeded" 848 | "title": "Too Many Requests", 849 | "status": 429, 850 | "policy-violations": ["default"] 851 | } 852 | ~~~ 853 | 854 | ## Responses with defined policies 855 | 856 | ### Throttling window specified via parameter 857 | 858 | The client has 99 quota units left for the next 50 seconds. 859 | The time window is communicated by the `w` parameter, so we know the throughput is 100 quota units per minute. 860 | 861 | Request: 862 | 863 | ~~~ http-message 864 | GET /items/123 HTTP/1.1 865 | Host: api.example 866 | 867 | ~~~ 868 | 869 | Response: 870 | 871 | ~~~ http-message 872 | HTTP/1.1 200 Ok 873 | Content-Type: application/json 874 | RateLimit: "fixedwindow";r=99;t=50 875 | RateLimit-Policy: "fixedwindow";q=100;w=60 876 | {"hello": "world"} 877 | ~~~ 878 | 879 | 880 | ### Dynamic limits with parameterized windows 881 | 882 | The policy conveyed by the RateLimit header field states that 883 | the server accepts 100 quota units per minute. 884 | 885 | To avoid resource exhaustion, the server artificially lowers 886 | the actual limits returned in the throttling headers. 887 | 888 | The remaining parameter then advertises 889 | only 9 quota units for the next 50 seconds to slow down the client. 890 | 891 | Note that the server could have lowered even the other 892 | values in the RateLimit header field: this specification 893 | does not mandate any relation between the field values 894 | contained in subsequent responses. 895 | 896 | Request: 897 | 898 | ~~~ http-message 899 | GET /items/123 HTTP/1.1 900 | Host: api.example 901 | 902 | ~~~ 903 | 904 | Response: 905 | 906 | ~~~ http-message 907 | HTTP/1.1 200 Ok 908 | Content-Type: application/json 909 | RateLimit-Policy: "dynamic";q=100;w=60 910 | RateLimit: "dynamic";r=9;t=50 911 | 912 | 913 | { 914 | "status": 200, 915 | "detail": "Just slow down without waiting." 916 | } 917 | ~~~ 918 | 919 | ### Dynamic limits for pushing back and slowing down 920 | 921 | Continuing the previous example, let's say the client waits 10 seconds and 922 | performs a new request which, due to resource exhaustion, the server rejects 923 | and pushes back, advertising `r=0` for the next 20 seconds. 924 | 925 | The server advertises a smaller window with a lower limit to slow 926 | down the client for the rest of its original window after the 20 seconds elapse. 927 | 928 | Request: 929 | 930 | ~~~ http-message 931 | GET /items/123 HTTP/1.1 932 | Host: api.example 933 | 934 | ~~~ 935 | 936 | Response: 937 | 938 | ~~~ http-message 939 | HTTP/1.1 429 Too Many Requests 940 | Content-Type: application/json 941 | RateLimit-Policy: "dynamic";q=15;w=20 942 | RateLimit: "dynamic";r=0;t=20 943 | 944 | { 945 | "status": 429, 946 | "detail": "Wait 20 seconds, then slow down!" 947 | } 948 | ~~~ 949 | 950 | ## Dynamic limits for pushing back with Retry-After and slow down 951 | 952 | Alternatively, given the same context where the previous example starts, we 953 | can convey the same information to the client via Retry-After, with 954 | the advantage that the server can now specify the policy's nominal limit and 955 | window that will apply after the reset, e.g. assuming the resource exhaustion 956 | is likely to be gone by then, so the advertised policy does not need to be 957 | adjusted, yet we managed to stop requests for a while and slow down the rest of 958 | the current window. 959 | 960 | Request: 961 | 962 | ~~~ http-message 963 | GET /items/123 HTTP/1.1 964 | Host: api.example 965 | 966 | ~~~ 967 | 968 | Response: 969 | 970 | ~~~ http-message 971 | HTTP/1.1 429 Too Many Requests 972 | Content-Type: application/json 973 | Retry-After: 20 974 | RateLimit-Policy: "dynamic";q=100;w=60 975 | RateLimit: "dynamic";r=15;t=40 976 | 977 | { 978 | "status": 429, 979 | "detail": "Wait 20 seconds, then slow down!" 980 | } 981 | ~~~ 982 | 983 | Note that in this last response the client is expected to honor 984 | Retry-After and perform no requests for the specified amount of 985 | time, whereas the previous example would not force the client to stop 986 | requests before the reset time is elapsed, as it would still be free to 987 | query again the server even if it is likely to have the request rejected. 988 | 989 | ### Missing Remaining information 990 | 991 | The server does not expose remaining values 992 | (for example, because the underlying counters are not available). 993 | Instead, it resets the limit counter every second. 994 | 995 | It communicates to the client the limit of 10 quota units per second 996 | always returning the limit and reset parameters. 997 | 998 | Request: 999 | 1000 | ~~~ http-message 1001 | GET /items/123 HTTP/1.1 1002 | Host: api.example 1003 | 1004 | ~~~ 1005 | 1006 | Response: 1007 | 1008 | ~~~ http-message 1009 | HTTP/1.1 200 Ok 1010 | Content-Type: application/json 1011 | RateLimit-Policy: quota;q=100;w=1 1012 | RateLimit: quota;t=1 1013 | 1014 | {"first": "request"} 1015 | ~~~ 1016 | 1017 | Request: 1018 | 1019 | ~~~ http-message 1020 | GET /items/123 HTTP/1.1 1021 | Host: api.example 1022 | 1023 | ~~~ 1024 | 1025 | Response: 1026 | 1027 | ~~~ http-message 1028 | HTTP/1.1 200 Ok 1029 | Content-Type: application/json 1030 | RateLimit-Policy: quota;q=10 1031 | RateLimit: quota;t=1 1032 | 1033 | {"second": "request"} 1034 | ~~~ 1035 | 1036 | ### Use with multiple windows 1037 | 1038 | This is a standardized way of describing the policy 1039 | detailed in {{use-with-multiple-policies}}: 1040 | 1041 | - 5000 daily quota units; 1042 | - 1000 hourly quota units. 1043 | 1044 | The client consumed 4900 quota units in the first 14 hours. 1045 | 1046 | Despite the next hourly limit of 1000 quota units, the closest limit 1047 | to reach is the daily one. 1048 | 1049 | The server then exposes the RateLimit header fields to inform the client that: 1050 | 1051 | - it has only 100 quota units left; 1052 | - the window will reset in 10 hours; 1053 | - the expiring-limit is 5000. 1054 | 1055 | Request: 1056 | 1057 | ~~~ http-message 1058 | GET /items/123 HTTP/1.1 1059 | Host: api.example 1060 | 1061 | ~~~ 1062 | 1063 | Response: 1064 | 1065 | ~~~ http-message 1066 | HTTP/1.1 200 OK 1067 | Content-Type: application/json 1068 | RateLimit-Policy: "hour";q=1000;w=3600, "day";q=5000;w=86400 1069 | RateLimit: "day";r=100;t=36000 1070 | 1071 | {"hello": "world"} 1072 | ~~~ 1073 | 1074 | # FAQ 1075 | {:numbered="false" removeinrfc="true"} 1076 | 1077 | 1. Why defining standard fields for throttling? 1078 | 1079 | To simplify enforcement of throttling policies and enable clients to constraint their requests to avoid being throttled. 1080 | 1081 | 2. Can I use RateLimit header fields in throttled responses (e.g. with status code 429)? 1082 | 1083 | Yes, you can. 1084 | 1085 | 3. Are those specs tied to RFC 6585? 1086 | 1087 | No. {{?RFC6585}} defines the `429` status code and we use it just as an example of a throttled request, 1088 | that could instead use even `403` or whatever status code. 1089 | 1090 | 4. Why is the partition key necessary? 1091 | 1092 | Without a partition key, a server can effectively only have one scope (aka partition), which is impractical for most services, or it needs to communicate the scopes out-of-band. 1093 | This prevents the development of generic connector code that can be used to prevent requests from being throttled. 1094 | Many APIs rely on API keys, user identity or client identity to allocate quota. 1095 | As soon as a single client processes requests for more than one partition, the client needs to know the corresponding partition key to properly track requests against allocated quota. 1096 | 1097 | 5. Why using delay-seconds instead of a UNIX Timestamp? 1098 | Why not using subsecond precision? 1099 | 1100 | Using delay-seconds aligns with Retry-After, which is returned in similar contexts, 1101 | e.g. on 429 responses. 1102 | 1103 | Timestamps require a clock synchronization protocol 1104 | (see {{Section 5.6.7 of HTTP}}). 1105 | This may be problematic (e.g. clock adjustment, clock skew, failure of hardcoded clock synchronization servers, 1106 | IoT devices, etc.). 1107 | Moreover timestamps may not be monotonically increasing due to clock adjustment. 1108 | See [Another NTP client failure story](https://community.ntppool.org/t/another-ntp-client-failure-story/1014/) 1109 | 1110 | We did not use subsecond precision because: 1111 | 1112 | - that is more subject to system clock correction 1113 | like the one implemented via the adjtimex() Linux system call; 1114 | - response-time latency may not make it worth. A brief discussion on the subject is 1115 | on the [httpwg ml](https://lists.w3.org/Archives/Public/ietf-http-wg/2019JulSep/0202.html) 1116 | - almost all rate-limit headers implementations do not use it. 1117 | 1118 | 6. Shouldn't I limit concurrency instead of request rate? 1119 | 1120 | You can use this specification to limit concurrency 1121 | at the HTTP level (see {#use-for-limiting-concurrency}) 1122 | and help clients to shape their requests avoiding being throttled out. 1123 | 1124 | A problematic way to limit concurrency is connection dropping, 1125 | especially when connections are multiplexed (e.g. HTTP/2) 1126 | because this results in unserviced client requests, 1127 | which is something we want to avoid. 1128 | 1129 | A semantic way to limit concurrency is to return 503 + Retry-After 1130 | in case of resource saturation (e.g. thrashing, connection queues too long, 1131 | Service Level Objectives not meet, etc.). 1132 | Saturation conditions can be either dynamic or static: all this is out of 1133 | the scope for the current document. 1134 | 1135 | 7. Do a positive value of remaining parameter imply any service guarantee for my 1136 | future requests to be served? 1137 | 1138 | No. FAQ integrated in {{ratelimit-remaining-parameter}}. 1139 | 1140 | 8. Is the quota-policy definition too complex? 1141 | 1142 | You can always return the simplest form 1143 | 1144 | ~~~ 1145 | RateLimit:"default";r=50;t=60 1146 | ~~~ 1147 | 1148 | The policy key clearly connects the current usage status of a policy to the defined limits. 1149 | So for the following field: 1150 | 1151 | ~~~ 1152 | RateLimit-Policy: "sliding";q=100;w=60;burst=1000 1153 | RateLimit-Policy: "fixed";q=5000;w=3600;burst=0 1154 | RateLimit: "sliding";r=50;t=44 1155 | ~~~ 1156 | 1157 | the value "sliding" identifies the policy being reported. 1158 | 1159 | 9. Can intermediaries alter RateLimit header fields? 1160 | 1161 | Generally, they should not because it might result in unserviced requests. 1162 | There are reasonable use cases for intermediaries mangling RateLimit header fields though, 1163 | e.g. when they enforce stricter quota-policies, 1164 | or when they are an active component of the service. 1165 | In those case we will consider them as part of the originating infrastructure. 1166 | 1167 | 10. Why the `w` parameter is just informative? 1168 | Could it be used by a client to determine the request rate? 1169 | 1170 | A non-informative `w` parameter might be fine in an environment 1171 | where clients and servers are tightly coupled. Conveying policies 1172 | with this detail on a large scale would be very complex and implementations 1173 | would likely be not interoperable. We thus decided to leave `w` as 1174 | an informational parameter and only rely on the limit, remaining and reset parameters 1175 | for defining the throttling 1176 | behavior. 1177 | 1178 | 11. Can I use RateLimit fields in trailers? 1179 | Servers usually establish whether the request is in-quota before creating a response, so the RateLimit field values should be already available in that moment. 1180 | Supporting trailers has the only advantage that it allows to provide more up-to-date information to the client in case of slow responses. 1181 | However, this complicates client implementations with respect to combining fields from headers and accounting for intermediaries that drop trailers. 1182 | Since there are no current implementations that use trailers, we decided to leave this as a future-work. 1183 | 1184 | # RateLimit header fields currently used on the web 1185 | {:numbered="false" removeinrfc="true"} 1186 | 1187 | Commonly used header field names are: 1188 | 1189 | - `X-RateLimit-Limit`, 1190 | `X-RateLimit-Remaining`, 1191 | `X-RateLimit-Reset`; 1192 | 1193 | There are variants too, where the window is specified 1194 | in the header field name, e.g.: 1195 | 1196 | - `x-ratelimit-limit-minute`, `x-ratelimit-limit-hour`, `x-ratelimit-limit-day` 1197 | - `x-ratelimit-remaining-minute`, `x-ratelimit-remaining-hour`, `x-ratelimit-remaining-day` 1198 | 1199 | Here are some interoperability issues: 1200 | 1201 | - `X-RateLimit-Remaining` references different values, depending on the implementation: 1202 | 1203 | * seconds remaining to the window expiration 1204 | * milliseconds remaining to the window expiration 1205 | * seconds since UTC, in UNIX Timestamp [UNIX] 1206 | * a datetime, either `IMF-fixdate` {{HTTP}} or {{?RFC3339}} 1207 | 1208 | - different headers, with the same semantic, are used by different implementers: 1209 | 1210 | * X-RateLimit-Limit and X-Rate-Limit-Limit 1211 | * X-RateLimit-Remaining and X-Rate-Limit-Remaining 1212 | * X-RateLimit-Reset and X-Rate-Limit-Reset 1213 | 1214 | The semantic of RateLimit depends on the windowing algorithm. 1215 | A sliding window policy for example, may result in having a remaining parameter value related to the ratio between the current and the maximum throughput. 1216 | e.g. 1217 | 1218 | ~~~ 1219 | RateLimit-Policy: "sliding";q=12;w=1 1220 | ; using 50% of throughput, that is 6 units/s 1221 | RateLimit: "sliding";q=12;r=6;t=1 1222 | 1223 | ~~~ 1224 | 1225 | If this is the case, the optimal solution is to achieve 1226 | 1227 | ~~~ 1228 | RateLimit-Policy: "sliding";q=12;w=1 1229 | ; using 100% of throughput, that is 12 units/s 1230 | RateLimit: "sliding";q=12;r=1;t=1 1231 | ~~~ 1232 | 1233 | At this point you should stop increasing your request rate. 1234 | 1235 | # Acknowledgements 1236 | {:numbered="false"} 1237 | 1238 | Thanks to Willi Schoenborn, Alejandro Martinez Ruiz, Alessandro Ranellucci, 1239 | Amos Jeffries, 1240 | Martin Thomson, 1241 | Erik Wilde and Mark Nottingham for being the initial contributors 1242 | of these specifications. 1243 | Kudos to the first community implementers: 1244 | Aapo Talvensaari, 1245 | Nathan Friedly 1246 | and Sanyam Dogra. 1247 | 1248 | In addition to the people above, this document owes a lot to the extensive discussion in the HTTPAPI workgroup, including 1249 | Rich Salz, 1250 | and Julian Reschke. 1251 | 1252 | # Changes 1253 | {:numbered="false" removeinrfc="true"} 1254 | 1255 | ## Since draft-ietf-httpapi-ratelimit-headers-08 1256 | {:numbered="false" removeinrfc="true"} 1257 | 1258 | * Added Problem Types 1259 | * Clarified when to use RateLimit-Policy vs RateLimit fields 1260 | 1261 | ## Since draft-ietf-httpapi-ratelimit-headers-07 1262 | {:numbered="false" removeinrfc="true"} 1263 | 1264 | * Refactored both fields to lists of Items that identify policy and use parameters 1265 | * Added quota unit parameter 1266 | * Added partition key parameter 1267 | 1268 | 1269 | ## Since draft-ietf-httpapi-ratelimit-headers-03 1270 | {:numbered="false" removeinrfc="true"} 1271 | 1272 | * Split policy informatiom in RateLimit-Policy #81 1273 | 1274 | 1275 | ## Since draft-ietf-httpapi-ratelimit-headers-02 1276 | {:numbered="false" removeinrfc="true"} 1277 | 1278 | * Address throttling scope #83 1279 | 1280 | ## Since draft-ietf-httpapi-ratelimit-headers-01 1281 | {:numbered="false" removeinrfc="true"} 1282 | 1283 | * Update IANA considerations #60 1284 | * Use Structured fields #58 1285 | * Reorganize document #67 1286 | 1287 | ## Since draft-ietf-httpapi-ratelimit-headers-00 1288 | {:numbered="false" removeinrfc="true"} 1289 | 1290 | * Use I-D.httpbis-semantics, which includes referencing delay-seconds 1291 | instead of delta-seconds. #5 1292 | -------------------------------------------------------------------------------- /issues.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 |ID | 74 |Title | 75 |State | 76 |Author | 77 |Assignee | 78 |Labels | 79 |
---|
This page shows GitHub issues in a simple form.
88 |The filter box above accepts a set of filters, each separated by space.
89 |You can /sort on id, recent, or closed.
92 |Pressing enter saves the current search. 93 | Pressing esc leaves the text input area.
94 |Clicking an issue title displays details for the issue including comments. 96 | Pressing n or j moves to the next issue, 97 | and p or k move to the previous one.
98 |Pressing esc closes the issue view, ' focuses search, and 99 | c clears the search.
100 |python3 -m http.server
,' +
1005 | ' then view this file using that server.';
1006 | document.getElementById('help').insertBefore(p, h.firstChild);
1007 | }
1008 |
1009 | function issueOverlaySetup() {
1010 | let overlay = document.getElementById('overlay');
1011 | overlay.addEventListener('click', hideIssue);
1012 | window.addEventListener('keyup', e => {
1013 | if (e.target.id === 'cmd') {
1014 | if (e.key === 'Escape') {
1015 | e.preventDefault();
1016 | e.target.blur();
1017 | }
1018 | return;
1019 | }
1020 | if (e.key === 'Escape') {
1021 | e.preventDefault();
1022 | hideIssue();
1023 | }
1024 | });
1025 | window.addEventListener('keypress', e => {
1026 | if (e.target.closest('input')) {
1027 | return;
1028 | }
1029 | if (e.key === 'p' || e.key === 'k') {
1030 | e.preventDefault();
1031 | step(-1);
1032 | } else if (e.key === 'n' || e.key === 'j') {
1033 | e.preventDefault();
1034 | step(1);
1035 | } else if (e.key === '?') {
1036 | e.preventDefault();
1037 | showHelp();
1038 | } else if (e.key === '\'') {
1039 | e.preventDefault();
1040 | hideIssue();
1041 | document.getElementById('cmd').focus();
1042 | } else if (e.key === 'c') {
1043 | e.preventDefault();
1044 | hideIssue();
1045 | document.getElementById('cmd').value = '';
1046 | redraw(true);
1047 | }
1048 | })
1049 | }
1050 |
1051 | window.onload = () => {
1052 | let cmd = document.getElementById('cmd');
1053 | let redrawHandler = debounce(redraw);
1054 | cmd.addEventListener('input', redrawHandler);
1055 | cmd.addEventListener('keypress', redrawHandler);
1056 | window.addEventListener('hashchange', e => {
1057 | cmd.value = decodeURIComponent(window.location.hash.substring(1));
1058 | redrawHandler(e);
1059 | });
1060 | if (window.location.hash) {
1061 | cmd.value = decodeURIComponent(window.location.hash.substring(1));
1062 | }
1063 | sortSetup();
1064 | generateHelp();
1065 | issueOverlaySetup();
1066 | get().then(redraw).catch(addFileHelp);
1067 | }
--------------------------------------------------------------------------------
/test_ratelimit.py:
--------------------------------------------------------------------------------
1 | #
2 | # Test ratelimit fields
3 | #
4 | import http_sfv
5 |
6 |
7 | def test_policy():
8 | policies = [
9 | "1000;w=3600,5000;w=86400",
10 | "100;w=60",
11 | "10;w=1, 50;w=60, 1000;w=3600, 5000;w=86400",
12 | "10;w=1;burst=1000, 1000;w=3600",
13 | ]
14 | for p in policies:
15 | l = http_sfv.List()
16 | l.parse(p.encode())
17 | for i in l:
18 | assert i.value
19 | assert "w" in i.params
20 | print("value: ", i.value, "params:", dict(i.params))
21 |
22 |
23 | def test_all_in_one():
24 | values = [
25 | (
26 | "limit=10, remaining=10, reset=4,"
27 | """policy=(10;w=1 50;w=60 1000;w=3600 5000;w=86400)"""
28 | ),
29 | ]
30 | for v in values:
31 | d = http_sfv.Dictionary()
32 | d.parse(v.encode())
33 | for k, v in d.items():
34 | print("parameter:", k, "value:", v)
35 | assert k
36 | assert v
37 | if hasattr(v, "__iter__"):
38 | for i in v:
39 | assert i.value
40 | assert "w" in i.params
41 | print("value: ", i.value, "params:", dict(i.params))
42 | raise NotImplementedError
43 |
44 |
45 | def find_quota_policy(policies, limit):
46 | for policy in policies:
47 | if policy.value == limit:
48 | return policy.params
49 | return {}
50 |
51 |
52 | def parse_fields(headers):
53 | # 27 µs ± 499 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
54 | limit = http_sfv.Item()
55 | limit.parse(headers["limit"].encode())
56 |
57 | policies = http_sfv.List()
58 | policies.parse(headers["policy"].encode())
59 | quota_policy = find_quota_policy(policies, limit.value)
60 | return limit, policies, quota_policy
61 |
62 |
63 | def parse_fields_token(headers):
64 | # 24.2 µs ± 506 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
65 | limit = http_sfv.Item()
66 | limit.parse(headers["limit"].encode())
67 |
68 | policies = http_sfv.Dictionary()
69 | policies.parse(headers["policy"].encode())
70 | quota_policy = policies[f"q{limit.value}"]
71 | return limit, policies, quota_policy.params
72 |
73 |
74 | def parse_fields_int(headers):
75 | # 24.5 µs ± 297 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
76 | limit = int(headers["limit"])
77 | policies = http_sfv.List()
78 | policies.parse(headers["policy"].encode())
79 | quota_policy = find_quota_policy(policies, limit)
80 | return limit, policies, quota_policy
81 |
82 |
83 | def test_get_policy():
84 | # To retreive the policy, I need to iterate over the list.
85 | headers = dict(
86 | policy=(
87 | """10;w=1,"""
88 | """50;w=60,"""
89 | """1000;w=3600;comment="foo", """
90 | """5000;w=86400"""
91 | ),
92 | limit="""1000""",
93 | )
94 |
95 | limit, policies, quota_policy = parse_fields(headers)
96 | assert limit.value == 1000
97 |
98 | for policy in policies:
99 | print("parameter:", policy.value, "value:", policy.params["w"])
100 |
101 |
102 | def test_get_policy_tokenized():
103 | # To retreive the policy, I need to iterate over the list.
104 | headers = dict(
105 | policy=(
106 | """q10;w=1,"""
107 | """q50;w=60,"""
108 | """q1000;w=3600;comment="foo", """
109 | """q5000;w=86400"""
110 | ),
111 | limit="""1000""",
112 | )
113 | limit = http_sfv.Item()
114 | limit.parse(headers["limit"].encode())
115 | assert limit.value == "q1000"
116 |
117 | policies = http_sfv.List()
118 | policies.parse(headers["policy"].encode())
119 | for policy in policies:
120 | print("parameter:", policy.value, "value:", policy.params["w"])
121 |
122 | # I could create a dictionary out of the list.
123 | policies_dict = {policy.value: policy.params for policy in policies}
124 | print("policies:", policies_dict)
125 | print("current policy:", policies_dict[limit.value])
126 | raise NotImplementedError
127 |
--------------------------------------------------------------------------------