Situation

A custom Typee deployment generates app configuration by a AWS Lambda. But there are some drawbacks:

  1. AWS Lambda has high delay when code start is required.
  2. Each refresh of a custom Typee deployment costs a AWS Lambda call and DynamoDB call. But the configuration actually changes less frequently.

Solution

I told myself as MVP I shouldn’t keep it simple and brutal, but I failed to convinced myself.

First version

I firstly put the Lambda behind Cloudfront(CDN). Cloudfront caches the app configuration for one day. The load time and cost decreased but I have to invalidate the app configuration every time I update it. Even after I invalidated it, the browser didn’t show latest version of app configuration because Browser cached app configuration.

It looks like below.

Second Version

To make new App Config is available immediately after invalidation, I need to let Browser to check if CDN has new data. HTTP Header Cache-Control: no-cache can direct Browser to store the content to Browser cache but check it with remote cache before use it. By using this, we can reduce data transfer, which is a cost to Cloudfront, when the data is not expire and reduce cost to Lambda. In the meanwhile, users can get latest App Config.

It still requires do invalidation once there is change in App Config but fit my current requirements.

The new setup looks like below.

Reference

Typee

Typee Deploy

Managing how long content stays in the cache (expiration)

Cache-Control