Rate Limit Best Practices
Plan request concurrency, gradual ramp-up, and monitoring for Switch API workflows.
Use these practices to keep high-volume Switch API workflows within safe concurrency and ramp-up patterns.
What defines a task
A task is a unit of work in your workflow, such as making an API request and processing its response. Examples include:
- API Calls: Making requests to our APIs and processing their responses. Examples include:
- Fetching tariffs
- Viewing territories
- Viewing tariff properties
- Running calculations
Concurrency in a workflow
Concurrency refers to the system managing and scheduling multiple tasks simultaneously to improve efficiency. Here's how it fits into your specific needs:
- Multiple Tasks: Handle several tasks at the same time, such as fetching utilities, retrieving tariffs, viewing tariff properties, and running calculations.
- Overlapping Tasks: Allow tasks to overlap in time, for example, retrieving tariff details while running calculations for previously retrieved tariffs.
- Efficiency: Improves efficiency by utilizing CPU time and other system resources effectively.
Key concepts
- Concurrency Limit: The maximum number of simultaneous API calls to avoid overloading the system.
- Gradual Ramp-Up: Increasing concurrency gradually over 8–10 minutes helps avoid sudden spikes in system load.
- Monitoring: Continuously monitor system performance to dynamically adjust concurrency settings.
Example workflow for acceptable use
Scenario
You run 1,000 API calls with these limits:
- A concurrency limit of 4
- A gradual ramp-up of request rates over 8–10 minutes
Steps
-
Initial Setup:
- Fetch a small set of utilities, retrieve matching tariffs, and run a few initial calculations.
- Monitor system response times and error rates.
-
Gradual Ramp-Up:
- Start with a concurrency of 1 and gradually increase it to 4 over 8–10 minutes (e.g., add one concurrent request every 2–3 minutes).
-
Batch Processing:
- Process tariff lookups and calculations in small batches while respecting the concurrency limit.
- Introduce short delays between batches.
-
Monitoring and Adjustments:
- Continuously monitor response times and error rates.
- If response times or error rates increase, reduce the request rate or lengthen the delay between batches.
Example timeline
-
Minute 0–2:
- Start with 1 concurrent API call.
- Begin processing the first batch of 50 utility, tariff, or calculation requests.
-
Minute 2–4:
- Increase to 2 concurrent API calls.
- Two API calls are made simultaneously to process the batch.
-
Minute 4–6:
- Increase to 3 concurrent API calls.
- Three API calls are made simultaneously.
-
Minute 6–8:
- Increase to 4 concurrent API calls.
- Four API calls are made simultaneously until the batch is processed.
-
After Batch Completion:
- Introduce a short delay before starting the next batch of 50 requests.
- Monitor system performance during the delay.
Example workflow for unacceptable use
Scenario
In this counterexample, you run 1,000 API calls without following concurrency and ramp-up guidelines.
Issues
-
Immediate High Concurrency:
- Starts with a high concurrency of 50, exceeding the recommended limit.
- No gradual ramp-up, causing a sudden spike in system load.
-
No Batch Processing:
- Attempts to process all tariff lookups and calculations simultaneously.
-
No Monitoring:
- Ignores system performance metrics, leading to prolonged system strain.
Example timeline
- Minute 0:
- Starts with 50 concurrent requests.
- Attempts to process all 1,000 utility, tariff, or calculation requests simultaneously.
- Ignores increasing response times and error rates.
Summary
By following these best practices:
- Adhere to concurrency limits to prevent system overload.
- Gradually ramp up request rates over 8–10 minutes.
- Monitor system performance continuously. Reduce the request rate or lengthen batch delays if response times or error rates increase.
Failure to follow these guidelines can result in:
- Increased response times
- Higher error rates
- Potential system crashes
Updated 3 days ago
