Skip to content

Azure/rate-limiter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

69 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rate-limiter

rate limiter workflow

ratelimiter design (2)

counter

aggregated counting + local counting counter

In memory cache

https://github.com/patrickmn/go-cache

remote store

Option1: Azure Redis Cache

Option2: Redis cluster

rate limiting algorithm

Option1: token bucket

image

Implementation of a self-maintained token bucket cache:

Prerequist: have a redis cache to store key-value pair

Goal:

  1. Provide TakeToken and GetBucketStats api
  2. No need to a separte process to keep adding token to bucket or remove key-value pair from cache to prevent overuse memory

Implementation:

  1. Start each bucket full: image
  2. Each bucket auto expired after reach max tokens: image
  3. Only need to save token number and timestamp when bucket reached saved token number image

run the prototype

With azure redis example

  1. Create an Azure Redis Cache
export AZURE_SUBSCRIPTION_ID=8ecadfc9-d1a3-4ea4-b844-0d9f87e4d7c8
export AZURE_RESOURCE_GROUP=xinywaTestRG
export location=eastus

# create resouregroup
az group create --name $AZURE_RESOURCE_GROUP --location $location --subscription $AZURE_SUBSCRIPTION_ID

# create azure redis cache
export AZURE_REDIS_NAME=xinywaRedisCache
az redis create --location $location --name $AZURE_REDIS_NAME --resource-group $AZURE_RESOURCE_GROUP --subscription $AZURE_SUBSCRIPTION_ID --sku Basic --vm-size c0
  1. Set them to the respective environment variables
export AZURE_SUBSCRIPTION_ID=8ecadfc9-d1a3-4ea4-b844-0d9f87e4d7c8
export AZURE_RESOURCE_GROUP=xinywaTestRG
export AZURE_REDIS_NAME=xinywaRedisCache
export MSI_RESOURCE_ID="/subscriptions/8ecadfc9-d1a3-4ea4-b844-0d9f87e4d7c8/resourcegroups/xinywaTestRG/providers/Microsoft.ManagedIdentity/userAssignedIdentities/xinywamsi"
export MSI_OBJECT_ID=0a35e6d8-1026-4082-8890-8725c27e7594
  1. Clone the repo. Then in the terminal, run the following command to start the application.
cd test/withazureredis
go run main.go

The HTTP server will start on port 8080.

  1. Send request to test cache and throttle:

    Create request with billingAccount id1:

    curl -i -X POST -d '{"billingAccount":"id1"}' localhost:8080/billingAccount/

    Get bucket stats for billingAccount id1:

    curl -i localhost:8080/billingAccount/id1

With redis cluster example

  1. Deployer redis cluster (minimum 6 nodes)
kubectl apply -f rediscluster/redis-cluster.yaml
kubectl apply -f rediscluster/redis-configmap.yaml
kubectl apply -f rediscluster/redis-service.yaml
rediscluster/roles.sh
  1. Deploy test server and expose service
kubectl apply -f cmd/rediscluster/template/deployment.yaml
kubectl expose deployment rate-limit-server --name=rate-limit-svc --port=8080 --target-port=8080 --type=NodePort
kubectl port-forward svc/rate-limit-svc 8080:80
  1. Clone the repo. Then in the terminal, run the following command to start the application.
cd test/withrediscluster
go run main.go

The HTTP server will start on port 8080.

  1. Send request to test cache and throttle:

    Create request with billingAccount id1:

    curl -i -X POST -d '{"billingAccount":"id1"}' localhost:8080/billingAccount/

    Get bucket stats for billingAccount id1:

    curl -i localhost:8080/billingAccount/id1