Google Cloud Run deploys containers that scale to zero — identical to AWS Lambda in the stateless execution model. UUID generation is entirely application-layer: there is no Cloud Run-specific UUID API. The right library choice depends on runtime. See UUID in JavaScript and UUID in Python for the underlying patterns.

Node.js Cloud Run service

// app.ts — Express on Cloud Run
import express from 'express';
import { v7 as uuidv7 } from 'uuid';
import { Firestore } from '@google-cloud/firestore';

const app = express();
const db  = new Firestore();

app.post('/orders', async (req, res) => {
  const orderId = uuidv7();  // time-sortable, globally unique

  await db.collection('orders').doc(orderId).set({
    status:    req.body.status,
    createdAt: Date.now(),
  });

  res.status(201).json({ id: orderId });
});

app.listen(Number(process.env.PORT) || 8080);

Using UUID v7 as the Firestore document ID means documents sort chronologically when queried with orderBy(FieldPath.documentId()) — no secondary createdAt index needed for time-range pagination. See UUID v4 vs v7.

Python Cloud Run service

# main.py — Flask on Cloud Run
import uuid
from flask import Flask, request, jsonify
from google.cloud import firestore

app   = Flask(__name__)
db    = firestore.Client()

@app.post('/orders')
def create_order():
    order_id = str(uuid.uuid7())  # Python 3.13+ stdlib

    db.collection('orders').document(order_id).set({
        'status':     request.json['status'],
        'created_at': firestore.SERVER_TIMESTAMP,
    })

    return jsonify({'id': order_id}), 201

Python 3.13’s uuid.uuid7() is available in Cloud Run’s Python 3.13 runtime. For earlier runtimes, install the uuid6 backport. See Python 3.13 uuid.uuid7().

Cloud Spanner — UUID v7 primary keys

Cloud Spanner recommends against sequential integer primary keys (they cause hotspot writes on a single split). UUID v7 distributes writes better than pure sequential integers while still being time-ordered:

-- Cloud Spanner DDL
CREATE TABLE orders (
  order_id  STRING(36)  NOT NULL,  -- UUID v7 as string
  status    STRING(32)  NOT NULL,
  created_at TIMESTAMP  NOT NULL   OPTIONS (allow_commit_timestamp=true),
) PRIMARY KEY (order_id);
import { Spanner } from '@google-cloud/spanner';
import { v7 as uuidv7 } from 'uuid';

const spanner = new Spanner();
const db = spanner.instance('my-instance').database('my-db');

await db.table('orders').insert({
  order_id:   uuidv7(),
  status:     'pending',
  created_at: Spanner.commitTimestamp(),
});

Cloud Spanner splits data by key range — UUID v7’s random suffix within each millisecond prevents all writes from hitting the same split. See UUID in Databases for the equivalent PostgreSQL B-tree analysis.

Cloud Run with Cloud SQL (PostgreSQL 18)

import { Pool } from 'pg';

const pool = new Pool({ connectionString: process.env.DATABASE_URL });

app.post('/orders', async (req, res) => {
  const { rows } = await pool.query(
    // uuidv7() generated by PostgreSQL 18 — no app-layer generation needed
    'INSERT INTO orders (status) VALUES ($1) RETURNING id',
    [req.body.status]
  );
  res.status(201).json({ id: rows[0].id });
});

When using Cloud SQL with PostgreSQL 18, delegate UUID v7 generation to the database via DEFAULT uuidv7() — the application does not need the uuid package at all. See PostgreSQL 18 Ships Native UUID v7.

Further reading

External references

Frequently asked questions

Does crypto.randomUUID() work in Google Cloud Run?

Yes. Cloud Run containers run standard Linux environments — Node.js 18+ has crypto.randomUUID() built in, Python 3.13 has uuid.uuid7() in stdlib, and Go has uuid.New7() via google/uuid. No GCP-specific configuration is needed. See crypto.randomUUID() runtime support for the full compatibility table.

Should I use UUID v7 or Firestore's auto-generated IDs in Cloud Run?

Firestore's db.collection().doc() generates a 20-character base62 string, not a UUID. For portability and compatibility with PostgreSQL, MySQL, or Cloud Spanner, generate UUID v7 at the application layer and use it as the Firestore document ID. UUID v7 also enables efficient time-range queries on Firestore's document ID field because lexicographic order matches creation order. See UUID in Databases.