Throttling a tiered, multi-tenant REST API at scale using API Gateway: Part 2
AWS Architecture BlogThrottling a tiered, multi-tenant REST API at scale using API Gateway: Part 2by Nick Choi | on09 MAY 2022| inAdvanced (300),Amazon API Gateway,Amazon Cognito,Amazon DynamoDB,Architecture,AWS Amplify,AWS Lambda|Permalink|ShareInPart 1of this blog series, we demonstrated why tiering and throttling become necessary at scale for multi-tenant REST APIs, and explored tiering strategy and throttling withAmazon API Gateway.In this post, Part 2, we will examine tenant isolation strategies at scale with API Gateway and extend the sample code from Part 1.Enhancing the sample codeTo enable this functionality in the sample code (Figure 1), we will make manual changes. First, create one API key for the Free Tier and five API keys for the Basic Tier. Currently, these API keys are private keys for yourAmazon Cognitologin, but we will make a further change in the backend business logic that will promote them to pooled resources. Note that all of these modifications are specific to this sample code’s implementation; the implementation and deployment of a production code may be completely different (Figure 1).Figure 1. Cloud architecture of the sample codeNext, in the business logic for thecreateKey(), find the AWS Lambda function inlambda/create_key.js.  It appears like this:function createKey(tableName, key, plansTable, jwt, rand, callback) {
  const pool = getPoolForPlanId( key.planId ) 
  if (!pool) {
    createSiloedKey(tableName, key, plansTable, jwt, rand, callback);
  } else {
    createPooledKey(pool, tableName, key, jwt, callback);
  }
}ThegetPoolForPlanId()function does a search for a pool of keys associated with the usage plan. If there is a pool, we “create” a kind of reference to the pooled resource, rather than a completely new key that is created by the API Gateway service directly. Thelambda/api_key_pools.jsshould be empty.exports.apiKeyPools = [];In effect, all usage plans were considered as siloed keys up to now. To change that, populate the data structure with values from the six API keys that were created manually. You will have to look up the IDs of the API keys and usage plans that were created in API Gateway (Figures 2 and 3). Using the AWS console to navigate to API Gateway is the most intuitive.Figure 2. A view of the AWS console when inspecting the ID for the Basic usage planFigure 3. A view of the AWS Console when looking up the API key value (not the ID)When done, your code inlambda/api_key_pools.jsshould be the following, but instead of ellipses (…), the IDs for the user plans and API keys specific to your environment will appear.exports.apiKeyPools = [{
    planName: "FreePlan"
    planId: "...",
    apiKeys: [ "..." ]
  },
 {
    planName: "BasicPlan"
    planId: "...",
    apiKeys: [ "...", "...", "...", "...", "..." ]
  }];After making the code changes, runcdk deployfrom the command line to update the Lambda functions. This change will only affect key creation and deletion because of the system implementation. Updates affect only the user’s specific reference to the key, not the underlying resource managed by API Gateway.When the web application is run now, it will look similar to before—tenants should not be aware what tiering strategy they have been assigned to. The only way to notice the difference would be to create two Free Tier keys, test them, and note that the value of theX-API-KEYheader is unchanged between the two.Now, you have a virtually unlimited number of users who can have API keys in the Free or Basic Tier. By keeping the Premium Tier siloed, you are subject to the 10,000-API-key maximum (less any keys allocated for the lower tiers). You may consider additional techniques to continue to scale, such as replicating your service in another AWS account.Other production considerationsThe sample code is minimal, and it illustrates just one aspect of scaling a Software-as-a-service (SaaS) application. There are many other aspects be considered in a production setting that we explore in this section.The throttled endpoint,GET /apirely only on API key for authorization for demonstration purpose. For any production implementation considerauthenticationoptions for your REST APIs. You may explore and extend to require authentication with Cognito similar to/admin/*endpoints in the sample code.One API key for Free Tier access and five API keys for Basic Tier access are illustrative in a sample code but not representative of production deployments. Number of API keys with service quota into consideration, business and technical decisions may be made to minimize noisy neighbor effect such as setting blast radius upper threshold of 0.1% of all users. To satisfy that requirement, each tier would need to spread users across at least 1,000 API keys. The number of keys allocated to Basic or Premium Tier would depend on market needs and pricing strategies. Additional allocations of keys could be held in reserve for troubleshooting, QA, tenant migrations, and key retirement.In the planning phase of your solution, you will decide how many tiers to provide, how many usage plans are needed, and what throttle limits and quotas to apply. These decisions depend on your architecture and business.To define API request limits, examine the system API Gateway is protecting and what load it can sustain. For example, if your service will scale up to 1,000 requests per second, it is possible to implement three tiers with a 10/50/40 split: the lowest tier shares one common API key with a 100 request per second limit; an intermediate tier has a pool of 25 API keys with a limit of 20 requests per second each; and the highest tier has a maximum of 10 API keys, each supporting 40 requests per second.Metrics play a large role in continuously evolving your SaaS-tiering strategy (Figure 4). They provide rich insights into how tenants are using the system. Tenant-aware andSaaS-wide metricson throttling and quota limits can be used to: assess tiering in-place, if tenants’ requirements are being met, and if currently used tenant usage profiles are valid (Figure 5).Figure 4. Tiering strategy example with 3 tiers and requests allocation per tierFigure 5. An example SaaS metrics dashboardAPI Gateway provides options for different levels of granularity required, includingdetailed metrics, andexecution and access loggingto enable observability of your SaaS solution. Granular usage metrics combined with underlying resource consumption leads to managing optimal experience for your tenants with throttling levels and policies per method and per client.CleanupTo avoid incurring future charges, delete the resources. This can be done on the command line by typing:cd ${TOP}/cdk
cdk destroy

cd ${TOP}/react
amplify delete${TOP}is the topmost directory of the sample code. For the most up-to-date information, see the README.md file.ConclusionIn this two-part blog series, we have reviewed the best practices and challenges of effectively guarding a tiered multi-tenant REST API hosted in AWS API Gateway. We also explored how throttling policy and quota management can help you continuously evaluate the needs of your tenants and evolve your tiering strategy to protect your backend systems from being overwhelmed by inbound traffic.Further reading:AWS Well-Architected SaaS Lens Performance Efficiency pillarAWS Serverless SaaS WorkshopSaaS Factory Serverless SaaS reference solutionThis series was co-authored by Gary Kumfert, PhD, former Principal Solutions Architect at AWS.TAGS:saas,Software as a ServiceNick ChoiNick Choi is a Solutions Architect with Amazon Web Services. He works with software and SaaS customers to design, build, operate, and evolve their SaaS service on AWS. He is passionate about understanding why and how SaaS businesses are changing way we live, work, and play.
In Part 1 of this blog series, we demonstrated why tiering and throttling become necessary at scale for multi-tenant REST APIs, and explored tiering strategy and throttling with Amazon API Gateway. In this post, Part 2, we will examine tenant isolation strategies at scale with API Gateway and extend the sample code from Part 1. […]
Nick Choi
Advanced (300) Amazon API Gateway Amazon Cognito Amazon DynamoDB Architecture AWS Amplify AWS Lambda