☁️ Cloud

Serverless

Serverless

Serverless is a cloud computing model in which you only write code, without managing server infrastructure. It is offered as FaaS (Function as a Service) on AWS Lambda, Vercel, Cloudflare Workers, and others, and you are billed only for what you use.

📖 Detailed Explanation

Serverless does not mean "there are no servers" but "there is no need to manage servers." The cloud provider automatically handles server provisioning, scaling, patching, and more, so developers can focus solely on business logic.

Major Serverless Platforms

AWS Lambda

The original serverless platform, supporting the most triggers

Vercel Functions

Next.js integration, Edge Runtime

Cloudflare Workers

V8 Isolate, ultra-low-latency Edge

Supabase Edge

Deno-based, PostgreSQL integration

Serverless vs Traditional Servers

💻 Code Example

AWS Lambda (Node.js)

// handler.js
export const handler = async (event, context) => {
  const { name } = JSON.parse(event.body);

  // 비즈니스 로직
  const greeting = `Hello, ${name}!`;

  return {
    statusCode: 200,
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ message: greeting }),
  };
};

// serverless.yml (Serverless Framework)
service: my-service
provider:
  name: aws
  runtime: nodejs20.x
  region: ap-northeast-2
functions:
  hello:
    handler: handler.handler
    events:
      - http:
          path: /hello
          method: post

Vercel Edge Function

// app/api/hello/route.ts (Next.js App Router)
import { NextRequest } from 'next/server';

export const runtime = 'edge'; // Edge Runtime 사용

export async function GET(request: NextRequest) {
  const { searchParams } = new URL(request.url);
  const name = searchParams.get('name') || 'World';

  return new Response(
    JSON.stringify({ message: `Hello, ${name}!` }),
    {
      status: 200,
      headers: { 'Content-Type': 'application/json' },
    }
  );
}

Cloudflare Workers

// src/index.ts
export default {
  async fetch(request: Request, env: Env): Promise<Response> {
    const url = new URL(request.url);

    if (url.pathname === '/api/hello') {
      const name = url.searchParams.get('name') || 'World';

      return new Response(
        JSON.stringify({ message: `Hello, ${name}!` }),
        {
          headers: { 'Content-Type': 'application/json' },
        }
      );
    }

    return new Response('Not Found', { status: 404 });
  },
};

Cold Start Optimization

// Lambda Provisioned Concurrency (항상 warm 상태 유지)
// serverless.yml
functions:
  api:
    handler: handler.main
    provisionedConcurrency: 5  # 5개 인스턴스 warm 유지

// 또는 코드 레벨 최적화
// 모듈 초기화를 핸들러 밖에서 (재사용됨)
import { DynamoDB } from '@aws-sdk/client-dynamodb';

const dynamodb = new DynamoDB({});  // Cold Start 시 1회만 실행

export const handler = async (event) => {
  // 핸들러는 매 요청마다 실행
  const result = await dynamodb.getItem({ /* ... */ });
  return { statusCode: 200, body: JSON.stringify(result) };
};

💬 Real-world conversation examples

👨‍💼 PM

"We expect traffic to increase 10x with this promotion. Do we need to add more servers?"

👩‍💻 Backend developer

"It's serverless, so it scales automatically. Lambda automatically increases the number of concurrent executions, so a traffic surge isn't a problem. And you're only billed for actual usage."

👨‍💻 Junior developer

"API responses are slow sometimes. Only the first request takes about 2 seconds, and after that it's fast."

👩‍💻 Senior developer

"That's the Cold Start phenomenon. When Lambda isn't used for a while, the container is shut down, and when a request comes in again, it's slow because it has to initialize. Setting up Provisioned Concurrency or using the Edge runtime will improve it."

⚠️ Cautions

Cold Start: If a function is not invoked for a certain period of time, the container is terminated and an initialization delay occurs on the next invocation. For latency-sensitive APIs, consider Provisioned Concurrency or Edge Runtime.

Execution time limit: There are platform-specific limits, such as 15 minutes for Lambda and 30 seconds for Vercel Edge. Separate long-running tasks using Step Functions or queue-based processing.

Tip: Cloudflare Workers is based on V8 Isolate, so there is almost no Cold Start, and it runs on Edge locations around the world, so latency is very low. It is well suited to simple APIs and proxies.

🔗 Related Terms

📚 Learn More