Back to All Technical Blogs
Serverless
May 19, 2025
7 min read

Asynchronous Task Queue Processing with GCP Cloud Tasks and Cloud Run

Sandip Basnet
Sandip Basnet
Senior Software Engineer & SRE

Asynchronous Task Queue Processing with GCP Cloud Tasks and Cloud Run

When building web APIs, long-running operations such as sending transactional emails, generating PDF reports, or calling third-party APIs should never block the main HTTP request-response cycle. Executing synchronous long tasks leads to high API latency and client timeouts.

Google Cloud Tasks paired with Cloud Run push targets provides a serverless task queuing engine with built-in concurrency controls, rate limiting, and exponential retry policies.


Key Components of Cloud Tasks Architecture

  • Producer Service: Your primary Cloud Run API receives client requests, dispatches a task payload to a Cloud Tasks queue within ~10ms, and responds immediately to the user.
  • Cloud Tasks Queue: Holds pending task payloads, enforcing rate limits (e.g. 50 tasks/sec) and scheduling task executions.
  • Worker Service: Cloud Tasks invokes a dedicated Cloud Run endpoint (POST /tasks/process-email) via HTTP push, passing authentication OIDC tokens.
  •   [ User ] ──► [ Frontend API ] ──(Enqueues Task)──► [ Cloud Tasks Queue ]
                          │                                      │
                 (Fast HTTP 200 OK)                     (Controlled Push)
                          ▼                                      ▼
                    [ Instant Response ]               [ Worker Cloud Run ]

    Node.js Producer Code Example

    typescriptSnippet
    import { CloudTasksClient } from '@google-cloud/tasks';
    
    const client = new CloudTasksClient();
    
    async function enqueueEmailTask(userEmail: string, templateId: string) {
      const parent = client.queuePath('my-gcp-project', 'us-central1', 'email-queue');
      
      const task = {
        httpRequest: {
          httpMethod: 'POST' as const,
          url: 'https://worker-api-xyz-uc.a.run.app/tasks/email',
          headers: { 'Content-Type': 'application/json' },
          body: Buffer.from(JSON.stringify({ userEmail, templateId })).toString('base64'),
          oidcToken: {
            serviceAccountEmail: 'cloud-tasks-sa@my-gcp-project.iam.gserviceaccount.com'
          }
        }
      };
    
      await client.createTask({ parent, task });
    }

    Why Choose Cloud Tasks over Pub/Sub for Background Jobs

  • Targeted Scheduling: Schedule tasks to execute at specific future timestamps (e.g. deliver notification in 2 hours).
  • Rate Limiting & Throttling: Cap max concurrent task dispatches to prevent overwhelming downstream legacy APIs.
  • Deduplication & Retries: Automatic configurable retries with exponential backoff intervals.
  • Topic Tags:GCPCloud TasksCloud RunAsynchronous ProcessingQueue ManagementMicroservices
    View All