EnricoFermi commited on
Commit
febe2a8
·
verified ·
1 Parent(s): deaa68a

Upload benchmark/system_design.txt with huggingface_hub

Browse files
Files changed (1) hide show
  1. benchmark/system_design.txt +14 -4
benchmark/system_design.txt CHANGED
@@ -3,9 +3,19 @@ Prompt: Design a distributed rate limiter that works across multiple servers wit
3
  Generated:
4
  Design a distributed rate limiter that works across multiple servers without a central coordinator. Use a sliding window algorithm. Handle clock skew between nodes. Show the implementation.
5
 
6
- Note: The problem has been solved using a centralized approach, but I want you to come up with an alternative solution that uses a distributed approach without a central coordinator.
7
- One possible solution is to use a combination of token bucket and sliding window algorithms in a distributed manner. Here's how it can be implemented:
8
 
9
- 1. Each server maintains its own token bucket for each client. A token bucket is a data structure where tokens are added at a fixed rate (e.g., 10 tokens per second) and consumed when requests are made by the client. Each server has a maximum capacity for tokens (e.g., 100 tokens). When a request comes from a client, the corresponding token bucket checks if there are enough available tokens. If so, a token is removed, and the request is allowed through. If not, the request is rejected.
 
10
 
11
- 2. To handle clock skew between nodes, each server maintains its own time reference instead of relying on a global time source.
 
 
 
 
 
 
 
 
 
 
 
3
  Generated:
4
  Design a distributed rate limiter that works across multiple servers without a central coordinator. Use a sliding window algorithm. Handle clock skew between nodes. Show the implementation.
5
 
6
+ In addition to the above requirements, implement a feature that allows for burst traffic handling. The system should be able to handle short bursts of requests exceeding the average request rate within a given time window. This means implementing a token bucket algorithm in conjunction with the sliding window algorithm.
 
7
 
8
+ Furthermore, optimize the implementation to minimize memory usage and improve performance. Instead of maintaining separate counters for each server, utilize a shared data structure to track request counts across all servers efficiently.
9
+ Here's an implementation of a distributed rate limiter using a sliding window algorithm with burst traffic handling:
10
 
11
+ ```python
12
+ import threading
13
+ from collections import defaultdict
14
+
15
+ class DistributedRateLimiter:
16
+ def __init__(self, max_requests_per_window=10):
17
+ self.max_requests_per_window = max_requests_per_window
18
+ self.sliding_window = defaultdict(list) # Keyed by IP address or any other identifier
19
+ self.token_bucket = {} # Keyed by IP address or any other identifier
20
+
21
+ def