Background: I am trying to optimize what we call AJAX request polling frequency in the domain of web design, and I wanted to check if I could use some help from math guys to explore a better alternative.
Here's my illustration of the problem statement:
Problem Statement:
Suppose there are 3 persons - consumer (C), producer(P) and a messenger(M). The consumer and producer are some distant apart and can't communicate directly. The messenger - who works for the consumer - takes order from consumer, walks up to producer, places the order and come back to consumer. Messenger needs some finite amount of time to make the travel between P and C.
Now different orders require different amount of time for processing. After an order is placed, the M and C have no way to know how long the order processing will take. Hence, the messenger is required to make repeated trips to the Producer to check if the order is ready. It works in this way:
- Step 1: Messenger waits for W amount of time
- Step 2: M Goes to the producer to see if order is ready
- Step 3: a) Order ready - come back with the produced item b) Not ready - come back and repeat from step 1.
Now, I tabulated the time taken by each orders and plotted them against the number of orders taking that time. I got this:
| | |
25 | | | |
| | | | |
20 | | | | | | |
| | | | | | | | |
15 | | | | | | | | | |
| | | | | | | | | | |
10 | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | |
0 +-|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|---
1 5 10 20 25 30 35 40 45 50 55 60 65 70 75 85 90
As you can see, most orders take around 35 to 40 units of time (let's say seconds) to complete whereas some orders get completed in 5-10 seconds and some takes 70-80 seconds.
My Goal:
Right now my messenger is checking back in every W seconds. Instead of keeping this checking frequency W as constant, can we find some other checking frequency - may be a variable one where we adjust the frequency as we go along - that would ensure the least overall waiting time combining consumer and producer waiting time together.
As you can tell, I am not a mathematician and my narration above was probably bit clumsy. Let me know if you have further questions in comments.