title: Best algorithm to resolve a tree
body: <p>I have a JSON dataset, published from a CMS, which contains a mixture of attributes and "references to content".  The resolution of the references is currently done by recursion, via a HTTP request, which looks up the ID and injects the response.  There may then be further references in the response so the recursion depth can be quite high (up to 20 levels).  The dataset is extremely granular, by requirement, stored in a document database, which probably isn't the best choice.</p>
  
  <p>The effect of runtime, is basically a DDOS effect, as might be expected.  The algorithm can take up to 3 minutes; at worst, it 503's the service.</p>
  
  <p>Attached a rubbish picture of what it looks like, to help.<a href="https://i.stack.imgur.com/nDCQW.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nDCQW.png" alt="enter image description here"></a></p>
  
  <p>I'm pretty hopeless with algorithmic complexity, so forgive me, but I'm pretty sure this is O(2^n) time complexity, so pretty unscalable beyond trivial tree height?  </p>
  
  <p>I'm looking for obvious ways to optimise this first run, other than memoisation in the resolution service.  The only way I can see is either compose the document up front, which is against the client wishes as they want to avoid lengthy publishing times, or do something at the database level with a "View" with a TTL, which would be eventually consistent.</p>