You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: throttling/README.md
+83-60Lines changed: 83 additions & 60 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,46 +1,49 @@
1
1
---
2
2
title: Throttling
3
-
category: Behavioral
3
+
category: Resource management
4
4
language: en
5
5
tag:
6
-
- Performance
7
-
- Cloud distributed
6
+
- API design
7
+
- Fault tolerance
8
+
- Performance
9
+
- Resilience
10
+
- Scalability
8
11
---
9
12
13
+
## Also known as
14
+
15
+
* Rate Limiting
16
+
10
17
## Intent
11
18
12
-
Ensure that a given client is not able to access service resources more than the assigned limit.
19
+
Throttling limits the number of requests a system can process within a given time frame to prevent overload and ensure stability.
13
20
14
21
## Explanation
15
22
16
23
Real-world example
17
24
18
-
> A young human and an old dwarf walk into a bar. They start ordering beers from the bartender.
19
-
> The bartender immediately sees that the young human shouldn't consume too many drinks too fast
20
-
> and refuses to serve if enough time has not passed. For the old dwarf, the serving rate can
21
-
> be higher.
25
+
> Imagine a popular amusement park that limits the number of visitors who can enter per hour to prevent overcrowding. This ensures that all visitors can enjoy the park without long wait times and maintain a pleasant experience. Similarly, the Throttling design pattern in software controls the rate of requests to a system, preventing it from being overwhelmed and ensuring consistent performance for all users.
22
26
23
27
In plain words
24
28
25
-
> Throttling pattern is used to rate-limit access to a resource.
29
+
> Throttling pattern is used to rate-limit access to a resource.
> Control the consumption of resources used by an instance of an application, an individual tenant,
30
-
> or an entire service. This can allow the system to continue to function and meet service level
31
-
> agreements, even when an increase in demand places an extreme load on resources.
33
+
> Control the consumption of resources used by an instance of an application, an individual tenant, or an entire service. This can allow the system to continue to function and meet service level agreements, even when an increase in demand places an extreme load on resources.
32
34
33
35
**Programmatic Example**
34
36
35
-
`BarCustomer` class presents the clients of the `Bartender` API. `CallsCount` tracks the number of
36
-
calls per `BarCustomer`.
37
+
In this example a young human and an old dwarf walk into a bar. They start ordering beers from the bartender. The bartender immediately sees that the young human shouldn't consume too many drinks too fast and refuses to serve if enough time has not passed. For the old dwarf, the serving rate can be higher.
38
+
39
+
`BarCustomer` class presents the clients of the `Bartender` API. `CallsCount` tracks the number of calls per `BarCustomer`.
`Bartender` offers the `orderDrink` service to the `BarCustomer`s. The customers probably don't
111
-
know that the beer serving rate is limited by their appearances.
111
+
`Bartender` offers the `orderDrink` service to the `BarCustomer`s. The customers probably don't know that the beer serving rate is limited by their appearances.
112
112
113
113
```java
114
114
classBartender {
@@ -129,7 +129,7 @@ class Bartender {
129
129
return-1;
130
130
}
131
131
callsCount.incrementCount(tenantName);
132
-
LOGGER.debug("Serving beer to {} : [{} consumed] ", barCustomer.getName(), count+1);
132
+
LOGGER.debug("Serving beer to {} : [{} consumed] ", barCustomer.getName(), count+1);
133
133
return getRandomCustomerId();
134
134
}
135
135
@@ -139,8 +139,7 @@ class Bartender {
139
139
}
140
140
```
141
141
142
-
Now it is possible to see the full example in action. `BarCustomer` young human is rate-limited to 2
143
-
calls per second and the old dwarf to 4.
142
+
Now it is possible to see the full example in action. `BarCustomer` young human is rate-limited to 2 calls per second and the old dwarf to 4.
144
143
145
144
```java
146
145
publicstaticvoid main(String[] args) {
@@ -205,14 +204,38 @@ An excerpt from the example's console output:
205
204
206
205
## Class diagram
207
206
208
-

207
+

209
208
210
209
## Applicability
211
210
212
-
The Throttling pattern should be used:
211
+
* You need to protect resources from being overwhelmed by too many requests.
212
+
* You want to ensure fair usage of a service among multiple users.
213
+
* You need to maintain the quality of service under high load conditions.
214
+
215
+
## Known Uses
216
+
217
+
* APIs of major cloud providers like AWS, Google Cloud, and Azure use throttling to manage resource usage.
218
+
* Web services to prevent denial-of-service (DoS) attacks by limiting the number of requests from a single IP address.
219
+
* Online platforms like social media sites and e-commerce websites to ensure even distribution of server load.
220
+
221
+
## Consequences
222
+
223
+
Benefits:
224
+
225
+
* Prevents resource exhaustion, ensuring system stability.
226
+
* Helps in maintaining consistent performance and quality of service.
227
+
* Improves fault tolerance by avoiding system crashes under high load.
228
+
229
+
Trade-offs:
230
+
231
+
* May cause increased latency or delay in request processing.
232
+
* Requires careful tuning to balance between resource protection and user experience.
233
+
* Could lead to denial of service to legitimate users if not configured correctly.
234
+
235
+
## Related Patterns
213
236
214
-
*When service access needs to be restricted not to have high impact on the performance of the service.
215
-
*When multiple clients are consuming the same service resources and restriction has to be made according to the usage per client.
237
+
*[Circuit Breaker](https://java-design-patterns.com/patterns/circuit-breaker/): Works in tandem with throttling to prevent repeated attempts to access an overloaded service.
238
+
*Bulkhead: Isolates different parts of the system to limit the impact of throttling on other components.
0 commit comments