IBM Cloud Docs
Accelerate a dynamic website using Dynamic Content Acceleration

Accelerate a dynamic website using Dynamic Content Acceleration

This tutorial may incur costs. Use the Cost Estimator to generate a cost estimate based on your projected usage.

Web applications are composed of static content like text, images, cascading style sheets, and JavaScript files. The tutorial Accelerate delivery of static files using a CDN shows how to host and serve static assets (images, videos, and documents) of a website from IBM Cloud Object Storage with IBM® Content Delivery Network (CDN).

Applications also contain personalized and dynamically changing contents that can’t be cached at CDN. A common example of non-cacheable dynamic content is adding an item to a cart in an e-commerce website that might be generated from JavaScript on the base page. Before Dynamic Content Acceleration is available, a CDN will pass every request for a non-cacheable object through to the owner’s origin server, and pass the result back to the user.

To stop these dynamic contents from being a performance bottleneck, you can utilize the new Dynamic Content Acceleration (DCA) capability of IBM® Content Delivery Network (CDN) to optimize the performance of dynamic contents:

  • the DCA capability of CDN will choose the optimal routes for requests
  • proactively pre-fetch contents from origin servers so that users can access these contents rapidly from the edge
  • extend the duration of TCP connections for multiple requests
  • automatically compress images for lower latency.

Objectives

  • Deploy a sample dynamic web application to a Kubernetes Service cluster.
  • Make static content globally available with IBM® Content Delivery Network.
  • Enable the Dynamic Content Acceleration (DCA) capability for performance optimization of non-static content.

Architecture
Figure 1. Architecture diagram of the tutorial

  1. The developer creates a simple dynamic application and produces a Docker container image.
  2. The image is pushed to a namespace in Container Registry.
  3. The application is deployed to Kubernetes Service.
  4. User accesses the application.
  5. The application is accelerated through the Dynamic Content Acceleration capability of IBM® Content Delivery Network.
  6. IBM® Content Delivery Network interacts with the application to fetch dynamic contents.

Before you begin

You can run the tutorial in the Cloud Shell from the IBM Cloud console. If you want to run it on your machine, it requires the following:

  • IBM Cloud CLI,
    • IBM Cloud Kubernetes Service plugin (kubernetes-service),
    • Container Registry plugin (container-registry),
    • dev plugin,
  • a Docker engine,
  • kubectl to interact with Kubernetes clusters,
  • git to clone source code repository.

You will find instructions to download and install these tools for your operating environment in the Getting started with solution tutorials guide.

In addition: A minimal cluster with one (1) zone, one (1) worker node and the smallest available size (Flavor) is sufficient for this tutorial.

Open the Kubernetes clusters and click Create cluster. See the documentation referenced below for more details based on the cluster type. Summary:

  • Click Standard tier cluster
  • For Kubernetes on VPC infrastructure see reference documentation Creating VPC clusters.
    • Click Create VPC:
      • Enter a name for the VPC.
      • Chose the same resource group as the cluster.
      • Click Create.
    • Attach a Public Gateway to each of the subnets that you create:
      • Navigate to the Virtual private clouds.
      • Click the previously created VPC used for the cluster.
      • Scroll down to subnets section and click a subnet.
      • In the Public Gateway section, click Detached to change the state to Attached.
      • Click the browser back button to return to the VPC details page.
      • Repeat the previous three steps to attach a public gateway to each subnet.
  • For Kubernetes on Classic infrastructure see reference documentation Creating classic cluster.
  • Choose a resource group.
  • Uncheck all zones except one.
  • Scale down to 1 Worker nodes per zone.
  • Choose the smallest Worker Pool flavor.
  • Enter a Cluster name.
  • Click Create.

Deploy a dynamic web application to be accelerated

Let's consider a simple dynamic web application for collaboration for a team geographically distributed. With this application, team members can create and manage team's to-do items together.

This sample application is based on Beego, a RESTful HTTP framework for the rapid development of Go applications including APIs, web apps and backend services.

Build the application

Open a terminal in the Cloud Shell, then follow these steps:

  1. Clone the application

    git clone https://github.com/IBM-Cloud/cdn-with-cda-todolist.git
    
  2. Change to the application directory

    cd cdn-with-cda-todolist
    
  3. Being logged in to the CLI environment, identify the cluster. ibmcloud ks cluster ls will return cluster names.

    ibmcloud ks cluster ls
    

    Set the variable accordingly:

    MYCLUSTER=<cluster_name>
    
  4. Identify the Container Registry and set a namespace. ibmcloud cr info will return the name of the container registry.

    ibmcloud cr info
    

    Set the variable accordingly:

    MYCONTAINERREGISTRY=<us.icr.io_like_value_returned_from_ibmcloud_cr_info>
    

    Set the variable to a name you want to use as a new namespace or an existing namespace:

    MYNAMESPACE=<my_container_registry_namespace>
    
  5. Create a namespace to store the container image. Feel free to skip this step and use an existing namespace.

    ibmcloud cr namespace-add $MYNAMESPACE
    
  6. Build a Docker image using the Dockerfile in Container Registry and use cdn-with-cda-todolist as the image name:

    docker build -t $MYCONTAINERREGISTRY/$MYNAMESPACE/cdn-with-cda-todolist:latest .
    
  7. Log in Container Registry:

    ibmcloud cr login
    
  8. Push the image to Container Registry:

    docker push $MYCONTAINERREGISTRY/$MYNAMESPACE/cdn-with-cda-todolist:latest
    

Run the application in the cluster

  1. Run the command below to target the cluster where to deploy the application.
    ibmcloud ks cluster config --cluster $MYCLUSTER
    
  2. Retrieve the cluster ingress subdomain and secret name:
    ibmcloud ks cluster get --cluster $MYCLUSTER
    
  3. Copy deployment.sample.yaml to deployment.yaml:
    cp deployment.sample.yaml deployment.yaml
    
  4. Edit deployment.yaml and replace the placeholders <image>, <ingress-subdomain> and <ingress-secret> with values matching your environment.
  5. Deploy the application to the cluster:
    kubectl apply -f deployment.yaml
    
  6. Access the application at https://cdn-with-cda-todolist.<ingress-subdomain>

Create a CDN instance

Before you create a IBM® Content Delivery Network instance, you should have registered a domain name for your application.

  1. Go to the cloud catalog, and select IBM® Content Delivery Network from the Network section. Click Create.

    • Set Hostname to a custom domain, for this tutorial it is not required to be a domain you own as we will not be using it, you can set it to todo.example.com.

    • Leave the Custom CNAME prefix empty, it will default to a generated unique name, this is the entry we will use to test the CDN.

    • Leave Host header and Path empty.

    • Click the Server tab and specify the application ingress subdomain as Origin server address, for example cdn-with-cda-todolist.<ingress-subdomain>.

    • Uncheck HTTP port.

    • Check HTTPS port and select Wildcard SSL certificate.

      With the Wildcard certificate, you will access your app through the Custom CNAME. The Wildcard certificate is the simplest way to deliver web content to your users securely. The Custom CNAME is added to the wildcard certificate maintained on the CDN Edge server and becomes the only way for users to use HTTPS for your CDN (for example, https://cdnakaivlnqidbg4.cdn.appdomain.cloud).

  2. Accept the Terms and Conditions and click Create.

After you have successfully created the CDN mapping:

  • CNAME configuration required may display in the Status column, you can ignore it and check the status again by selecting Get status from the overflow menu. It should change to Running after a few minutes.
  • To view your CDN instance, select the CDN instance in the list. The Details panel shows both the Hostname and the CNAME for your CDN.
  • Your application is now accessible through the CNAME only: https://<CNAME>. You may need to wait a few minutes for all configuration to complete and for the CNAME to work.

Enable Dynamic Content Acceleration (DCA)

At that stage, the static content of the application is cached by the CDN but not the dynamic content.

The Dynamic Content Acceleration (DCA) feature will query a test object in about 10KB size on your origin server to determine the optimal routes for real requests. For this purpose, the application has been customized to include a test object made available at /test-dca.

To activate DCA:

  1. Select the Settings tab in the CDN configuration.

  2. Under the Optimization settings section, click Edit and select Dynamic Content Acceleration from the Optimized for drop-down list.

  3. Under the Detection path section, specify the path /test-dca as the detection path, and click Test to verify the path is set correctly. This detection path will be used periodically by IBM® Content Delivery Network to determine the fastest path to the origin.

  4. Make sure Prefetching and Image compression are both set to On.

    Configure DCA
    Configure DCA

  5. Click Save. You have successfully accelerated your application deployed in Kubernetes Service cluster with Dynamic Content Acceleration.

Verify DCA performance

You can use common website performance tools such as Web Page Test to compare the website response time before and after DCA is turned on. Use the path /test-dca along with the original URL and the CDN-based URL from above.

After enabling DCA for a period, you can view the both static and dynamic traffic bandwidth by clicking on the View CDN report on the CDN Overview page.

Conclusion

With DCA turned on and the detection path specified, CDN edge servers periodically fetch the test object from the origin to look for any path between the internal network of CDN edge servers that have lower latency and/or packet loss rate than the default route on the Internet. When a real request comes in, IBM® Content Delivery Network consults the most recent data to send that request over the best path to the origin.

With Prefetching enabled, DCA also finds which content is required by the application and preemptively fetches content from origin and stores it close to the user by analyzing user behavior data and web sessions. The Image compression option serves compressed images to reduces the amount of content required to load a page, especially when end users have slow network speed. DCA also employs TCP-layer optimizations that accelerate connection set up and reduce round trips.

Remove resources

Related content