Pie Logo
Quiz: Server-less Architectures
by Tomas Trescak

QuizTest your understanding of the section content.

ContentDetailed description of the section content.

Full Stack Development
Introduction
Generative AI
🍰 Player
💼 Editor
Setup
React Fundamentals
Next.js
Advanced React
Databases
React Hooks
Authentication and Authorisation
APIs
CI/CD and DevOps
Continuous Integration (CI)
Continuous Deployment (CD)
Server-less Architectures
Quiz: Server-less Architectures
Exercise: CI/CD
Major Assignment
Testing React
Advanced Topics
    Pie Logo
    Quiz: Server-less Architectures
    by Tomas Trescak

    QuizTest your understanding of the section content.

    ContentDetailed description of the section content.

    Full Stack Development
    Introduction
    Generative AI
    🍰 Player
    💼 Editor
    Setup
    React Fundamentals
    Next.js
    Advanced React
    Databases
    React Hooks
    Authentication and Authorisation
    APIs
    CI/CD and DevOps
    Continuous Integration (CI)
    Continuous Deployment (CD)
    Server-less Architectures
    Quiz: Server-less Architectures
    Exercise: CI/CD
    Major Assignment
    Testing React
    Advanced Topics
    QUESTION 1

    Your blogging app’s /api/posts?tag=tech endpoint is getting slow for users in Asia. You want to speed it up using the edge function below. What changes would make it perform better globally?

    import type { NextRequest, NextResponse } from 'next/server';
    export const config = {
      runtime: 'nodejs', // Current setting
    };
    export default async function handler(req: NextRequest): Promise<NextResponse> {
      const url = new URL(req.url);
      const tag = url.pathname.slice(1);
      if (tag) {
        url.pathname = '/api/posts';
        url.searchParams.set('tag', tag);
        return fetch(url);
      }
      return new NextResponse('Invalid tag', { status: 400 });
    }
    


    QUESTION 2

    BlogStar, your blogging app, gets a traffic spike to 10,000 users after a viral post. The /api/posts Lambda function below handles it. Which factors ensure it scales without crashing?

    import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
    import { DynamoDBClient, ScanCommand } from '@aws-sdk/client-dynamodb';
    import { unmarshall } from '@aws-sdk/util-dynamodb';
    const client = new DynamoDBClient({ region: 'us-east-1' });
    export const handler = async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {
      const tag = event.queryStringParameters?.tag || '';
      const params = {
        TableName: 'BlogPosts',
        FilterExpression: tag ? 'contains(tags, :tag)' : undefined,
        ExpressionAttributeValues: tag ? { ':tag': { S: tag } } : undefined,
      };
      try {
        const result = await client.send(new ScanCommand(params));
        const posts = result.Items?.map(item => unmarshall(item)) || [];
        return {
          statusCode: 200,
          body: JSON.stringify(posts),
        };
      } catch (error) {
        return {
          statusCode: 500,
          body: JSON.stringify({ error: 'Failed to fetch posts' }),
        };
      }
    };
    


    QUESTION 3

    You deploy an edge function to rewrite /tech to /api/posts?tag=tech, but users see a 504 Timeout error. What likely caused it, based on this code?

    import type { NextRequest, NextResponse } from 'next/server';
    export const config = {
      runtime: 'edge',
    };
    export default async function handler(req: NextRequest): Promise<NextResponse> {
      const url = new URL(req.url);
      const tag = url.pathname.slice(1);
      if (tag) {
        const posts = await fetch('https://slow-api.com/posts').then(res => res.json());
        const filtered = posts.filter((post: any) => post.tags.includes(tag));
        return new NextResponse(JSON.stringify(filtered), { status: 200 });
      }
      return new NextResponse('Invalid tag', { status: 400 });
    }
    


    QUESTION 4

    Blog’s admin page at /admin is slow to load during peak times. You’re deploying the Next.js app as Lambda. What can improve performance?


    QUESTION 5

    A user in London visits /tech on your blog, expecting fast load times. Your edge function uses country-based personalization. What ensures the speed?

    import type { NextRequest, NextResponse } from 'next/server';
    export const config = {
      runtime: 'edge',
    };
    export default async function handler(req: NextRequest): Promise<NextResponse> {
      const url = new URL(req.url);
      const tag = url.pathname.slice(1);
      const country = req.headers.get('x-vercel-ip-country') || 'US';
      if (tag) {
        url.pathname = '/api/posts';
        url.searchParams.set('tag', tag);
        url.searchParams.set('country', country);
        const response = await fetch(url);
        return new NextResponse(response.body, {
          status: response.status,
          headers: { 'x-country': country },
        });
      }
      return new NextResponse('Invalid tag', { status: 400 });
    }
    


    QUESTION 6

    You notice high costs for your blog’s Lambda-based API during a traffic spike. What could reduce expenses without losing functionality?

    This content is only available to registered users.
    Please register at the the home page.

    QUESTION 1

    Your blogging app’s /api/posts?tag=tech endpoint is getting slow for users in Asia. You want to speed it up using the edge function below. What changes would make it perform better globally?

    import type { NextRequest, NextResponse } from 'next/server';
    export const config = {
      runtime: 'nodejs', // Current setting
    };
    export default async function handler(req: NextRequest): Promise<NextResponse> {
      const url = new URL(req.url);
      const tag = url.pathname.slice(1);
      if (tag) {
        url.pathname = '/api/posts';
        url.searchParams.set('tag', tag);
        return fetch(url);
      }
      return new NextResponse('Invalid tag', { status: 400 });
    }
    


    QUESTION 2

    BlogStar, your blogging app, gets a traffic spike to 10,000 users after a viral post. The /api/posts Lambda function below handles it. Which factors ensure it scales without crashing?

    import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
    import { DynamoDBClient, ScanCommand } from '@aws-sdk/client-dynamodb';
    import { unmarshall } from '@aws-sdk/util-dynamodb';
    const client = new DynamoDBClient({ region: 'us-east-1' });
    export const handler = async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {
      const tag = event.queryStringParameters?.tag || '';
      const params = {
        TableName: 'BlogPosts',
        FilterExpression: tag ? 'contains(tags, :tag)' : undefined,
        ExpressionAttributeValues: tag ? { ':tag': { S: tag } } : undefined,
      };
      try {
        const result = await client.send(new ScanCommand(params));
        const posts = result.Items?.map(item => unmarshall(item)) || [];
        return {
          statusCode: 200,
          body: JSON.stringify(posts),
        };
      } catch (error) {
        return {
          statusCode: 500,
          body: JSON.stringify({ error: 'Failed to fetch posts' }),
        };
      }
    };
    


    QUESTION 3

    You deploy an edge function to rewrite /tech to /api/posts?tag=tech, but users see a 504 Timeout error. What likely caused it, based on this code?

    import type { NextRequest, NextResponse } from 'next/server';
    export const config = {
      runtime: 'edge',
    };
    export default async function handler(req: NextRequest): Promise<NextResponse> {
      const url = new URL(req.url);
      const tag = url.pathname.slice(1);
      if (tag) {
        const posts = await fetch('https://slow-api.com/posts').then(res => res.json());
        const filtered = posts.filter((post: any) => post.tags.includes(tag));
        return new NextResponse(JSON.stringify(filtered), { status: 200 });
      }
      return new NextResponse('Invalid tag', { status: 400 });
    }
    


    QUESTION 4

    Blog’s admin page at /admin is slow to load during peak times. You’re deploying the Next.js app as Lambda. What can improve performance?


    QUESTION 5

    A user in London visits /tech on your blog, expecting fast load times. Your edge function uses country-based personalization. What ensures the speed?

    import type { NextRequest, NextResponse } from 'next/server';
    export const config = {
      runtime: 'edge',
    };
    export default async function handler(req: NextRequest): Promise<NextResponse> {
      const url = new URL(req.url);
      const tag = url.pathname.slice(1);
      const country = req.headers.get('x-vercel-ip-country') || 'US';
      if (tag) {
        url.pathname = '/api/posts';
        url.searchParams.set('tag', tag);
        url.searchParams.set('country', country);
        const response = await fetch(url);
        return new NextResponse(response.body, {
          status: response.status,
          headers: { 'x-country': country },
        });
      }
      return new NextResponse('Invalid tag', { status: 400 });
    }
    


    QUESTION 6

    You notice high costs for your blog’s Lambda-based API during a traffic spike. What could reduce expenses without losing functionality?

    This content is only available to registered users.
    Please register at the the home page.

    Pie Logo
    Quiz: Server-less Architectures
    by Tomas Trescak
    Pie Logo
    Quiz: Server-less Architectures
    by Tomas Trescak
    Full Stack Development
    Introduction
    Generative AI
    🍰 Player
    💼 Editor
    Setup
    React Fundamentals
    Next.js
    Advanced React
    Databases
    React Hooks
    Authentication and Authorisation
    APIs
    CI/CD and DevOps
    Continuous Integration (CI)
    Continuous Deployment (CD)
    Server-less Architectures
    Quiz: Server-less Architectures
    Exercise: CI/CD
    Major Assignment
    Testing React
    Advanced Topics
    Full Stack Development
    Introduction
    Generative AI
    🍰 Player
    💼 Editor
    Setup
    React Fundamentals
    Next.js
    Advanced React
    Databases
    React Hooks
    Authentication and Authorisation
    APIs
    CI/CD and DevOps
    Continuous Integration (CI)
    Continuous Deployment (CD)
    Server-less Architectures
    Quiz: Server-less Architectures
    Exercise: CI/CD
    Major Assignment
    Testing React
    Advanced Topics