The Simplest Way to Make Google Pub/Sub and Spanner Work Like They Should
You pair google pub/sub and spanner to build fast, event-driven apps at scale. One tool moves messages between services, while the other stores relational data across regions. This setup lets your team process events and save records without losing data. When a user acts, the message queue catches the event right away. Worker services then read that message and write the update into the database. You can also stream database changes out to the message queue for downstream jobs. How google pub
Your coding agents run with your access.
Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.
You pair google pub/sub and spanner to build fast, event-driven apps at scale. One tool moves messages between services, while the other stores relational data across regions.
This setup lets your team process events and save records without losing data. When a user acts, the message queue catches the event right away. Worker services then read that message and write the update into the database. You can also stream database changes out to the message queue for downstream jobs.
How google pub/sub and spanner move data and check roles
Data flows between these two services in two main ways during production runs. In the first path, an app sends an event to a topic. A worker service pulls that message from a subscription and writes rows to the database. In the second path, database change streams capture row updates as they happen. A data job reads those updates and publishes new messages to a topic.
Cloud IAM controls how each service logs in and runs these tasks. Each worker app needs a dedicated service account with narrow roles. To read messages, the worker role needs permission to pull from the subscription. To write data, that same worker needs database user permissions on the specific database. When streaming changes out, the pipeline service account needs read permissions on the change stream. It also needs publish rights on the destination topic.
Good role design keeps these two paths separate at all times. A service that only reads messages should not hold rights to publish new ones. A worker that writes order rows should not hold admin rights on the database instance.
Common failure modes and how to scope access
Many teams hit trouble when they share one service account across many worker apps. If that shared account holds broad project roles, any single bug can harm all tables. Static service account keys stored on laptops or in code repos create huge risks. Anyone who finds a leaked key can read messages or change production rows.
Another big issue comes from human engineers who debug broken message flows. When a message fails to write to the database, an engineer must check the state. Teams often grant engineers broad database roles so they can fix the issue fast. Those engineers then run manual queries and updates that leave weak audit trails. Over time, temporary debug roles turn into permanent standing permissions across the whole team.
End-to-End Encryption: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
You can fix service-to-service risks by using workload identity instead of static keys. Give each worker app its own identity tied to a single subscription and database role. Restrict network paths by running both services inside a private virtual network perimeter. This rule stops data from leaving your cloud boundary even if a key leaks.
Fixing human debug workflows and audit gaps
Machine roles are only half of the security puzzle for event-driven systems. Engineers still need a safe way to inspect dead-letter queues and check database rows. Standard cloud logs show that a user connected, but they miss query details unless configured. Turning on full database audit logs for every read can raise storage costs fast.
Teams should separate machine pipeline roles from human debug permissions right from the start. Never let an engineer log in using a worker service account to test a fix. Instead, require engineers to use their own identity when they query tables or inspect topics. Set up data masking to rewrite sensitive fields like emails, phone numbers, and card numbers. When masking rewrites matched fields in results, engineers can fix bugs without exposing customer data.
You should also require peer reviews before anyone runs write queries on production tables. Timed role grants ensure that debug permissions expire as soon as the ticket closes. This habit keeps your production tables safe while your message queues run at full speed.
Add runtime guardrails and masking beside your databases and APIs
Login rules, role grants, and CI checks govern who connects to your stack. Yet any allowed script, engineer, or AI agent still runs commands with the full scope of its role. For workloads on Postgres, MySQL, SQL Server, MongoDB, ClickHouse, Kubernetes, SSH, or internal HTTP services, hoop.dev sits beside the service as an open-source proxy, a sidecar for your database or API.
Before a query runs, hoop.dev guardrails check the statement against your rules. An AI Analyzer you configure can score intent or hold risky calls for human review. Next, hoop.dev data masking rewrites sensitive fields in responses, and each verdict is logged per listener. Explore the MIT-licensed project on GitHub.
Frequently asked questions
How do you prevent duplicate writes when Pub/Sub retries a message?
Messages can arrive more than once when networks lag or workers restart. Store the unique message ID or a business key inside your database table. Make your worker check that key inside a transaction before it writes the new row.
Should workers use static service account keys to reach Spanner?
No, you should avoid static keys because they are hard to rotate and easy to leak. Run your workers on managed compute platforms that attach short-lived identities to each container. This setup handles token rotation for you without storing secret files on disk.
How can teams protect PII when debugging failed Spanner transactions?
Route human database sessions through a control point that checks queries before they run. Apply masking rules that rewrite emails, national IDs, and card numbers in query results. This lets engineers check row states and fix dead-letter messages safely.
Related guides
In this post