Building Scalable Applications with Alibaba Cloud Serverless Computing

Over the years, cloud computing has seen a significant shift towards hosting various components such as databases, file storage, and servers in the cloud. This migration has made it easier for businesses to deploy and run their applications on virtual cloud servers, eliminating the need for managing physical servers and on-premises data centers.

Building scalable application with Alibaba cloud serverless computing

One of the latest advancements in cloud computing is the introduction of serverless architecture, which further simplifies application deployment and management. With services like Alibaba Cloud's Function Compute, developers can now upload their application logic to the cloud without the complexities of server provisioning and management.

What is Serverless Architecture?

As the name suggests, serverless architecture allows you to deploy and run applications without the need to set up or maintain any servers in the cloud. Instead, the application relies on various cloud services to operate.

Advantages of Serverless Architecture

The primary advantage of serverless architecture is the elimination of server management. This means developers can focus more on their core product and business logic without worrying about server provisioning, configuration, and maintenance tasks.

Additionally, serverless architecture offers flexible scaling capabilities, allowing applications to automatically scale based on demand. This can significantly reduce the time-to-market for cloud applications and lower operational costs, as you only pay for the resources you use.

Limitations of Serverless Architecture

While serverless architecture offers many benefits, it also comes with limitations that developers need to consider:

  1. Rethinking Application Design: Serverless architecture requires a different approach to designing application logic, especially with event-driven and stateless functions like Alibaba Cloud's Function Compute.

  2. Lack of Control: Developers have limited control over the environment in serverless architecture, restricting the installation of third-party packages and libraries.

  3. Resource Limits: Serverless platforms impose limits on execution time, payload size, and the number of functions that can be created, which can impact application design and performance.

  4. Startup Latency: Serverless functions run in containers, which can result in higher startup latency due to the need to initialize the environment.

Understanding Serverless Architecture on Alibaba Cloud

Serverless architecture on Alibaba Cloud allows applications to run without the need for server instances. Instead, all components are managed using various services provided by Alibaba Cloud, eliminating the need to provision or manage servers.

Serverless Architecture on Alibaba Cloud

Key Components of Serverless Architecture

  1. Object Storage Service (OSS)

    • Hosts and serves static website content like HTML, CSS, JavaScript, images, and PDFs.
    • Supports frequent or infrequent data access.
    • Offers cost-effectiveness, high security, reliability, and a Pay-As-You-Go billing model.
    • Data transfer to and from OSS can be done using API actions or SDK interfaces.
    • Alibaba Cloud CDN can be used to efficiently cache data for users in different geographical areas.
  2. API Gateway

    • Provides high-performance and highly available API hosting services.
    • Acts as an HTTP endpoint to front Function Compute logic.
    • Allows users to access data, business logic, or functionality from back-end services.
    • Manages access permissions with multiple authentication methods.
    • Supports security mechanisms like anti-attack, anti-injection, anti-request replay, and anti-request tampering.
  3. Function Compute

    • Fully-managed event-driven compute service for running application logic.
    • Runs code without provisioning or managing servers.
    • Prepares computing resources and runs codes on behalf of users elastically and reliably.
    • Users only pay for resources consumed when running codes; no payment if the code isn't executed.
    • Processes events triggered by event source services automatically.
  4. ApsaraDB for MongoDB

    • NoSQL databases like MongoDB have gained popularity due to their ability to scale horizontally.
    • MongoDB is a document database, where a record is a document composed of field and value pairs, similar to JSON objects.
    • Alibaba Cloud ApsaraDB for MongoDB provides a secure and elastically scalable service to manage MongoDB databases on the cloud.

Creating a Serverless Application on Alibaba Cloud

As discussed earlier, a serverless architecture can be implemented using the following Alibaba Cloud components:

  1. Object Storage Service (OSS): This service stores static content like HTML, CSS, JavaScript files, and images for your application.

  2. Function Compute: Function Compute hosts and executes the code for your application logic, allowing you to run code without managing servers.

  3. ApsaraDB for MongoDB: This service serves as the backend database server for your system, providing secure and scalable storage for your application data.

In the following sections, we will explore how to set up and configure these components to build a serverless application on Alibaba Cloud.

Configuring Object Storage Service (OSS)

To set up an Object Storage Service (OSS) on Alibaba Cloud, follow these steps:

  • Log in to Alibaba Cloud Console: Navigate to the Object Storage Service in the left menu. If you are using OSS for the first time, you will need to activate the service. Click on the checkbox to agree with the agreement and enable the service.
    Object storage service

  • Publish Your Website to OSS: Once OSS is activated, you can proceed to publish your website by following these steps:

Step 1. Creating a Bucket

Click on the plus icon to create a new bucket.

Buckets

Name your bucket and choose a region. There are three storage classes for an OSS bucket:

  • Standard: for files that require high reliability and availability for frequent access.
  • Infrequent Access (IA): for long-term storage with infrequent access and lower storage costs.
  • Archive: for long-term storage with very infrequent access and the lowest storage costs.

Since this tutorial is for a public website, set the Access Control List (ACL) to "Public Read".

Create Bucket

Step 2. Uploading Files to the Bucket

Creating folders and uploading files is straightforward. Simply click on the corresponding buttons in the user interface.

Files upload

Step 3. Previewing the Website

Once you have uploaded all the files for your website to the bucket, click on the Overview tab to view the website's address.

Overview

However, when you visit the URL, you might encounter an "Access Denied" message. This occurs because the default homepage for the website has not been set. To set it, go to the Basic Settings tab and scroll down to the Static Pages section.

Basic setting- static pages

Returning to your website's URL, you will now see the homepage.

Mapping a Custom Domain Name to an OSS Bucket

By default, OSS assigns a domain name to your bucket in the format [bucket name].[endpoint], where the endpoint is oss-[region-id].aliyuncs.com. If you have your own domain name, you can map it in the Domain Names tab.

Domain names

After entering the custom domain name, you can opt to enable the CDN service.

That's all you need to set up on the OSS side. In the Domain Name System (DNS) settings of your custom domain name, you need to add a CNAME record to map it to the bucket domain name (which is [bucket name].[endpoint]). If you're unsure how to do this, please contact the service provider where you purchased the domain name.

If you ever need to unlink the custom domain name from your bucket, you can do so by clicking on Binding Configuration and then clicking Unbind.

Binding configuration

Setting Up Alibaba Cloud ApsaraDB for MongoDB

MongoDB is a popular NoSQL database, and Alibaba Cloud offers its ApsaraDB for MongoDB service to deploy MongoDB databases. Users can choose from three pricing schemes: Subscription (Replica Set), PAYG (Pay-As-You-Go) (Replica Set), and PAYG (Sharding). These schemes offer different replication and sharding options for data distribution.

Replication and Sharding

  • Replication involves copying data across multiple servers and storing the same data in different locations. ApsaraDB for MongoDB uses a 3-server replica set, which can aid in the horizontal scaling of reads.
  • Sharding distributes data across multiple machines so that each machine contains a different subset of data. This allows for horizontal scaling of writes.

Follow these steps to set up your database:

Step 1. Create an Instance

  • In the Alibaba Cloud console, navigate to Products and select ApsaraDB for MongoDB under the ApsaraDB group. Alternatively, use the search box to find the service.
  • Choose the pricing scheme, region, and server specification, and set a password for your database. Alibaba Cloud will provide the cost based on your selections.
  • Ensure to select a subscription instance if you are using a free trial account, as PAYG instances are not included in the trial program.

Step 2. IP Whitelists and Connection String

  • To ensure database security, Alibaba Cloud automatically blocks all access to the database. You must specify IP addresses in the whitelist to gain access to the target instance.Add a White Group

Once you have configured your IP whitelist, navigate to "Database Connection" to view the connection parameters.

Database connection

You can use this connection string to connect to the database in your Node.js code.

Setting Up Function Compute

Function Compute enables you to execute code without dealing with server provisioning or management. This service prepares computing resources and runs your code on your behalf, offering elasticity and reliability. You only pay for the resources used when your code is running, with no charges if your code isn't executed.

Function Compute processes your code in response to events. When an event source service triggers an event, the associated function is automatically invoked to process the event.

To create a new service, navigate to Function Compute in the Alibaba Cloud console and click on the plus icon.

Create service

To start, within the newly established service, tap on the + symbol to initiate a new function. This will lead you through a series of steps using a wizard to tailor options for your function.

When prompted for a Function Template, opt for Empty Function.

Function templete

Moving on to the Configure Triggers step, choose HTTP Trigger and assign it a suitable name. Refer to the image below for additional settings and configurations.

Configure triggers

In the subsequent Configure Function Settings step, assign a name to your function and opt for nodejs6 as the runtime.

Create service

Proceed by clicking Next in the final two steps to finalize the wizard and generate the function. Subsequently, navigate to the function you've just created and access the Code tab. This is where you input the code for your function to execute.

Code

Ensure to utilize the connection string from your MongoDB server. Additionally, within this interface, you can review the HTTP trigger responsible for invoking your function. You also have the option to run the trigger to test your function directly from here.

HTTP Trigger Debugging

By completing the process, you've created a function that inserts a new record into a MongoDB database upon receiving an HTTP request. Function Compute proves invaluable in scenarios where code needs to be executed in response to a specific action or event.

Additional applications of Function Compute include:

  1. Monitoring user activity on a website: For instance, a function can be triggered by a user's mouse click to log which item was clicked.
  2. Image manipulation: Functions can be triggered when users upload images to OSS buckets, allowing for the creation of thumbnails or the addition of watermarks.
  3. Automation: Functions can be scheduled to handle repetitive tasks such as creating backups, generating reports, or executing other routine operations.
  4. Internet of Things (IoT) systems: IoT sensors can trigger functions to control other devices within the network.

Exploring Function Compute Further

Previously, we covered the essential steps for creating and configuring a Function Compute to execute your application logic. Function Compute is a robust tool that can facilitate the necessary capabilities for running and scaling your system. It automatically manages scalability, high availability, security, and performance requirements, providing these features seamlessly.

Moreover, this service offers additional functionalities such as log querying, performance monitoring, and alarms, enhancing its utility.

However, using Function Compute can be complex as it requires a different logical structure and a well-designed implementation. You must carefully design and code your logic and workflow due to its event-driven and stateless nature. The system relies on separate function calls triggered by various events, necessitating the creation of a separate state machine to store the application state. It's also advisable to refactor large functions into smaller ones to optimize execution time.

In practice, finding the optimal balance between cost and performance for your system may require experimentation. Allocating more memory and processing power can speed up code execution. However, beyond a certain point, increasing RAM and CPU may not reduce execution time but can increase running costs.

For more information about Alibaba Cloud Function Compute, refer to the Frequently Asked Questions Page.

Conclusion

This article has guided you through setting up a serverless application within the Alibaba Cloud ecosystem. By utilizing Object Storage Service, ApsaraDB for MongoDB, and Function Compute, you've created a fully managed, event-driven compute service. This setup allows for more flexible and reliable code execution without the need for maintenance. Additionally, it provides the benefits of real-time auto-scaling and dynamic load balancing within milliseconds. Welcome to the new era of application development and service provision.

Up Next
    Ebook Download
    View all
    Learn
    View all
    Revinfotech Inc - Enterprise Software And Web Development Company