How to reduce the build & startup time of an app

There are two easy ways to greatly speed up the build and startup time of web apps in Squash. Besides improving the user experience, fast startup times allow you to reduce the expiration time of your deployments, which will lead to significant cost savings.

Using the Deployment Cache

First check if the Deployment Cache feature is enabled for your repositories. This feature can speed things up a bit depending on your application and setup.

Enabling Persistent Storage + "post_launch"

The Persistent Storage feature is the one that can greatly speed things up when it comes to startup times. The downside of this feature is that Squash won't automatically fetch the latest version of your branch of code on each deployment. However, you can easily overcame this limitation by using the post_launch field in the Squash YAML file and adding some code to tell Squash how to update your application and services.

You may also decide to not update your application automatically and do it so from the Squash web interface, with a click of a button. See example #2 below.

Example #1

Here we are running a script to update the application within the host VM, outside of any containers. This is often enough for many apps using Docker volumes to map folders within containers.

  • The path /home/test-instance/code is the standard path where Squash downloads your branch of code.
  • wait_post_launch is important when it comes to startup times. It will allow your app to fully start first, bringing the shortest startup time. Then Squash will update your app in the background, without affecting the user experience.
  • The "update_app.sh" script needs to be implemented within your own code base according to your needs. In its most basic form this script needs to accomplish the following tasks:

    • pull the latest code from the current branch
    • Update your database schema if needed
    • Restart your application and related services if needed

deployments:
  MyApp:
    filename:
      ./src/Dockerfile
    wait_post_launch: true
    post_launch:
      - context: squash_host
        # source to load aliases
        command: /home/test-instance/code/update_app.sh

Sample of an "update_app.sh" script that can pull the latest code and update your application.

  • You need to update the Repository address at the top with your own correct path.
  • The environment variable $SQUASH_BRANCH is a default environment variable automatically set on each Squash deployment.
  • squash_docker is also a built-in Squash command always available within Squash VMs.

#!/bin/bash

REPO_NAME='git@github.com:ACMECorp/my-repo.git';

if [ "$(git remote -v | wc -l)" -eq "0" ]; then
  echo 'Setting up git repository';
  git remote add origin "$REPO_NAME";
  git fetch && git checkout "$SQUASH_BRANCH";
else
  echo 'Pulling the latest code';
  git pull origin "$SQUASH_BRANCH";
fi

# tail removes the `first line that is only useful for debugging
DOCKER_CONTAINER_ID="$(sudo squash-docker ps -qf 'name=website' | tail -n +2)";

sudo squash-docker exec -it "$DOCKER_CONTAINER_ID" python manage.py migrate
sudo squash-docker exec -it "$DOCKER_CONTAINER_ID" python manage.py collectstatic --noinput --clear

Example #2

This is very similar to the previous example except that this time Squash won't update the code automatically. Instead we are using a Custom Deployment Action. Users will just need to go to the Squash interface and update the application with a click of a button.

deployments:
  MyApp:
    filename:
      ./src/Dockerfile
    deployment_page_commands:
      Update App:
        squash_host:
          - /home/test-instance/code/update_app.sh

Example #3

We can also combine both examples above to accomplish the following:

  • Automatically update the code on each new deployment;
  • Allow users to update the code from the Squash interface when needed. This is handy if a deployment stays active for many hours and the code keeps changing during this period;

deployments:
  MyApp:
    filename:
      ./src/Dockerfile
    deployment_page_commands:
      Update App:
        squash_host:
          - /home/test-instance/code/update_app.sh
    wait_post_launch: true
    post_launch:
      - context: squash_host
        # source to load aliases
        command: /home/test-instance/code/update_app.sh

Pulling the latest code inside a container

Depending on your use case it might be helpful to pull the latest code inside a container.