Cloud Batch in Google Cloud
A beginner-friendly guide to running big, heavy, "do this thousands of times" jobs on Google Cloud without managing a single server yourself.
Imagine you own a bakery and a customer walks in and orders 50,000 cupcakes for tomorrow morning. You cannot bake them one at a time in your small kitchen. So you rent 500 ovens for one night, hire helpers, split the work, bake everything, and then give the ovens back. You only pay for the hours you used them.
That is the exact idea behind Cloud Batch (officially called Batch) in Google Cloud. It is a fully managed service that takes a big pile of work, finds computers for it, runs it, and cleans up afterwards. In this guide you will learn every important idea from zero, in simple words, with analogies for each one.
1What Is Batch Computing?
Before we meet the Google Cloud service, we need to understand the kind of work it is built for.
Two kinds of programs
Most programs you use every day are interactive. You tap a button and get an answer in a blink. A website, a chat app, and a map are all interactive. Somebody is waiting for each answer.
A batch program is different. Nobody is staring at the screen. You hand it a large amount of work, it chews through the work for minutes or hours, and it leaves the results somewhere for you to pick up later. Examples include turning a movie scene into finished video frames, scanning millions of photos, analyzing DNA, running a weather forecast, and processing a whole month of bank transactions.
An interactive program is a waiter taking your order and bringing you a drink right now. A batch program is the laundry service. You drop off a big bag in the evening, they wash it overnight, and you collect it tomorrow. Nobody stands around watching the washing machine.
What is a “job”?
In batch computing, one complete piece of work you want done is called a job. A job usually breaks into many small pieces that can run at the same time. Each small piece is a task. Running many tasks together is called parallel processing, and it is how huge jobs finish quickly.
So what is Cloud Batch?
Batch is a Google Cloud service that lets you describe a job, then it does the boring heavy lifting for you. It creates virtual machines (VMs) on Compute Engine, starts your program on them, retries things that fail, collects the logs, and deletes the machines when the job ends. You never log in to a server to set up a cluster.
to manage
task
resources you use
There is no extra fee for the Batch service itself. You pay for the underlying Google Cloud resources it uses, such as VMs, disks, networking, and storage. Always check the current pricing page before planning a big budget.
2Why Does Cloud Batch Exist?
Every good tool is born from a pain. Here is the pain that Batch removes.
The old way: build your own cluster
Before managed batch services, a team that needed to process a huge workload had to build a mini data center. They installed a job scheduler, set up a head node, created a pool of worker machines, wrote scripts to add and remove machines, and watched everything day and night. If one worker died halfway, somebody had to notice and restart the work.
This is slow to build, hard to keep healthy, and wasteful. Machines sat idle between jobs but you still paid for them. Or the cluster was too small at the critical moment and work waited in line for days.
The Batch way: describe it, submit it, forget it
With Batch, you write down what you want: what program to run, how many copies, and how big the machines should be. You submit the job. Google Cloud finds capacity, builds the machines, runs the work, and shuts everything down. The machines exist only while your job needs them.
Building your own cluster is like buying a fleet of trucks to move house once. Using Batch is like calling a moving company. You say what needs moving and when, they bring the trucks, and they take the trucks away when the move is over.
Problems Batch solves
Elastic compute
Scale from one machine to a very large fleet for the length of a job, then back to zero.
No scheduler to run
Queueing, placement, retries, and cleanup are handled for you.
Pay only while running
No idle cluster. Spot VMs can cut the price further for work that can restart.
Bring your own code
Run a plain script or a container. Use the tools your team already knows.
3Core Concepts You Must Know
Learn these five words and the rest of the service will feel easy.
Job
A job is the top-level thing you submit. It has a name, a location (a Google Cloud region), and a description of the work. Once submitted, it moves through states such as queued, scheduled, running, succeeded, or failed.
Task group
A task group is a set of identical tasks inside a job. All tasks in one group run the same instructions and use the same kind of machine. You set how many tasks the group has. A job can hold several task groups, but a simple job has just one.
Task
A task is the smallest unit of work. Each task gets its own number, starting at zero. Your program can read that number, so task 0 can handle the first slice of data, task 1 the second slice, and so on.
Think of grading exam papers. The job is “grade all 3,000 papers”. The task group is “the team of graders who all use the same answer sheet”. Each task is one stack of papers handed to one grader. Every stack has a number written on top so nobody grades the same stack twice.
Runnable
A runnable is the actual instruction a task performs. Each task runs a list of runnables in order. There are three main kinds:
- Script runnable: a block of shell commands, or a script file, that runs directly on the VM.
- Container runnable: a container image that is pulled and run. This is the best choice when you want your program and all its libraries packaged together.
- Barrier runnable: a “wait here” marker. Every task must reach it before any task continues. It is used when tasks must stay in step with each other.
Environment variables
Batch gives every task a few helpful built-in values, such as the task’s own index number, the total number of tasks, and which retry attempt this is. Your program uses them to know “which slice is mine?”. You can also add your own values, and you can load secrets from Secret Manager so passwords never sit in plain text.
Job holds task groups. Task groups hold tasks. Tasks run runnables. If you can say that sentence from memory, you have learned the data model of Batch.
4Architecture and Components
Let us look at the big pieces and how they talk to each other.
The main pieces
When you use Batch, several parts of Google Cloud work together. Here they are in plain words.
- You and your tools: the Google Cloud console, the gcloud command line, the API, client libraries, or tools such as Terraform. They all send the same job description.
- The Batch service: the brain. It accepts your job, keeps a queue, decides when and where it can run, and tracks its state.
- Compute Engine: the muscle. Batch asks Compute Engine to create the VMs that run your tasks. Behind the scenes it uses managed groups of VMs.
- The Batch agent: a small helper program that runs on every VM. It starts your runnables, watches them, and reports progress back to the service.
- Storage: places to read input and write results, such as Cloud Storage buckets, shared network file systems, and disks.
- Cloud Logging and Pub/Sub: where your program’s output goes, and where state-change messages can be sent.
flowchart LR
U["You: Console, gcloud, API, Terraform"] --> B["Batch Service: queue and scheduler"]
B --> CE["Compute Engine: creates VMs"]
CE --> VM1["VM 1 with Batch Agent"]
CE --> VM2["VM 2 with Batch Agent"]
CE --> VM3["VM 3 with Batch Agent"]
VM1 --> ST["Storage: Cloud Storage, Filestore, Disks"]
VM2 --> ST
VM3 --> ST
VM1 --> LG["Cloud Logging"]
VM2 --> LG
VM3 --> LG
B --> PS["Pub/Sub notifications"]
Read the picture from left to right. You submit a job. The Batch service queues it and asks Compute Engine for machines. Each machine runs the Batch agent, which starts your tasks. Tasks read and write storage, send logs to Cloud Logging, and the service can announce progress through Pub/Sub.
The Batch service is a construction manager. Compute Engine is the equipment rental yard. The Batch agent is the foreman on each work site who actually tells the workers what to do and calls the manager with updates. Storage is the warehouse of materials and finished goods.
Regions matter
Batch is a regional service. You choose where the job runs. Pick a region close to your data, because moving huge files across the world is slow and can cost money. You can also limit which zones inside the region may be used.
5How a Job Runs Internally
Follow one job from the moment you press submit to the moment it finishes.
Submit and validate
You send the job description. Batch checks that it is well formed and that you have permission. If something is wrong, you hear about it right away.
Queued
The job waits in line. If there is not enough capacity or quota right now, it stays here until resources free up.
Scheduled
Batch has found a place to run. It asks Compute Engine to create the VMs with the right size, image, disks, and network settings.
Running
The VMs boot. The agent starts, mounts any storage you asked for, then runs your runnables for each task.
Retry or finish
If a task fails in a way you allowed to retry, it is run again. When all tasks are done, the job is marked succeeded or failed.
Cleanup
The VMs are deleted. Logs and your output files remain so you can look at them later.
stateDiagram-v2
[*] --> QUEUED
QUEUED --> SCHEDULED
SCHEDULED --> RUNNING
RUNNING --> SUCCEEDED
RUNNING --> FAILED
SUCCEEDED --> [*]
FAILED --> [*]
The states in Figure 2 are exactly what you will see in the console. Most waiting time happens in the first two states. Most of your money is spent in the running state.
What happens inside one task?
On each VM, the agent walks through the list of runnables in order. Normally, if one runnable fails, the task stops and is marked failed. You can change this. A runnable can be marked to run no matter what happened before, which is perfect for cleanup steps. Another can be marked to run in the background, such as a helper program that must stay alive while the main work happens. You can also say that a certain failure should simply be ignored.
A job that sits in the queued state is not always broken. The most common reason is not enough quota or no free capacity in your chosen region. Check your quotas before assuming something is wrong with the job itself.
6Data Flow and Storage
Compute is useless without data. Here is how data gets in and out.
The rule of the temporary machine
The VMs that Batch creates are temporary. When the job ends, they are deleted, and anything stored only on their own disks disappears. So the golden rule is: read your input from permanent storage and write your results to permanent storage.
Storage choices
| Option | What it is | Best for |
|---|---|---|
| Cloud Storage bucket | Cheap, huge object storage that can be mounted like a folder | Large input files and final results |
| Filestore or other NFS share | A shared network drive many VMs can use at once | Tools that expect a normal shared file system |
| Persistent Disk | A network disk attached to a VM | Bigger working space for a VM |
| Local SSD | A very fast disk physically close to the VM | Scratch files that need high speed |
| BigQuery or databases | Structured data reached through their own clients | Tables, records, and analytics output |
Cloud Storage is a big public library: it holds everything safely and anyone with a card can borrow. A local SSD is the notepad on your desk: very quick to use, but you throw it away when you leave for the day.
A typical data path
flowchart TD
IN["Input bucket in Cloud Storage"] --> T0["Task 0 reads slice 0"]
IN --> T1["Task 1 reads slice 1"]
IN --> T2["Task 2 reads slice 2"]
T0 --> OUT["Output bucket in Cloud Storage"]
T1 --> OUT
T2 --> OUT
OUT --> NEXT["Next step: BigQuery, dashboard, or another job"]
Because each task knows its own number, the split is simple. With 1,000 input files and 100 tasks, task 0 takes files 0 to 9, task 1 takes files 10 to 19, and so on. No two tasks fight over the same file.
Write each task’s output to its own unique file name, usually including the task number. That makes retries safe, because a re-run simply replaces the same file instead of creating duplicates.
7Choosing the Right Compute
Different jobs need different machines. Batch lets you pick.
Machine types
A machine type is the size and shape of a VM: how many CPUs, how much memory. A job that handles lots of numbers may want many CPUs. A job that loads a giant file into memory wants lots of RAM. You choose a machine type per task group, or you let Batch pick from a list of allowed types.
GPUs and special hardware
A GPU is a chip with thousands of tiny workers that is very good at math on large blocks of numbers. Machine learning training, video processing, and scientific simulation often use GPUs. Batch can attach GPUs to the VMs it creates, so you only pay for them while the job runs.
Standard versus Spot
Google Cloud sells spare computer capacity at a big discount. These are called Spot VMs. The catch is that Google may take them back with short notice when it needs the capacity. Standard VMs cost more but stay until your job is done.
Spot VMs: pros
- Much lower price than standard VMs
- Great for big jobs split into many small tasks
- Combined with retries, the job still finishes
Spot VMs: cons
- A VM can be taken away at any time
- Long single tasks may keep restarting
- Not suitable for work that cannot be repeated safely
Standard VMs are like booking a hotel room in advance: guaranteed but full price. Spot VMs are like last-minute empty rooms sold cheap: a great deal, but the hotel might ask you to move out if a guest with a reservation arrives.
Where tasks land on machines
You can control how many tasks share one VM. If each task needs one CPU and your VM has eight, you can run up to eight tasks at once on that VM. You can also set a limit on how many tasks run at the same time across the whole job, which protects other systems your tasks may be calling, such as a database.
Images and networking
The VM needs an operating system image. Batch offers images prepared for batch work, and you can use your own custom image if your software is complicated to install. For networking, you can attach the VMs to your own private network. If you do not want the VMs to have public internet addresses, you can switch that off and rely on private access to Google services.
8Parallelism, Retries, and Teamwork Between Tasks
How to make a job faster, and how to survive things going wrong.
Two shapes of parallel work
Embarrassingly parallel work is the easy kind. Every task is independent and never talks to the others. Rendering frames of a film is a classic case, because frame 500 does not need to know about frame 501. Batch is excellent at this.
Tightly coupled work is the harder kind. Tasks must exchange messages constantly while they run, as in weather simulation or physics modeling. These often use a communication standard called MPI. For this you can ask Batch to place all required hosts together, and you can use a barrier so every task waits for the others at key points.
Independent tasks are like a hundred people each painting a different wall in different houses. Tightly coupled tasks are like a rowing crew: every rower must pull at the same moment or the boat goes nowhere.
Retries and failure rules
Machines break, networks hiccup, and Spot VMs get reclaimed. Batch lets you set a maximum retry count for each task, so a failed task is simply started again. You can also write lifecycle policies. A lifecycle policy says: “if the task ends with this exit code, take this action.” For example, an exit code that means “temporary network error” can trigger a retry, while a code that means “bad input data” can fail the task immediately so you do not waste money retrying something that will never work.
Time limits
You can set a maximum run time for a task. If a program gets stuck in an endless loop, the time limit stops it before it burns through your budget.
flowchart TD
S["Task starts"] --> R["Runs the runnables"]
R --> OK{"Finished well?"}
OK -- "Yes" --> D["Task succeeded"]
OK -- "No" --> P{"Lifecycle policy and retries left?"}
P -- "Retry allowed" --> S
P -- "Not allowed or no retries left" --> F["Task failed"]
Make every task idempotent. That is a fancy word meaning “running it twice gives the same result as running it once”. Without this, retries can create duplicate records or double-charge a customer.
9Security
Powerful machines running on your behalf must be locked down properly.
Who can do what: IAM
Google Cloud uses IAM (Identity and Access Management) to decide who may do what. For Batch there are roles for people who create jobs, people who only view jobs, and administrators. Give each person the smallest role that lets them do their work. This idea is called least privilege.
The job’s own identity: service accounts
The VMs run as a service account, which is an identity for programs rather than people. Your tasks use this identity to read buckets, call APIs, or write to a database. Create a dedicated service account for each kind of job and give it only the permissions that job needs, never broad owner-level power. The Batch agent on the VM also needs a role that lets it report progress back to the service.
A service account is a staff badge. A cleaner’s badge opens the supply room but not the vault. If the badge is lost, the damage is small because it never had much power.
Network safety
- Run VMs inside your own VPC network so firewall rules apply.
- Turn off external IP addresses when the tasks only need Google services.
- Allow only the network paths that the job truly needs.
Secrets and data protection
Never put passwords or API keys inside your job description or container image. Store them in Secret Manager and let Batch hand them to your task as environment variables at run time. Data at rest in Google Cloud is encrypted by default, and you can use your own encryption keys if your rules require it. Use trusted container images, and scan them for known weaknesses before you run them at scale.
Example of a safe setup
A genomics team runs a job with its own service account that can read one input bucket and write one output bucket, nothing else. VMs have no public IP addresses, and the database password comes from Secret Manager. If a task is ever compromised, the attacker reaches only two buckets.
10Monitoring, Logging, and Notifications
A job that nobody watches can fail quietly. Here is how to keep an eye on it.
Logs
Anything your program prints to its normal output can be sent to Cloud Logging. You turn this on with a logs setting in the job. Each log line is labeled with the job and the task, so you can filter to see only task 742 when it misbehaves. Without this setting, you may have no way to see why a task died, so switch it on for every job.
Job and task states
The console and the API show the state of the job and of every task. You can see how many tasks are pending, running, succeeded, and failed. This gives you a quick view of progress, such as “812 of 1,000 tasks done”.
Metrics and alerts
The VMs report normal Compute Engine metrics, such as CPU use and memory. If CPU is almost zero, your tasks may be waiting on slow storage. If memory is maxed out, the machine type is too small. You can build dashboards and alerts in Cloud Monitoring around these numbers.
Notifications with Pub/Sub
You can ask Batch to publish a message to a Pub/Sub topic whenever the job or a task changes state. Another program can listen to these messages, then send a chat alert, start the next job, or update a status page. This turns Batch into a building block of larger automated pipelines.
Logs are the security camera footage you review after something goes wrong. Metrics are the dashboard lights on a car. Pub/Sub notifications are the text message your delivery service sends when your parcel moves to the next step.
Have your program print its task number and what it is doing at the start and end of each major step. When you read logs at 2 AM, you will be grateful for clear messages.
11Advantages, Disadvantages, and Trade-offs
No tool is perfect. Here is an honest look.
Advantages
- No cluster to build or patch
- Pay only while the job runs
- Works with scripts and containers
- Supports Spot VMs, GPUs, and big machines
- Built-in retries, logging, and notifications
- Fits easily into larger Google Cloud pipelines
Disadvantages
- Not made for instant answers; jobs can wait in a queue
- Startup takes time because VMs must boot
- Limited by your quotas and regional capacity
- Tied to Google Cloud, so moving elsewhere needs rework
- Complex tightly coupled work still needs expert design
How does it compare to its neighbors?
| Service | Think of it as | Choose it when |
|---|---|---|
| Batch | Rent machines for a big job, then return them | You have heavy compute, scripts or containers, and need to control machine type |
| Cloud Run jobs | Run a container to completion with no VM thinking | Tasks are small, fit in a container, and you want the simplest setup |
| GKE Jobs | Batch work inside a Kubernetes cluster | You already run Kubernetes and want everything in one place |
| Dataflow | A managed pipeline for streaming or batch data | The work is data transformation written with Apache Beam |
| Dataproc | Managed Spark and Hadoop | Your code already uses those big-data tools |
The main trade-off is control versus convenience. Batch gives you more control over machines than Cloud Run jobs, and less operational work than running your own Kubernetes cluster.
12Design Patterns and Anti-patterns
Proven shapes to copy, and traps to avoid.
Good patterns
Fan-out and fan-in
Split the work across many tasks, then run one final job that gathers all the pieces into a single result.
Index-based sharding
Use the task number to choose which slice of data to process. Simple and collision-free.
Pipeline of jobs
Job A finishes, a Pub/Sub message fires, and job B starts. Each step stays small and replaceable.
Scheduled batch
Use a scheduler such as Cloud Scheduler or Workflows to submit a nightly job automatically.
Spot plus retry
Many small, repeatable tasks on cheap Spot VMs, protected by retry rules.
Container for everything
Package code and libraries in one image so every VM behaves the same way.
flowchart LR
SCH["Cloud Scheduler: nightly trigger"] --> WF["Workflows: orchestrator"]
WF --> J1["Batch Job A: process data in many tasks"]
J1 --> PS["Pub/Sub: job finished message"]
PS --> WF
WF --> J2["Batch Job B: merge results"]
J2 --> BQ["BigQuery: final tables"]
Anti-patterns
What people do
They create one giant task that processes everything on a single big VM.
Why it hurts
You lose parallelism, a single failure restarts everything, and Spot VMs become useless.
Do this instead
Split the work into many small tasks that each finish in minutes.
What people do
They save results only on the VM’s local disk.
Why it hurts
The VM is deleted at the end, and the results vanish with it.
Do this instead
Always write final output to Cloud Storage or another permanent place.
What people do
They use Batch for tiny requests that need an answer in under a second.
Why it hurts
Queue time and VM boot time make Batch far too slow for that purpose.
Do this instead
Use a service built for live traffic, such as Cloud Run or GKE services.
13Best Practices and Common Mistakes
A checklist you can follow on day one.
Best practices
- Test small first. Run a job with two or three tasks before you launch thousands.
- Right-size the machines. Watch CPU and memory in a test run, then choose the smallest type that keeps tasks comfortable.
- Use containers for repeatable environments, and pin the image to a specific version instead of a floating “latest” label.
- Keep tasks short and idempotent so retries and Spot VMs are harmless.
- Set time limits and retry counts on every task.
- Limit parallelism when your tasks call a database or an outside service that cannot handle a flood.
- Turn on Cloud Logging for every job.
- Use labels on jobs so your bill can be split by team or project.
- Use a dedicated service account with minimum permissions.
- Check quotas early for CPUs, GPUs, and IP addresses in your chosen region.
Common mistakes
Forgetting about quota
A team submits a job needing 2,000 CPUs while their regional quota is 500. The job waits in the queue for hours. Request quota increases before the big day.
Reading everything from the internet
Each task downloads the same large file from an outside website. This is slow, may break, and hammers the source. Copy it once to Cloud Storage first.
Tasks that depend on each other by accident
Task 5 quietly expects a file that task 4 creates, but there is no guarantee of order. Either make tasks truly independent or use a barrier or a separate job step.
No cleanup of output
Old results pile up in buckets for months. Use lifecycle rules in Cloud Storage to delete or archive them automatically.
Before submitting a huge job, estimate cost with a small trial. Multiply the cost per task by the number of tasks. A surprise bill is much worse than a quick test.
14Real-World Use Cases
Where batch computing shows up in real industries.
Media and visual effects rendering
A studio needs to turn thousands of film frames into finished images. Each frame is one task. Hundreds of VMs render in parallel overnight, and the studio releases them in the morning instead of owning a render farm that sits idle most of the year.
Genomics and life sciences
Researchers analyze DNA samples from thousands of patients. Each sample is processed independently by the same pipeline, which is a perfect fit for one task per sample.
Financial risk simulation
A bank runs millions of “what if” market scenarios each night to estimate risk. Each batch of scenarios is a task, and the final job combines the numbers into a morning report.
Machine learning
Teams use GPU machines to train or tune models, or to run predictions across a huge archive of images and documents. Batch gives GPUs only for the hours needed.
Engineering simulation
Car makers and aerospace teams run crash tests, airflow models, and structural stress tests on computers. Tightly coupled simulations use MPI and many hosts working together.
Large-scale data processing
An online store converts millions of product photos into several sizes, or reprocesses a whole year of log files, as one big parallel job.
Continuous integration at scale
A software company runs a huge test suite split across many machines so the full test run finishes in minutes instead of hours.
The common thread is clear: the work is large, can be divided, does not need an instant answer, and needs a lot of power for a short time.
15Frequently Asked Questions
16Summary and Key Takeaways
You started this guide with a bakery that needed 500 ovens for one night. You now know that Cloud Batch is the Google Cloud version of that idea: rent exactly the computing power you need, use it hard, and give it back.
Key Takeaways
- Batch is a managed service that queues, schedules, and runs big jobs on Compute Engine VMs, then removes the machines when finished.
- The model is simple: a job holds task groups, task groups hold tasks, and tasks run runnables such as scripts or containers.
- Each task knows its own number, so you can split data cleanly and run thousands of tasks in parallel.
- VMs are temporary, so always read input from and write results to permanent storage such as Cloud Storage.
- Use Spot VMs plus retries and idempotent tasks to save money without risking the result.
- Secure every job with a dedicated service account, least-privilege IAM roles, private networking, and Secret Manager.
- Always turn on logging, set time limits, check quotas early, and test small before going big.