Skip to main content

Operation Mechanism

Cloud Functions is a serverless computing service that allows you to run code without managing servers. CloudBase cloud functions provide you with a simple, reliable, and efficient code execution environment, automatically handling all underlying infrastructure, allowing you to focus on developing business logic.

Core Features

Stateless Computing

CloudBase dynamically controls the number of cloud function instances and evenly distributes requests based on real-time load conditions. Consecutive requests may be handled by different instances, therefore:

  • Cloud functions must be designed to be stateless: Each execution should be independent of previous executions
  • Functions should be idempotent: The side effects produced by multiple invocations should be the same as those of a single invocation.
  • Should not rely on shared state between instances: Each function instance is an isolated execution environment

Best practices: For saving state, please use external storage services (such as cloud databases or cloud storage) rather than relying on the memory or local storage of function instances.

Event-Driven Model

Cloud functions use an event-driven architecture, and each invocation essentially triggers a cloud function execution event.

Supported Trigger Types

Trigger TypeDescriptionDocumentation Link
SDK/API InvocationDirect invocation via language-specific SDKs or REST APIWeb-side invocation
node.js server-side invocation
HTTP TriggerInvoked via HTTP requests, can be used to build APIsAccessing Cloud Functions via HTTP
Timer TriggerAutomatically triggers execution based on preset time rulesTimer Trigger

Auto-scaling

CloudBase automatically handles the scaling of cloud functions, freeing you from infrastructure management concerns:

  • On-demand Scaling: Automatically creates more instances when traffic increases
  • Automatic Scaling Down: Releases excess instances when traffic decreases
  • Zero-instance Cold Start: Occupies no resources when there is no traffic; automatically starts upon receiving requests

Function Input Parameters Details

Each cloud function invocation receives two important objects: event and context.

The event Object

The event` object contains the event data that triggers the cloud function, and its content varies depending on the trigger method:

  • Mini Program Invocation: Contains parameters passed from the Mini Program client
  • HTTP Request Invocation: Contains HTTP request information (such as request headers, request body, etc.)
  • Timer Trigger: Contains relevant information about the timer trigger

The context Object

The context` object provides invocation context information, helping you understand the function's runtime environment and invocation method:

  • Request ID: The unique identifier for the current invocation
  • Call source: The service or client information that triggers the function
  • Execution Environment: The runtime information of the function
  • User identity: Identity information of the caller (if available)

Code Sample

Here is a simple cloud function example demonstrating how to process input parameters and return results:

// index.js - Cloud function entry file
exports.main = async (event, context) => {
// 1. Parse the cloud function input parameters
const {
a,
b
} = event;

// 2. Execute business logic
const sum = a + b;

// 3. Return the result
return {
sum,
timestamp: Date.now()
};
};

Since instance management is automatically handled by the platform, it is recommended to use synchronous execution mode for cloud functions and avoid asynchronous functions.

exports.main = async (event, context) => {
// bad
getList().then((res) => {
// do something...
})

// good
const res = await getList()
// do something...
}

Runtime Environment Details

Containerized Runtime Environment

  • Cloud functions run in an isolated containerized Unix environment.
  • Each function instance has an independent execution environment and is completely isolated from others.
  • The creation, management, and termination of instances are fully automated and handled by the platform.

Temporary Storage

  • Each function instance provides 512MB of /tmp temporary storage space
  • Designed for temporary file I/O operations during a single execution
  • Temporary storage may be cleared after function execution
Important
  • The current runtime environment is based on CentOS 7.2, but do not rely on specific operating systems or versions as the runtime environment may be updated at any time.
  • Temporary storage is not guaranteed to persist after function execution. For persistent storage, please use Cloud Storage

Supported Runtimes

CloudBase cloud functions support multiple programming language runtimes:

LanguageSupported Versions
Node.js12.16, 10.15, 8.9
PHP7.2
Python3.6, 2.7
Golang1
Java8

Resource Limits and Performance

Memory Configuration

  • Default memory: 256 MB
  • Configurable range: 128 MB ~ 2048 MB
  • Recommendation: Select an appropriate memory configuration based on function complexity and data processing volume.

Performance Tip: Increasing memory configuration typically enhances CPU performance, which is particularly important for compute-intensive tasks.

Concurrency Processing Capability

The concurrency of cloud functions refers to the number of function instances executing simultaneously at any given point in time:

  • Maximum Concurrent Instances: Defaults to 1000 instances per cloud function
  • Concurrency Limit Behavior: Calls exceeding the limit will be blocked (and not executed).

Concurrency Calculation Formula

Concurrent function instances = Requests per second × Function execution time (seconds)

Instance Calculation

ScenarioCalculation ProcessResult
Function execution: 0.2 seconds, 300 requests per second300 × 0.260 concurrent instances
Function execution: 0.2 seconds, to achieve maximum concurrency of 10001000 ÷ 0.2Requires 5000 QPS

Performance Optimization Recommendations

To ensure the efficient operation of cloud functions, consider the following optimization recommendations:

  1. Reduce Cold Start Impact

    • Avoid loading large dependencies globally
    • Separate initialization code from processing logic
    • Consider using Provisioned Concurrency
  2. Optimize Execution Time

    • Use asynchronous parallel processing for independent tasks
    • Avoid unnecessary network requests
    • Optimize database queries and file operations
  3. Properly use memory

    • Avoid unnecessary large object creation
    • Release promptly after processing large data
    • Allocate sufficient memory for compute-intensive tasks
  4. Error and Exception Handling

    • Implement a robust error handling mechanism
    • Log critical operations
    • Set appropriate timeout

By understanding the operating mechanism of cloud functions and following best practices, you can build efficient, reliable, and scalable serverless applications.