Setting up Quotas in CE

Hi,

I am unable to get global rate limit working. I have it setup as below but I am able to hit the endpoint as many times as I want. This is keyless.

"disable_quota":false,
"quota_max" : 5,
"quota_remaining":5,
"quota_renewal_rate":30

I got rate limit to work per api. I haven’t tried with key. Can both rate limit and quota be used per key on top of api?

Can they be used as a setting for all apis in one go?

UPDATE: I actually posted the wrong snippet. It was for rate limit. I meant to ask about quota. Is quota meant to be used with key only? I am trying to setup quota for both key and api level.

I’ve also added a ./policy/policy.json with contents below (and tinkered with it few times).

{
    "default": {
        "rate": 1000,
        "per": 1,
        "quota_max": 10,
        "quota_renewal_rate": 60,
        "access_rights": {
          "41433797848f41a558c1573d3e55a410": {
            "api_name": "Quota test api",
            "api_id": "41433797848f41a558c1573d3e55a410",
            "versions": [
              "Default"
            ]
          }
        },
        "active": true,
        "state": "active", 
        "org_id": "54de205930c55e15bd000001",
        "hmac_enabled": false
    }
  }
  

It is version 3.0 using docker-compose.

version: '3.3'
services:
  tyk-gateway:
    image: tykio/tyk-gateway:v3.0.4
    ports:
      - 8080:8080
    networks:
      - tyk
    volumes:
      - ./tyk.standalone.conf:/opt/tyk-gateway/tyk.conf
      - ./apps:/opt/tyk-gateway/apps
      - ./middleware:/opt/tyk-gateway/middleware
      - ./certs:/opt/tyk-gateway/certs
      - ./policies/policies.json:/opt/tyk-gateway/policies/policies.json
    environment:
      - TYK_GW_SECRET=bar
    depends_on:
      - tyk-redis
  tyk-redis:
    image: redis:5.0-alpine
    networks:
      - tyk
    ports:
      - 6379:6379
    volumes:
      - redis_data:/data

networks:
  tyk:
volumes:
  redis_data:

Policy mapping in tyk.standalone.conf

  "policies": {
    "policy_source": "file",
    "policy_record_name": "/opt/tyk-gateway/policies/policies.json"
  },

The problem I am facing is quota does not work regardless (policy or within the app definition)

Hi @tej-rana, could you answer a couple of questions.

I am unable to get global rate limit working. I have it setup as below but I am able to hit the endpoint as many times as I want

Have you set “disable_rate_limit”: false? I do not see the value based on your snippet.

I got rate limit to work per api. I haven’t tried with key

Could you explain further?. The API-level global limit is set within the API definition file, as shared above. Do you mean you got it working that way? If not then what way did you get it working without a key? Via a policy?

Can they be used as a setting for all apis in one go?

Do you have a use case? We can advise better if you share what you want to achieve

Sorry I had poorly written question. I have updated the code snippet and my question is in the update part only. I just need help with quotas - setting at api level as well as key level.

Firstly of all (just to clarify), you cannot set quota on the API definition. You can only enable or disable it

To set the quota for a key and also for a specific API, you would need to enable partitioned policies. Partitioned policies can enforce the same access rights and rate limits on all API but each API can have different quotas. For example, the policy below shows API 02 has a different quota set than API 01 which inherits from the policy quota set.

{
	"default": {
		"access_rights": {
			"{API 01 ID}": {
				"api_id": "{API 01 ID}",
				"api_name": "{API 01 NAME}",
				"versions": [
					"Default"
				]
			},
			"{API 02 ID}": {
				"allowance_scope": "",
				"allowed_urls": [
					{
						"url": "/anything/(.*)",
						"methods": [
							"GET",
							"POST"
						]
					}
				],
				"api_id": "{API 02 ID}",
				"api_name": "{API 02 NAME}",
				"limit": {
					"max_query_depth": -1,
					"per": -1,
					"quota_max": 20,
					"quota_renews": 0,
					"quota_remaining": 20,
					"quota_renewal_rate": 120,
					"rate": -1,
					"set_by_policy": true,
					"throttle_interval": -1,
					"throttle_retry_limit": -1
				},
				"restricted_types": [],
				"versions": [
					"Default"
				]
			}
		},
		"active": true,
		"id": "default",
		"is_inactive": false,
		"key_expires_in": -1,
		"org_id": "default",
		"name": "default",
		"partitions": {
			"acl": false,
			"complexity": false,
			"per_api": true,
			"quota": false,
			"rate_limit": true
		},
		"per": 1,
		"quota_max": 10000,
		"quota_renewal_rate": 3600,
		"rate": 100,
		"tags": [],
		"throttle_interval": -1,
		"throttle_retry_limit": -1
	}
}

Last but not least, you would to apply a policy / policies to the key. I have attached a snippet below

"apply_policies": [
		"{POLICY 01 ID}",
		"{POLICY 02 ID}",
]

Hope this helps