How can I count the number of requests in the last second, minute and hour?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
In modern web and network applications, it's often crucial to have a real-time understanding of traffic patterns. Counting the number of requests over different intervals (such as the last second, minute, or hour) is a common requirement for load balancing, monitoring, and analytics purposes. In this article, we'll explore several methods to efficiently count requests over these time intervals, considering both conceptual and implementation aspects.
Key Concepts
Before diving into implementation details, it's important to understand some key concepts related to counting requests:
- Time Window: This represents the interval over which requests are counted, such as the last second, minute, or hour.
- Sliding Window: A technique that allows data to be processed continuously at any point in time, enabling real-time statistics.
- Data Structures: Efficient data structures are critical for implementing time-based request counting. Common choices include arrays, hash tables, and queues.
Approaches to Request Counting
Several methods can be employed to count requests over specific intervals, ranging from simple to complex, depending on the accuracy and performance levels required. Below, we discuss some popular methodologies:
1. Fixed Window Counter
A fixed window counter divides time into discrete chunks (e.g., one minute or one second) and counts the number of requests within each chunk.
- Advantages:
- Simplicity of implementation.
- Efficient for larger time windows like hours or days.
- Disadvantages:
- Lack of precision for smaller windows due to sudden resets when windows change.
Example
For counting requests by the minute:
- Advantages:
- Provides a more accurate real-time count.
- Avoids sudden reset issues associated with fixed windows.
- Disadvantages:
- Implementation can be more complex.
- Advantages:
- Balances accuracy and resource usage.
- Disadvantages:
- May require tuning of decay parameters.
- Distributed Systems: When dealing with request counting in a distributed system, the choice of technology (e.g., Redis, Kafka) and architecture (e.g., sharding, replication) can significantly impact performance.
- Concurrency: In a multithreaded or multiprocess setup, it's essential to manage concurrent access to shared data structures effectively to prevent race conditions and ensure accuracy.
- Scalability: As request volume increases, the approach should scale horizontally, maintaining an accurate count across additional servers or services.
Related reading
- How can I create a Route 53 Record to an ALB? AWS
- How can I create an image from a container running in Kubernetes?
- How can I delete Docker's images?
- How can I delete environment variable with kustomize?
- How can I deploy an iPhone application from Xcode to a real iPhone device?
- How can I disable logging while running unit tests in Python Django?
- How can I distribute a deployment across nodes?
- How can I edit a Deployment without modify the file manually?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.