跳轉到

Introduction

If you have dealt with any backend services before, you have probably heard of the term Rate Limit.

Without this crucial toolkit, clients can make as many requests as they want to your service at any point in time. This leads to a sudden traffic spike, thus, hoarding your servers.

Let’s return to the fundamentals and discuss commonly used rate-limiting algorithms.

Leaky Bucket

graph TD
    A[Incoming packet] --> B{Is bucket full?}
    B --> |No| C[Add packet to bucket]
    B --> |Yes| D[Drop packet]
    C --> E{Is packet size <= available space in bucket?}
    E --> |Yes| F[Update bucket size]
    F --> G{Is there more data in the input stream?}
    G --> |Yes| A
    G --> |No| H[Wait for next tick]
    E --> |No| I[Drop packet]
    I --> G
    D --> G
    H --> A

Reference