Cloud Native 8 min read

How Migrating a Holiday Gift Service to Alibaba Cloud Function Compute Cuts Costs and Handles Traffic Spikes

Facing occasional traffic spikes and high server costs, the author migrated a holiday gift‑distribution project from an ALB‑ECS‑MySQL‑Redis stack to Alibaba Cloud Function Compute, detailing cost reductions, zero‑idle‑instance savings, custom runtime setup, logging adjustments, VPC networking, deployment via Serverless Devs, and domain binding.

Alibaba Cloud Native
Alibaba Cloud Native
Alibaba Cloud Native
How Migrating a Holiday Gift Service to Alibaba Cloud Function Compute Cuts Costs and Handles Traffic Spikes

Why Move to Alibaba Cloud Function Compute

The project is a holiday gift‑claim service that experiences short‑term traffic surges during festivals while remaining idle most of the time. The original architecture used an Alibaba Cloud ALB, multiple ECS instances, Cloud MySQL, and Cloud Redis, leading to unnecessary costs during low‑traffic periods.

Alibaba Cloud Function Compute (FC) provides a serverless environment where instances are created on demand and terminated when idle, achieving near‑zero cost during off‑peak times. Cold starts may add a slight delay, but a minimum reserved instance can mitigate this.

Benefits of Migration

Significant cost reduction

Elimination of environment setup (no need to install Nginx, PHP, Redis drivers, etc.)

Automatic scaling to handle high concurrency

Code Adjustments

Logging originally wrote to a local directory, which is unsuitable for FC because instances are transient. The solution is to write logs to Alibaba Cloud OSS or use the Cloud Log Service.

Session handling originally relied on server‑side files. Since sessions are stored in Redis in this project, no changes are needed, but projects that keep sessions on disk must migrate them to Redis.

Communication between FC and Cloud MySQL/Redis must use VPC internal networking to reduce latency and avoid security risks.

Serverless Devs Configuration (s.yml)

edition: 1.0.0
name: compoent-test
access: 'default'
services:
  cn-hangzhou-test1002-func-3i3c0f95:
    component: devsapp/fc
    props:
      region: cn-hangzhou
      service:
        logConfig:
          enableRequestMetrics: true
          enableInstanceMetrics: true
          logBeginRule: DefaultRegex
          project: aliyun-fc-cn-hangzhou-ae3ef8b8-db4a-5b7a-a040-7012789ad20f
          logstore: function-log
        role: acs:ram::1621341641365186:role/AliyunFcDefaultRole
        internetAccess: true
        name: test1002
      function:
        customRuntimeConfig:
          command:
            - bash
          args:
            - '-c'
            - 'chmod 777 /code/start.sh && /code/start.sh'
        handler: index.handler
        instanceType: e1
        runtime: custom
        timeout: 5
        instanceConcurrency: 20
        memorySize: 512
        caPort: 9000
        environmentVariables: {}
        internetAccess: true
        name: func-3i3c0f95
        asyncConfiguration: {}
        codeUri: ./test1002/func-3i3c0f95
      triggers:
        - name: defaultTrigger
          description: ''
          type: http
          qualifier: LATEST
          config:
            methods:
              - GET
              - POST
              - PUT
              - DELETE
            authType: anonymous

Startup Script (start.sh)

#!/usr/bin/env bash
cd /code/tp5/public
php -S 0.0.0.0:9000 router.php

The script grants execution permission to start.sh and runs it. The caPort in the configuration (9000) must match the port used by the script.

Deployment Using Serverless Devs

Install the Serverless Devs CLI (installation steps omitted for brevity).

Configure Alibaba Cloud AccessKey ID and AccessKey Secret.

Create s.yml at the project root and place start.sh in the code directory.

Run s deploy to publish the function to Alibaba Cloud FC.

After deployment, the service can be accessed via the generated HTTP endpoint.

Binding a Custom Domain

In the FC console, add a CNAME record pointing your domain to the provided public CNAME, then select the correct service name and function version before creating the binding.

Personal Reflection

Serverless is the future, allowing developers to focus on business logic. The migration, from code changes to reading FC documentation, took about three days and deepened the author's understanding of Alibaba Cloud Function Compute. Ongoing monitoring will ensure stability during future traffic peaks.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Cloud NativeServerlessDeploymentDevOpsPHPAlibaba CloudFunction Compute
Alibaba Cloud Native
Written by

Alibaba Cloud Native

We publish cloud-native tech news, curate in-depth content, host regular events and live streams, and share Alibaba product and user case studies. Join us to explore and share the cloud-native insights you need.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.