Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Effとリーキーバケットを使って実装するレートリミット

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
Avatar for kzmake kzmake
April 16, 2023

 Effとリーキーバケットを使って実装するレートリミット

Avatar for kzmake

kzmake

April 16, 2023
Tweet

Other Decks in Programming

Transcript

  1. Alp, inc. thealp.co.jp Scalebase • This is services for managing

    revenue in subscription-based businesses. • Can use integrate Scalebase into their services using our APIs. ΞϧϓͰ͸4DBMFCBTFͱ͍͏αϒεΫϦϓγϣϯϏδωεͷͨΊͷαʔϏεΛఏڙ͠ ͍ͯ·͢ɻ"1*ͷఏڙ΋։࢝͠·ͨ͠ʂ
  2. What is rate limit? • A technique for controlling the

    amount of traf fi c. • There are some algorithms: leaky bucket, token bucket, etc... ϨʔτϦϛοτ͸τϥϑΟοΫΛίϯτϩʔϧ͢ΔςΫχοΫͰ͢ɻΞϧΰϦζϜϞ σϧʹϦʔΩʔόέοτ౳͕͋Γ·͢ɻ
  3. Why is rate limit used? • Performance: Prevent performance degradation

    from overuse or burst. • Security: Protect from malicious attacks. • Monetization: Control the amount of req/ res through monetized APIs. ϨʔτϦϛοτΛ࢖͏Ϟνϕʔγϣϯʹ͸ɺύϑΥʔϚϯεɺηΩϡϦςΟɺϚωλ Πθʔγϣϯͱ͍ͬͨจ຺͕͋Δ͔΋ɻ
  4. Α͋͘ΔϨʔτϦϛοτΛߟ͑ͯΈ·͠ΐ͏ɻ࢓૊Έʹґଘ͠ɺίετͷௐ੔ʹ೰· ͞ΕΔ͔΋͠Ε·ͤΜɻ call api rate limit security performance monetization adapter

    usecase infrastructure domain complexity rate limit 1. per client IP address 2. per user multiple executions cost: 1 cost: 1 reconcile inconsistencies .
  5. ΋͠ɺϏδωε͕੒௕͠ෳࡶͳίετΛ௥Ճ͍ͨ͠ͱͳΔͱɺ5JFSͷಋೖ౳Λߟ͑ͳ ͚Ε͹͍͚ͳ͍͔΋͠Ε·ͤΜɻ call api rate limit security performance monetization adapter

    usecase infrastructure domain more complexity rate limit multiple executions cost: 2 cost: 1 cost: 1 apply cost: 3? or need tiers? 1. per client IP address 2. per user multiple costs reconcile inconsistencies . NEW!
  6. ͭͷࣜͱ࣮ߦͷ̏ͭʹ෼ׂ͠ɺϨʔτϦϛοτΛ࣮ݱ͠Α͏ͱࢼΈ·ͨ͠ɻ call api cost rate limit request throttle execution security

    performance monetization adapter usecase infrastructure domain expressions ᶃ ᶄ ᶅ Run (ᶃs << ᶄ) with ᶅ to make a rate limit interpretation
  7. ۩ମతʹɺϓϩάϥϜͷදݱͷͨΊಠࣗͷ5ISPUUMJOHΤϑΣΫτಋೖɺ࣮૷ʹϦʔ Ωʔόέοτͱ($3"Λద༻͠·͢ɻ 1. Original effect: Throttling using atnos-org/eff separate into

    expressions and executions 2. Leaky bucket use the scheduling process 3. GCRA(Generic cell rate algorithm) apply time-based leaky bucket algorithm
  8. Leaky bucket ϦʔΩʔόέοτͱ͸݀ͷۭ͍ͨόέπͷϞσϧͰ͢ɻυϦοϓϓϩηεΛ࣋ͪɺҰ ఆͷ଎౓Ͱ࿙Εग़͍͖ͯ·͢ɻ • With drip process. • A

    constant drip out. • Over fl ow when the average water injection rate exceeds the drip out rate. drip request A: request B: request C: leaky bucket burst full?
  9. Leaky bucket ࠷େ༰ྔ·Ͱ͍ͬͺ͍ʹͳΔͱड͚෇͚ͳ͍දݱɺͦΕΛϨʔτϦϛοτͱͯ͠ར༻ ͠·͢ɻ • With drip process. • A

    constant drip out. • Over fl ow when the average water injection rate exceeds the drip out rate. drip request A: request B: request C: leaky bucket burst full?
  10. GCRA ($3"͸ཧ࿦౸ୡ࣌ࠁ 5"5 Λ༻͍ͯɺ࣍ͷεςοϓͷ5"5ΛٻΊΔΞϧΰϦζϜͰ ͢ɻ • GCRA(Generic cell rate algorithm)

    is a leaky bucket scheduling algorithm. • Convert bucket size and current capacity to time- based leaky bucket. Update: Reject: Params: TATn+1 = { ta + q * T (n = 0) TATn + q * T (otherwise) TATn+1 − (τ + T) ≥ ta : time of request : leak interval : time capacity of bucket ( size) ta T τ = T * : number of cells : burst capacity q τ + T
  11. GCRA ֤߲͸࣌ؒతͳϦʔΩʔόέοτͷݱࡏ༰ྔ΍࠷େ༰ྔΛද͓ͯ͠ΓɺࠨͷࣜͰεέ δϡʔϧΞϧΰϦζϜΛදݱ͠·͢ɻ • GCRA(Generic cell rate algorithm) is a

    leaky bucket scheduling algorithm. • Convert bucket size and current capacity to time- based leaky bucket. Update: Reject: Params: TATn+1 = { ta + q * T (n = 0) TATn + q * T (otherwise) TATn+1 − (τ + T) ≥ ta : time of request : leak interval : time capacity of bucket ( size) ta T τ = T * : number of cells : burst capacity q τ + T
  12. ࠷΋஫໨͢Δ΂͖఺͸ɺυϦοϓϓϩηεͷ࣮૷ͳ͠ͰϦʔΩʔόέοτΛΤϛϡ ϨʔτͰ͖Δ͜ͱͰ͢ɻ • Store only one parameter: Theoretical Arrival Time

    ( ). • Without dripping process. • Can be implemented using only the SETEX (= SET and EXPIRE) command provided by Redis. TATn Update: Reject: Params: TATn+1 = { ta + q * T (n = 0) TATn + q * T (otherwise) TATn+1 − (τ + T) ≥ ta : time of request : leak interval : time capacity of bucket ( size) ta T τ = T * : number of cells : burst capacity q τ + T GCRA
  13. ͭ·Γɺ5"5ͷΈΛߋ৽͠ӬଓԽ͢Δ͚ͩͰදݱͰ͖ɺ3BEJTͰఏڙ͞ΕΔ4&5&9ͩ ͚Ͱ࣮૷Ͱ͖Δ΋ͷͱͳͬͯ·͢ɻ • Store only one parameter: Theoretical Arrival Time

    ( ). • Without dripping process. • Can be implemented using only the SETEX (= SET and EXPIRE) command provided by Redis. TATn Update: Reject: Params: TATn+1 = { ta + q * T (n = 0) TATn + q * T (otherwise) TATn+1 − (τ + T) ≥ ta : time of request : leak interval : time capacity of bucket ( size) ta T τ = T * : number of cells : burst capacity q τ + T GCRA
  14. Implement interpreter ίετͷҡ࣋ʹ4UBUFΤϑΣΫτɺ5"5ͷӬଓԽͰ,74UPSFΤϑΣΫτɺΤϥʔͷͨ Ί&JUIFSΤϑΣΫτͰղऍͯ͠Έ·͢ɻ "Throttling" effect interpret • "State" effect

    for maintaining the map of key/cost. • "KVStore" Effect for storing the TAT. • "Either" effect for rate limit error.
  15. ·ͱΊ • It is possible to write programs fl exibly

    by introducing the "Throttling" effect. • By switching the implementation of the interpreter, it is possible to adopt different algorithms as well. • Removing `<< throttle[R]` makes it easy to disable rate limiting without implementing a no-op interpreter. .