0Pricing
Supabase Backend as a Service · 강의

수파베이스와 워커를 활용한 작업 큐

수파베이스와 외부 워커 서비스를 사용하여 백그라운드 작업 처리와 메시지 큐를 구현하는 전략을 살펴봅니다.

수파베이스와 워커를 활용한 작업 큐은(는) CoddyKit의 무료 Supabase Backend as a Service 강의입니다. 이것은 3개 중 2번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Supabase Backend as a Service 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Supabase Backend as a Service 강의에는 총 3개의 강의가 포함되어 있습니다.

이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.

Tasks: Background vs. Foreground

Imagine your app needs to send a welcome email, process a large image, or generate a report. If your user has to wait for these long tasks to finish, their experience suffers.

These are background tasks: operations that don't need immediate user interaction and can run independently without blocking the user interface.

Introducing Task Queues

A task queue (or message queue) is a system that allows different parts of your application to communicate asynchronously. It acts like a temporary holding area for tasks.

  • Producers: Add tasks to the queue.
  • Consumers (Workers): Pick up tasks from the queue and process them.

This decouples the task creation from its execution.

Why Use a Task Queue?

Task queues bring several key benefits to your application architecture:

  • Improved Responsiveness: Users don't wait for long operations.
  • Scalability: You can add more workers to handle increased load.
  • Reliability: Tasks can be retried if they fail.
  • Decoupling: Separates task submission from task execution.

Supabase as a Simple Queue

While Supabase isn't a dedicated message queue, its powerful PostgreSQL database can serve as a simple task queue for many use cases. We can create a dedicated table to store tasks.

A typical tasks table might have columns like:

  • id (Primary Key)
  • payload (JSONB, for task data)
  • status (e.g., 'pending', 'processing', 'completed', 'failed')
  • created_at (Timestamp)
  • processed_at (Timestamp, nullable)

Enqueueing Tasks (Producer)

To enqueue a task, your client-side application or API endpoint simply inserts a new row into the tasks table with a 'pending' status. The payload column holds all the necessary data for the worker to process.

For example, to send a welcome email after user signup:

import { createClient } from '@supabase/supabase-js';

const supabaseUrl = 'YOUR_SUPABASE_URL';
const supabaseKey = 'YOUR_SUPABASE_ANON_KEY';
const supabase = createClient(supabaseUrl, supabaseKey);

async function enqueueWelcomeEmail(userId, email) {
  const { data, error } = await supabase
    .from('tasks')
    .insert({
      type: 'send_welcome_email',
      payload: { userId, email },
      status: 'pending'
    });

  if (error) {
    console.error('Error enqueueing task:', error.message);
  } else {
    console.log('Welcome email task enqueued!');
  }
}

// Example usage (not runnable directly without Supabase setup)
// enqueueWelcomeEmail('user123', 'test@example.com');

Introducing External Workers

An external worker is a separate application or service that continuously monitors the task queue for new jobs. When it finds a 'pending' task, it picks it up, processes it, and updates its status.

Workers can be written in any language (Node.js, Python, Go) and run on various platforms (servers, serverless functions, containers). They connect to your Supabase database to read and update task records.

Dequeueing Tasks (Consumer/Worker)

A worker needs to:

  1. Fetch 'pending' tasks.
  2. Mark a task as 'processing' to prevent other workers from picking it up.
  3. Execute the task logic using the payload.
  4. Update the task status to 'completed' or 'failed'.

Workers can either poll (periodically check) or use Supabase's Realtime feature to listen for new task insertions.

Code: Simple Worker Logic (Polling)

Here's a simplified Node.js worker logic that polls the Supabase tasks table every few seconds. Remember to replace placeholder values.

// worker.js
const { createClient } = require('@supabase/supabase-js');

const SUPABASE_URL = 'YOUR_SUPABASE_URL';
const SUPABASE_KEY = 'YOUR_SUPABASE_SERVICE_ROLE_KEY'; // Use service key for workers
const supabase = createClient(SUPABASE_URL, SUPABASE_KEY);

async function processTask(task) {
  console.log(`Processing task ${task.id}: ${task.type}`);
  // Simulate work (e.g., sending email, processing image)
  await new Promise(resolve => setTimeout(resolve, 2000));
  console.log(`Task ${task.id} processed.`);
  // In a real app, this would involve calling an email API etc.
}

async function runWorker() {
  console.log('Worker started, looking for tasks...');
  setInterval(async () => {
    try {
      // Fetch one pending task and lock it by updating status
      const { data: tasks, error } = await supabase
        .from('tasks')
        .select('*')
        .eq('status', 'pending')
        .order('created_at', { ascending: true })
        .limit(1);

      if (error) throw error;

      if (tasks.length > 0) {
        const task = tasks[0];
        // Atomically update status to 'processing'
        const { error: updateError } = await supabase
          .from('tasks')
          .update({ status: 'processing', processed_at: new Date().toISOString() })
          .eq('id', task.id)
          .eq('status', 'pending'); // Ensure no other worker picked it up

        if (updateError) {
          if (updateError.code === '40600') { // Row already updated by another process
            console.log(`Task ${task.id} already picked up.`);
            return; 
          }
          throw updateError;
        }

        await processTask(task);

        // Update status to 'completed'
        await supabase
          .from('tasks')
          .update({ status: 'completed' })
          .eq('id', task.id);

      } else {
        // console.log('No pending tasks.');
      }
    } catch (err) {
      console.error('Worker error:', err.message);
      // Implement error handling, e.g., update task status to 'failed'
    }
  }, 5000); // Poll every 5 seconds
}

// To run this: node worker.js
// (Requires 'npm install @supabase/supabase-js' and a Supabase project)
runWorker();

Task States & Reliability

Properly managing task states is crucial for reliability:

  • Pending: Task is waiting to be processed.
  • Processing: Worker has picked up the task.
  • Completed: Task finished successfully.
  • Failed: Task encountered an error.

For failed tasks, you might implement retries (e.g., after a delay) and ensure tasks are idempotent (running them multiple times has the same effect as running once) to prevent unintended side effects.

Queueing Question

You've learned about using Supabase and external workers for task queues. Consider a scenario where a user uploads a large video file that needs transcoding.

Recap: Task Queues & Workers

We've explored how task queues enable efficient background processing in your applications. By using Supabase as a simple queue and external workers, you can:

  • Offload long-running operations.
  • Improve user experience by keeping your app responsive.
  • Build more scalable and robust systems.

This pattern is fundamental for building complex, high-performance web and mobile applications.

자주 묻는 질문

“수파베이스와 워커를 활용한 작업 큐” 강의는 무료인가요?

네 — “수파베이스와 워커를 활용한 작업 큐” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Supabase Backend as a Service 강의 전체를 잠금 해제할 수 있습니다. Supabase Backend as a Service 강의에는 총 3개의 강의가 포함되어 있습니다.

“수파베이스와 워커를 활용한 작업 큐”에서 뭘 배우나요?

수파베이스와 외부 워커 서비스를 사용하여 백그라운드 작업 처리와 메시지 큐를 구현하는 전략을 살펴봅니다. 브라우저에서 직접 실행하는 실습 코드로 Supabase Backend as a Service을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Supabase Backend as a Service을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 Supabase Backend as a Service은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 3개 중 2번째 강의입니다.

“수파베이스와 워커를 활용한 작업 큐” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 Supabase Backend as a Service 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 Supabase Backend as a Service 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. 외부 서비스와 통합하기
  2. 수파베이스와 워커를 활용한 작업 큐
  3. pg_cron으로 반복 작업 예약하기
← Supabase Backend as a Service(으)로 돌아가기