title: Algorithm to automatically adjust resource pool capacity based on demand
body: <p>You are the producer of a resource which takes a while to create (let's say a couple minutes for illustration). You have consumers requesting instances of that resource, and the pattern of requests is not initially known (and can vary over time).</p>
  
  <p>Using a resource pool is an obvious choice. But what is a good and simple way of choosing and evolving the pool capacity over time in a way that:</p>
  
  <ul>
  <li><strong>Reduces wait time</strong>: we want most consumers get their request fulfilled with no wait at all (let's say 90% of the time).</li>
  <li><strong>Minimizes waste</strong>: assume that keeping these resources in the pool is expensive, so we don't want a large number of them sitting around for extended time.</li>
  </ul>
  
  <p>I'm not looking for some advanced AI learning solution here, but for a simple algorithm that adjusts the pool size based on simple factors, e.g.</p>
  
  <ul>
  <li>resources currently left in the pool</li>
  <li>age of the resources in the pool</li>
  <li>frequency of requests</li>
  </ul>
  
  <p>Clearly, there are more than one way to do this, and the optimal algorithm will depend on how expensive resources are, how bad it is to make consumers wait, and the kind of organic request pattern you are getting (e.g. slowly ramping up vs bursts). But I'm looking for a solution that does a decent job under most situations, and has a few constants that can be tweaked based on real world problem.</p>
  
  <p>To illustrate, I'll give a fairly naive example algorithm:</p>
  
  <ul>
  <li>Start with a pool size of 10</li>
  <li>If pool to 20% of total capacity (so initially 2 items), increase pool size by one.</li>
  <li>If oldest item in pool (i.e. the one next in line to be used) is older than 1 hour, decrease pool size by 1.</li>
  </ul>