request counting
time-based counting
real-time analytics
data monitoring
rate limiting

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.

Practice system design

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:

  1. Time Window: This represents the interval over which requests are counted, such as the last second, minute, or hour.
  2. Sliding Window: A technique that allows data to be processed continuously at any point in time, enabling real-time statistics.
  3. 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
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design