Skip to content
Justin Liu edited this page Oct 31, 2018 · 30 revisions

實作內容:

線上預覽

http://ratelimit.justintw.com/

演算法:

Token Bucket

已實作的儲存資料庫:

  1. Redis (提供 production 環境使用)
  2. Memory (提供 unit test 環境使用)

🤔 如何選擇資料庫:

Rate Limit 通常用在搶票系統,該情境須滿足低延遲 (Low Latency) 與支援高併發 (High Concurrency)等特性,因此 In-Memory 資料庫較為適合。常見(支援) In-Memory 資料庫有:

* Redis
* Memcached
* MySQL (支援 In-Memory Engine)
* Mongodb (企業版支援 In-Memory Engine)

註: MySQL 雖也有提供 In-Memory 儲存引擎,但其處理交易採用 Table Lock 的方式,高併發時可能會有效能上的問題。

執行方式:

  1. docker-compose
  2. Makefile (僅在 Linux 環境測試過)

(詳見 README.md)

額外說明:

實務上,佈署到生產環境 ( Production ) 可能會有多伺服器共同存取 Redis,然而 Redis API 操作在高併發時無法保證交易原子性。 本次實作依據 Redis 官方說明,自訂 Lua 腳本來存取 Redis 可保證高併發下的原子性。

Clone this wiki locally