[Design] Monitor Rps for Past sec/min/hr - Shuatiblog.com
link
Print Rps in real time. I posted my code below (the code is without thread-safety consideration).
Keep an array of int of size 60 * 60. Each second, use the number of request in the past second to update the array values in a rolling way.
http://stackoverflow.com/questions/18364490/implementation-of-a-hits-in-last-second-minute-hour-data-structure
Read full article from [Design] Monitor Rps for Past sec/min/hr - Shuatiblog.com
link
Given a server that has requests coming in.
Design a data structure such that you can fetch the count of the number requests in the last second, minute and hour.
Solution 1
Keep a record of all request timestamps, suggested by the top answer by whatevva:- Use a queue implemented as a resizable array to store the timestamps of all new requests
- maintain head/tail pointers as usual
- Also maintain three pointers, for past sec, past min and past hr.
Print Rps in real time. I posted my code below (the code is without thread-safety consideration).
Solution 2
This solution does not store all timestamps, and it does not generate real-time Rps data. But it's good enough because result is only updated every 1 second, so its performance is better.Keep an array of int of size 60 * 60. Each second, use the number of request in the past second to update the array values in a rolling way.
http://stackoverflow.com/questions/18364490/implementation-of-a-hits-in-last-second-minute-hour-data-structure
Read full article from [Design] Monitor Rps for Past sec/min/hr - Shuatiblog.com