When the interface is being attacked, should the speed limit be configured at the gateway layer or in the program?
When the site interface is flooded with a large number of requests, where should the rate limit be set, at the Web server gateway layer or in the program? Both layers are set, but they block different things: the gateway layer limits the request rate based on the source address, and the request is dropped before it enters the program; the program layer knows who the caller is and what action they are taking, and can set tiers for interfaces such as login, comment, and background. The judgment sequence is to first see which type of interface is being flooded - if it is the entire site page, use the gateway layer, and if it is a specific business action, limit it according to the action in the program layer.
What do the two layers of speed limiting block separately
The gateway layer's rate limiting dimension is by address: it counts the rate by the source address and rejects when exceeding. It does not care which business the request belongs to, nor does it matter whether the caller is logged in. The benefit is that it does not consume application resources, but the drawback is that normal users under the same exit address may be limited together.The program layer can read identity and actions, and can set search, message, and background login thresholds for the same address separately. The cost is that each request has to enter the program before counting starts.In the official description, the loading speed and concurrent承载 of this type of site is an advantage over traditional PHP systems, capable of handling about 5 million PVs on a single machine - but this is a capacity description and cannot replace speed limits: a large capacity does not mean that abnormal traffic should not be restricted.
How to write the two parameters of rate and burst volume
For example, with nginx's rate limiting module, the rule first declares a shared memory area, and then retrieves the key by dimension. The document specifies that the rate is expressed in requests per second (r/s);The burst quantity (burst) defaults to zero, which means that it cannot handle anything beyond the rate. The actual format is that the rate provides a stable line, while burst provides a temporary surplus that can pass without delay: when burst is zero, any instantaneous fluctuation will trigger a limit, and a normal user refreshing a little faster may be rejected;Provide a small burst, which means allowing short bursts. There is also a delay method where requests exceeding the rate are queued instead of being rejected directly, the document also states that requests exceeding the rate will be delayed until the number exceeds the maximum burst size, at which point they will be terminated with an error.
limit_req_zone $binary_remote_addr zone=api:10m rate=5r/s;
limit_req zone=api burst=10 nodelay;
Which error code should be returned for exceeding the limit
By default, a rejected request returns 503, and this status code can be changed in the example configuration with limit_req_status.The selection matters: 503 indicates temporarily unavailable, with a retry semantics that are clear, suitable for throttling; 429 is a specific request too many semantics, making it easier for the caller to recognize it as 'throttled' rather than 'service is down.'It is not recommended to return 500, as it will mix speed limits with program failures, misleading the troubleshooting direction. The default listening port within the site is 8001, and when the gateway is proxying in front, the speed limit return code should be set in the gateway configuration;Under the form that directly interacts with the program, the speed limit logic must be borne by the application itself. The deployment form determines where this parameter is written in the configuration document.
Which interfaces should be limited and which should not
| Interface Type | Suggested level | Reason | Risk of misfire |
|---|---|---|---|
| Search and filter | Gateway + Program | High cost, easy to be scanned by scripts | Low |
| Leave comments and submit reviews | Program | Distinguish between login and user groups | Low |
| Back-end login | Program is the main one | Need to judge according to account and action | Low |
| Content images and static resources | Be cautious of restrictions | Multiple requests on the same page will interrupt loading due to speed limits | High |
| Normal page | Relaxed | Affects user experience and crawling | High |
What should not be restricted is static resources and normal page browsing: the same page will request multiple images and styles concurrently. Tight control by address dimension will cause the page to be incomplete. The address segment of the crawling tool also needs to be reserved separately, otherwise speed limits will directly affect the inclusion and visibility of the content.
Common questions
Question: Is it enough to configure gateway speed limits?Answer: It is sufficient when it comes to brushing the entire site traffic. For brushing volume of a specific business interface, the gateway cannot control the specific action by address dimension, and it depends on the program layer to categorize.
Ask: How much should burst be set to?Answer: Starting from the behavior of normal users: Allow a short burst without queuing, set the value so that normal refreshes are not rejected, there is no general value. After modification, observe the rejection rate and adjust accordingly.
Ask: How to see who was limited after speed limiting in the log?Answer: Look at the aggregation of the source address corresponding to the status code in the gateway access log, and judge whether it is a single address or a group of users from the same exit with the key of the speed limit area (default by source address).