title: Use Amazon S3 and Cloudfront for intelligently caching webapges
body: <p>I have a website (running within Tomcat on Elastic Beanstalk) that generates artist discographies (a single page for one artist). This can be resource intensive, so as the artist pages don't change over a month period I put a CloudFront Distribution in front of it. </p>
  
  <p>I thought this would mean no artist request ever had to be served more than once by my server however its not quite as good as that. This post explains that every edge location (Europe, US etc.) will get a miss the first time they look up the resource and that there is a limit to how many resources are kept in the cloudfront cache so they could be dropped.</p>
  
  <p>So to counter this I have changed by server code to store a copy of the webpage in a bucket within S3 AND to check this first when a request comes in, so if the artist page already exists in S3 then the server retrieves it and returns its contents as the webpage. This greatly reduces the processing as it only constructs a webpage for a particular artist once. </p>
  
  <p>However:</p>
  
  <ol>
  <li>The request still has to go to the server to check if the artist page exists.</li>
  <li>If the artist page exists then the webpage (and they can sometimes be large up-to 20mb) is first downloaded to the server and then server returns the page.</li>
  </ol>
  
  <p>So I wanted to know if I could improve this - I know you can construct an S3 bucket as a redirect to another website. Is there a per-page way I could get the artist request to go to the S3 bucket and then have it return the page if it exists or call server if it does not?</p>
  
  <p>Alternatively could I get the server to check if page exists and then redirect to the S3 page rather than download the page to the server first?</p>