Lesson 12 / 25

Cloud Tasks, Cloud Scheduler and Workflows

Schedule jobs, queue work to an endpoint and orchestrate multi-step processes.

Three helpers for background work

Cloud Scheduler is managed cron: on a schedule it calls an HTTP endpoint, publishes to Pub/Sub or triggers a workflow, for example to start a nightly Cloud Run job. Cloud Tasks is a queue of individual tasks delivered to an HTTP endpoint with rate limits, retries with backoff and scheduled delivery times; use it when you want to control how fast a downstream service is called, such as sending emails at 10 per second. Compare with Pub/Sub, where publishers do not control delivery and many subscribers can receive the same event. Workflows orchestrates steps defined in YAML: call services and APIs in order, branch, loop, wait, handle errors and retries, keeping state between steps without you running a server.

A small workflow

Validate an order, charge it, and notify, with a retry policy on the payment call.

main:
  params: [order]
  steps:
    - validate:
        call: http.post
        args:
          url: https://orders-api-xyz.a.run.app/validate
          body: ${order}
          auth: { type: OIDC }
        result: validation
    - charge:
        try:
          call: http.post
          args:
            url: https://payments-xyz.a.run.app/charge
            body: ${order}
            auth: { type: OIDC }
          result: payment
        retry: ${http.default_retry}
    - notify:
        call: googleapis.pubsub.v1.projects.topics.publish
        args:
          topic: projects/shop-dev-123456/topics/order-paid
          body:
            messages:
              - data: ${base64.encode(json.encode(order))}
    - done:
        return: ${payment.body}

Pick by who controls the pace

Fan-out of events: Pub/Sub. Rate-limited calls to one service: Cloud Tasks. Run at a time: Cloud Scheduler. A multi-step process with branching and retries: Workflows.

Quick check: You must call a partner API for each signup but never exceed 5 calls per second. Which service fits best?

  • Cloud Tasks
  • Cloud Scheduler
  • Pub/Sub with many subscriptions
  • Cloud Storage
Answer

Cloud Tasks — Cloud Tasks queues individual tasks to an endpoint with configurable dispatch rate limits.