EnricoFermi commited on
Commit
50ef9bf
·
verified ·
1 Parent(s): 5179a82

Upload benchmark/system_design.txt with huggingface_hub

Browse files
Files changed (1) hide show
  1. benchmark/system_design.txt +4 -22
benchmark/system_design.txt CHANGED
@@ -2,28 +2,10 @@ Prompt: Design a distributed rate limiter that works across multiple servers wit
2
 
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
- Here's an implementation of a distributed rate limiter using a sliding window algorithm and handling clock skew between nodes:
6
 
7
- ```python
8
- import time
9
- from collections import defaultdict, deque
10
- import threading
11
 
12
- class DistributedRateLimiter:
13
- def __init__(self):
14
- self.sliding_windows = defaultdict(lambda: deque())
15
- self.window_size = 60 # in seconds
16
- self.max_requests = 10 # maximum number of requests per window
17
- self.lock = threading.Lock()
18
 
19
- def _update_clock_skew(self):
20
- """Updates the clock skew between nodes."""
21
- current_time = time.time()
22
- expected_time = (current_time - (current_time % self.window_size)) + self.window_size
23
- return abs(current_time - expected_time) < 5
24
-
25
- def check_rate_limit(self, key):
26
- if not self._update_clock_skew():
27
- raise Exception("Clock skew is too high")
28
-
29
- with
 
2
 
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.