Mastering calculation of Query Per Second - QPS
What is QPS?
QPS, or Queries Per Second, is a measure of how many requests your system handles in one second. It’s an important metric to understand the load on your system and helps estimate the infrastructure needed to support that load. QPS is critical for planning and designing scalable systems that can handle current and future traffic.
When designing a system, you need to estimate QPS to ensure that it can handle both average daily traffic and peak traffic loads efficiently. This tutorial will walk through three different examples of estimating QPS: a system with consistent 24-hour traffic, a system with daily active users and periodic traffic, and a system that experiences peak traffic at specific hours.
URL Shortener - 24 Hours
Example 1: Estimating QPS for a URL Shortener (24-Hour Traffic)
A URL shortener is a service where users create shortened links, and other users resolve those links to their full URLs. For a system like this, traffic is typically steady throughout the day because users access and share shortened URLs at various times. There are usually no significant peak hours, making this system ideal for calculating QPS over a 24-hour period.
Steps to Estimate QPS
- Estimate Total Requests: Let’s assume the URL shortener handles 1 million requests in a day, including both URL creation and resolution.
- Even Distribution: For this system, traffic is assumed to be evenly spread across the entire day.
- Calculate QPS Over 24 Hours: We will calculate the QPS by dividing the total requests by the total time in seconds (24 hours).
Calculating QPS
- Total Requests: 1,000,000 requests per day.
- Time Period: A day has 86,400 seconds (24 hours × 60 minutes × 60 seconds).
- QPS: 1,000,000 requests ÷ 86,400 seconds = ~11.6 QPS.
For this URL shortener, the system needs to handle an average of 12 QPS throughout the day.
Key Considerations
- Consistent Load: Since there are no significant peaks, the system needs to be designed to handle a steady load.
- Caching: Frequently accessed URLs can be cached, reducing the number of queries that hit the backend, further lowering the QPS load on the database.
Social Media - By Daily Active Users
Example 2: Estimating QPS for a Social Media Platform (Daily Active Users)
Let’s now look at a social media platform, such as a service similar to Twitter. Users visit the platform, post updates, like posts, and browse content. The traffic varies throughout the day, but we’ll focus on calculating the QPS based on total daily active users and their average activity.
Steps to Estimate QPS
- Estimate Active Users: Suppose the platform has 10 million registered users, and 10% of them are active daily. That gives us 1 million daily active users.
- Requests Per User: On average, each user performs 20 actions per day, including posting, liking, and commenting.
- Calculate QPS Over 24 Hours: Since we’re estimating for a full day, we assume traffic is spread out, although there may be some minor peaks.
Calculating QPS
- Total Requests: 1 million active users × 20 actions per user = 20,000,000 requests per day.
- Time Period: A day has 86,400 seconds.
- QPS: 20,000,000 requests ÷ 86,400 seconds = ~231 QPS.
In this case, the social media platform needs to handle an average of 231 QPS throughout the day.
Key Considerations
- Peak vs. Off-Peak Traffic: Even though this example assumes a steady flow of traffic throughout the day, the platform might see higher traffic during certain hours (e.g., evenings or special events). Therefore, it’s important to plan for peak traffic as well.
- Traffic Spikes: During major events, such as a trending topic or a viral post, the platform could experience traffic spikes much higher than the average daily QPS.
E-commerce - By Peak Hours
Example 3: Estimating QPS for an E-commerce Platform (Peak Traffic)
In this example, let’s estimate the QPS for an e-commerce platform. Unlike the previous examples, an e-commerce platform often sees peak traffic during specific times, such as flash sales or promotions. This makes it essential to calculate QPS based on peak hours rather than evenly distributed traffic.
Steps to Estimate QPS
- Estimate Active Shoppers: Suppose the platform has 500,000 active shoppers during a big sale event.
- Requests per Shopper: Each shopper performs an average of 10 actions (browsing products, adding to cart, and checking out) during the sale.
- Peak Traffic Window: Let’s assume the peak traffic happens over a 4-hour period (14,400 seconds).
Calculating QPS
- Total Requests: 500,000 active shoppers × 10 actions per shopper = 5,000,000 requests during the peak hours.
- Time Period: Peak hours = 14,400 seconds (4 hours).
- QPS: 5,000,000 requests ÷ 14,400 seconds = ~347 QPS.
For the e-commerce platform, during peak traffic, the system needs to handle around 347 QPS.
Key Considerations
- Handling Traffic Spikes: Since peak hours generate significantly higher traffic, the system must be designed to handle much more than the average QPS. Load balancing and auto-scaling can help manage these surges effectively.
- Caching and Pre-fetching: To handle high traffic during sales, the platform can use caching for product pages and pre-fetching to speed up the user experience, reducing the number of requests hitting the database.
Other Ways to Calculate QPS
While the examples above show standard approaches to calculating QPS, there are other methods depending on the nature of the system:
1. Monitoring Tools
If your system is already live, using monitoring tools such as Google Analytics, AWS CloudWatch, or Datadog can give you real-time data on QPS. These tools track how many requests are being processed and can alert you when traffic spikes occur.
2. Load Testing
Before launching a system, load testing tools like Apache JMeter or Locust can simulate real traffic to measure how the system handles different QPS levels. This helps ensure the system is ready to handle the expected load and can scale when necessary.
3. Traffic Logs
For systems that already generate logs, you can analyze the logs to calculate QPS over time. By reviewing server logs, you can track the number of requests processed during specific time periods and calculate QPS based on actual usage patterns.
4. Estimating Based on Growth
If your system is still in development or recently launched, you can estimate QPS based on projected user growth. For instance, if you know that your platform expects to grow by 50% in the next year, you can scale up your QPS estimates accordingly to ensure the system is prepared for increased traffic.
Conclusion
Estimating QPS is a vital part of designing scalable systems. Whether you’re working with a system that experiences steady traffic, like a URL shortener, or one that faces traffic surges during peak hours, like an e-commerce platform, understanding QPS helps you ensure your system can handle the load. By calculating QPS for different scenarios, such as full-day usage or peak traffic, you can design a system that’s prepared for both average traffic and unexpected spikes.
Remember, while calculating QPS is a crucial first step, planning for growth, traffic surges, and failover strategies are equally important to ensure your system performs well under all conditions.