Skip to main content

Function Types

CloudBase Cloud Functions provide two types, each suitable for different business scenarios. Choosing the right function type has significant impact on performance, development efficiency, and cost.

Key Differences

DimensionCustom FunctionHTTP Function
Design PhilosophyEvent-Driven Architecture (EDA)Web Service Mode (Serverless Web)
Trigger MethodEvent-driven (SDK, Timer, Cloud Product Events)Direct HTTP Request Trigger
Input FormatFixed event and context objectsNative HTTP Request/Response
Concurrency ModelSingle instance, single requestSingle instance, multiple concurrent requests
Port ListeningNot requiredMust listen on port 9000
Execution ModeSupports synchronous/asynchronous invocationSynchronous invocation only
Framework SupportPlain function codeWeb frameworks like Express, Koa, Next.js
Use CasesBackground tasks, data processing, cloud product integrationWebsites, API services, legacy framework migration

How to Choose?

Choose「Custom Function」if your scenario is:

✅ Background Data Processing

  • Image/video processing, format conversion
  • Log analysis, data cleansing
  • Batch data processing

Example Scenario: When users upload images to cloud storage, automatically trigger a cloud function for compression and watermarking.

// Custom Function - Responds to COS upload events
exports.main = async (event, context) => {
// event contains trigger event data
const { fileID } = event;

// Process business logic
const result = await processImage(fileID);

// Return JSON format result
return {
code: 0,
data: result
};
};

✅ Scheduled Tasks

  • Regular data backup
  • Scheduled report generation
  • Scheduled data synchronization

Example Scenario: Automatically generate business reports for the previous day at 2 AM and send emails.

✅ Cloud Product Integration

  • Respond to Cloud Object Storage (COS) file upload events
  • Consume message queue (CKafka) messages
  • Database triggers

Example Scenario: Listen for database changes and automatically send welcome emails when users register.

✅ Asynchronous Task Processing

  • Background operations that don't require immediate response
  • Long-running computational tasks
  • Batch processing tasks

Choose「HTTP Function」if your scenario is:

✅ Web Application Development

  • Corporate websites, landing pages
  • H5 pages, mini-program backends
  • API services for frontend-backend separation

Example Scenario: Develop a frontend-backend separated todo application where the frontend calls RESTful APIs from HTTP cloud functions.

// HTTP Function - Using Express framework
const express = require('express');
const app = express();

app.use(express.json());

// RESTful API endpoints
app.get('/api/todos', async (req, res) => {
const todos = await db.collection('todos').get();
res.json({ code: 0, data: todos });
});

app.post('/api/todos', async (req, res) => {
const result = await db.collection('todos').add(req.body);
res.json({ code: 0, data: result });
});

// Must listen on port 9000
app.listen(9000, () => {
console.log('HTTP Function started on port 9000');
});

✅ Legacy Framework Migration

  • Existing Express/Koa/Next.js projects that need quick cloud deployment
  • Zero or low-modification migration (only need to change listening port to 9000)
  • Leverage existing npm ecosystem and middleware

Example Scenario: Migrate existing Express applications to Serverless by simply changing the listening port.

✅ High-Concurrency Web Services

  • Need single instance to handle multiple concurrent requests
  • Reduce cold start impact
  • Optimize cost and performance

Comparison Note: HTTP functions support single-instance multi-concurrency, which can use fewer instances for the same traffic, significantly reducing cold starts and costs.

✅ Real-Time Communication Scenarios

  • WebSocket long connections
  • SSE (Server-Sent Events) server push
  • File streaming upload/download

Example Scenario: Build real-time chat applications using WebSocket for message push.

Detailed Explanation

Custom Function

「Custom Function」is designed based on Event-Driven Architecture, where functions passively respond to various event sources (timers, cloud product events, SDK calls, etc.).

Core Features:

  • Simple and Easy: Only need to implement business logic functions without worrying about HTTP protocol details
  • Standardized Input: event contains event data, context contains runtime environment information
  • Multiple Trigger Methods: Supports SDK, HTTP API, timer, and cloud product event triggers
  • Flexible Invocation Modes:
    • Synchronous Invocation: Waits for function execution to complete and returns result
    • Asynchronous Invocation: Returns request ID immediately, function executes in background

Typical Use Cases:

Scenario TypeSpecific Examples
Data ProcessingImage compression, video transcoding, document format conversion
Scheduled TasksDaily report generation, regular data backup, scheduled cleanup
Event ResponsePost-upload file processing, message queue consumption, database change triggers
Microservice ComponentsIndependent business logic units, background computing services

HTTP Function

「HTTP Function」is designed specifically for Web service scenarios, enabling seamless migration of traditional Web applications to Serverless.

Core Features:

  • Native HTTP Support: Directly receives and processes complete HTTP requests without protocol conversion
  • Zero-Modification Migration: Existing Web framework code only needs to change listening port to 9000 for deployment
  • High Concurrency Performance: Single instance can handle multiple requests simultaneously, greatly reducing cold start impact
  • Complete Ecosystem Support: Compatible with mainstream frameworks like Express, Koa, Next.js, Nuxt.js
  • Rich HTTP Features:
    • Complete HTTP Header and Cookie operations
    • Custom HTTP status codes
    • WebSocket, SSE real-time communication
    • File streaming processing

Typical Use Cases:

Scenario TypeSpecific Examples
Web ApplicationsCorporate websites, product landing pages, admin dashboards
API ServicesRESTful API, GraphQL interfaces
Framework MigrationExpress/Koa/Next.js projects quick cloud deployment
Real-Time CommunicationWebSocket chat, SSE message push, real-time data streams

Quick Decision Guide

Not sure which type to choose? Answer the following questions:

  1. Does your service need to directly respond to HTTP requests?

    • Yes → Consider HTTP Function
    • No → Consider Custom Function
  2. Does your project already use Web frameworks like Express/Koa?

    • Yes → Strongly recommend HTTP Function (low migration cost)
    • No → Choose based on business scenario
  3. Does your business need to handle high-concurrency Web requests?

    • Yes → HTTP Function (single-instance multi-concurrency, better performance)
    • No → Either works
  4. Do you need scheduled tasks or respond to cloud product events?

    • Yes → Custom Function (better suited for event-driven scenarios)
    • No → Choose based on business scenario
  5. Do you need WebSocket or SSE real-time communication?

    • Yes → Must use HTTP Function
    • No → Either works
Hybrid Usage

In actual projects, you can use both types of cloud functions simultaneously:

  • Use HTTP functions to handle Web requests and API endpoints
  • Use Custom functions to handle background tasks and scheduled tasks

Learn More