Architect a multi-tier app with Duet AI assistance

This tutorial shows you how to use Duet AI, an AI-powered collaborator in Google Cloud, to architect a multi-tier web application using fully managed services where possible.

Objectives

  • Explore various Google services that you can use to serve traffic over the public internet.
  • Explore various Google services that you can use to cache data for faster retrieval.
  • Explore various Google services that you can use to persist stateful data.
  • Architect how various Google services interoperate.

Google Cloud products used

  • Cloud Run. Cloud Run is a managed compute platform that lets you run containers directly on top of Google's scalable infrastructure. You can deploy code written in any programming language on Cloud Run if you can build a container image from it.
  • Cloud Memorystore. Cloud Memorystore is a fully managed in-memory datastore service for Redis and Memcached. Build application caches that provide sub-millisecond data access.
  • Cloud SQL. Cloud SQL is a service that delivers fully managed relational databases in the cloud. It offers MySQL, PostgreSQL, and SQL Server database engines.

Before you begin

  1. Ensure that Duet AI is set up for your Google Cloud user account and project.

Explore web serving in Google Cloud

For the following example, consider that you're an architect who wants to use Duet AI to help make decisions on an organization-wide golden path for developers looking to quickly build a web app in Google Cloud. Your goal is to use Duet AI assistance to understand the options at your disposal, their characteristics, and their interoperability.

In the Google Cloud console, you can chat with Duet AI to get help. Using the Duet AI pane, you enter prompts, which are questions or statements that describe the help you want, and Duet AI returns responses.

You begin by asking Duet AI to help you make a decision around the compute tier where your web serving will run. Take the following steps:

  1. In the Google Cloud console toolbar, click Open Duet AI.

  2. In the Duet AI pane, enter the following prompt based on your requirements and preferences, and then click send:

    I am designing a multi-tier web app in Google Cloud. First, I would
    like help on deciding what to use for the compute tier. What are my
    options for running an app to serve traffic over the public internet?
    I would like to prioritize fully-managed services.
    

    Duet AI's response should look similar to the following:

    There are a few options for running an app to serve traffic over the public internet on Google Cloud. You can use Cloud Run, App Engine, or Compute Engine.
    Cloud Run is a fully managed serverless platform that lets you run stateless containers that are invocable using HTTP requests. App Engine is a fully managed platform that provides a programming environment for developing and running web applications. Compute Engine is a virtual machine (VM) service that lets you create and manage virtual machines.
    To decide which option is best for you, you need to consider the following factors:
    The size and complexity of your app
    The amount of traffic you expect to receive
    The level of control you need over your app
    The cost
    If you have a small, simple app that you expect to receive a small amount of traffic, then Cloud Run is a good option. If you have a larger, more complex app that you expect to receive a large amount of traffic, then App Engine or Compute Engine may be a better option. If you need a lot of control over your app, then Compute Engine is a good option. If you are on a budget, then Cloud Run is a good option.

  3. Given that you are architecting the fastest path to running smaller web apps in your organization, Duet AI's description of Cloud Run seems to best meet your requirements. But first, you want to confirm some of the characteristics of the platform. Enter the following prompt and then click send:

    Okay, I think I will move forward with Cloud Run. What metric does
    Cloud Run use to perform autoscaling?
    

    Duet AI's response should look similar to the following:

    Cloud Run uses the number of incoming requests or events to perform autoscaling. When a revision does not receive any traffic, by default it is scaled to zero instances. Find more information for your question here: About instance autoscaling.

  4. With Cloud Run autoscaling based on incoming requests, you now want to understand how traffic ingresses into applications it hosts. Enter the following prompt:

    How is a Cloud Run application exposed to users over the internet?
    

    Duet AI's response should look similar to the following:

    Cloud Run accepts incoming ingress traffic from the following sources:
    Public internet: requests are routed directly from public sources to your Cloud Run services with the option of routing traffic through an external HTTP(S) load balancer.
    Google APIs and services: requests from Google APIs and services are routed to your Cloud Run services without the need for an external HTTP(S) load balancer.
    VPC networks: requests from VPC networks are routed to your Cloud Run services through a VPC connector.
    You can control how your Cloud Run service is exposed to users by using the ingress settings.

  5. While an external, public load balancer is okay for this class of applications, you want Duet AI to help you understand if the platform can expose this application to only authenticated users with the right permissions. Enter the following prompt:

    Is there an easy mechanism in Cloud Run that allows only
    authenticated users to access an application?
    

    Duet AI's response should look similar to the following:

    Yes, there is a mechanism for ensuring that only authenticated users can access a Cloud Run application. You can do this by specifying the no-allow-unauthenticated option when deploying a service. This will cause your service to reject unauthenticated requests with an HTTP 403 Forbidden error. Find more information for your question here: Allowing public (unauthenticated) access.

  6. Finally, to set this up for developers in your organization, your preference is to not have them write any configuration related to infrastructure, such as Dockerfiles.

    We have standardized on using Django as a web framework. Can I deploy
    a Python app to Cloud Run directly from source code? Or do we need to
    implement a way to containerize this ourselves?
    

    Duet AI's response should look similar to the following:

    Yes, you can deploy a Python app to Cloud Run directly from source code. You can use the Cloud Run Python buildpack to build and deploy your Python app. Find more information for your question here: Deploying from source code.

Explore caching in Google Cloud

Now you want to proceed with choosing a service for the next tier: caching. A common need for this layer from your team's leads you to prompt Duet AI to help you understand the offerings Google Cloud has in this area. Take the following steps to ask Duet AI for assistance:

  1. In the Google Cloud console toolbar, click Open Duet AI.

  2. Enter the following prompt and click send:

    Okay! Thank you. Now let's move on to the caching tier. How can I
    introduce a caching layer to my web app? I would like to prioritize
    fully-managed services.
    

    Duet AI's response should look similar to the following:

    There are a few ways to introduce a caching layer to your web app. One option is to use a fully managed service like Cloud Memorystore. Cloud Memorystore is a fully managed in-memory datastore service for Redis. It provides a high-performance, scalable, and reliable caching solution that can help you improve the performance of your web app. You can also use Cloud Memorystore to cache static content, such as images and JavaScript files. Find more information for your question here: Introducing Memorystore: A fully managed in-memory datastore service for Redis.

  3. Before proceeding with using Cloud Memorystore for Redis, you want to ensure that communication between Cloud Run and Cloud Memorystore is private. Enter the following prompt:

    If I were to use Cloud Memorystore, how do I ensure the communication
    between Cloud Run and Cloud Memorystore is private?
    

    Duet AI's response should look similar to the following:

    To ensure the communication between Cloud Run and Cloud Memorystore is private, you need to create a Serverless VPC Access connector. This will allow for connectivity between Cloud Run and the VPC where your Memorystore instance lives. Find more information for your question here: Using Memorystore for Redis to cache your Django applications.

Explore relational databases in Google Cloud

Finally, you now want to settle on a relational database store. You prompt Duet AI to help you navigate your options:

  1. Enter the following prompt:

    And for the relational database tier, where do you recommend I
    persist data? Please keep in mind that I would like to continue to
    prioritize fully-managed services.
    

    Duet AI should respond with something similar to the following:

    There are a few options for persisting data in a fully-managed service. One option is to use Cloud SQL. Cloud SQL is a fully managed relational database service that provides a simple, reliable, and secure platform for running SQL Server, MySQL, and PostgreSQL workloads on Google Cloud. It is designed to help you get started fast with serverless migrations using Database Migration Service. Cloud SQL for SQL Server automatically ensures your SQL Server databases are reliable, secure, and scalable so that your business continues to run without disruption. Find more information for your question here: Cloud SQL overview.

Deploying a sample architecture

Finally, you now want to settle on a relational database store. You prompt Duet AI to help you navigate your options:

Okay, we have landed on using Cloud Run, Cloud Memorystore, and Cloud
SQL together. Is there a solution I can use that is available from
Google that uses these three technologies for a web application?

Duet AI should respond with something similar to the following:

Sure, here is a solution that uses Cloud Run, Cloud Memorystore, and Cloud SQL together for a web application:
Jump Start Solution: Three-tier web app.
This solution is a three-tier web application that uses Cloud Run, Cloud Memorystore for Redis, and Cloud SQL for PostgreSQL. The application uses Cloud Memorystore to cache data and Cloud SQL to store persistent data.

To test your new architecture, you decide that you would like to follow the instructions on the provided web page and deploy this sample solution so you can test and inspect your golden path architecture!

Clean up

To avoid incurring charges to your Google Cloud account for the resources used in this tutorial, you can delete the Google Cloud project that you created for this tutorial. Alternatively, you can delete the individual resources.

  1. In the Google Cloud console, go to the Manage resources page.

    Go to Manage resources

  2. In the project list, select the project that you want to delete, and then click Delete.
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

What's next