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
| Dimension | Custom Function | HTTP Function |
|---|---|---|
| Design Philosophy | Event-Driven Architecture (EDA) | Web Service Mode (Serverless Web) |
| Trigger Method | Event-driven (SDK, Timer, Cloud Product Events) | Direct HTTP Request Trigger |
| Input Format | Fixed event and context objects | Native HTTP Request/Response |
| Concurrency Model | Single instance, single request | Single instance, multiple concurrent requests |
| Port Listening | Not required | Must listen on port 9000 |
| Execution Mode | Supports synchronous/asynchronous invocation | Synchronous invocation only |
| Framework Support | Plain function code | Web frameworks like Express, Koa, Next.js |
| Use Cases | Background tasks, data processing, cloud product integration | Websites, 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:
eventcontains event data,contextcontains 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 Type | Specific Examples |
|---|---|
| Data Processing | Image compression, video transcoding, document format conversion |
| Scheduled Tasks | Daily report generation, regular data backup, scheduled cleanup |
| Event Response | Post-upload file processing, message queue consumption, database change triggers |
| Microservice Components | Independent 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 Type | Specific Examples |
|---|---|
| Web Applications | Corporate websites, product landing pages, admin dashboards |
| API Services | RESTful API, GraphQL interfaces |
| Framework Migration | Express/Koa/Next.js projects quick cloud deployment |
| Real-Time Communication | WebSocket chat, SSE message push, real-time data streams |
Quick Decision Guide
Not sure which type to choose? Answer the following questions:
Does your service need to directly respond to HTTP requests?
- Yes → Consider HTTP Function
- No → Consider Custom Function
Does your project already use Web frameworks like Express/Koa?
- Yes → Strongly recommend HTTP Function (low migration cost)
- No → Choose based on business scenario
Does your business need to handle high-concurrency Web requests?
- Yes → HTTP Function (single-instance multi-concurrency, better performance)
- No → Either works
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
Do you need WebSocket or SSE real-time communication?
- Yes → Must use HTTP Function
- No → Either works
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