The Simplest Way to Make Azure Cosmosdb and Nats Work Like They Should

Published
Length
3 min read

Luis Felipe Dias

All posts

Running azure cosmosdb and nats together pairs fast message passing with global data storage. This setup lets microservices publish events and save state with very low latency. NATS moves tiny messages between your apps in real time. Azure Cosmos DB stores those records across many cloud regions at once. A worker service sits between the two tools to link them. The worker reads a message from a NATS subject and writes to the database. Another worker can read database change feeds and send updat

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.

Running azure cosmosdb and nats together pairs fast message passing with global data storage. This setup lets microservices publish events and save state with very low latency.

NATS moves tiny messages between your apps in real time. Azure Cosmos DB stores those records across many cloud regions at once. A worker service sits between the two tools to link them. The worker reads a message from a NATS subject and writes to the database. Another worker can read database change feeds and send updates back to NATS. This pattern keeps your apps fast and loose.

How data and permissions flow in azure cosmosdb and nats

Neither tool talks to the other on its own. You must run a bridge app or worker service between them. This worker needs two separate sets of login keys to run. First, it logs in to NATS using a token, key pair, or certificate. Second, it logs in to the database using a master key or cloud role.

Your permissions must match the exact job each worker does. In NATS, you set up rules for which subjects a worker can read. You also limit which subjects the worker can write to. In the cloud database, you assign role rules to the worker identity. Give a reader worker permission to read items from one container only. Give a writer worker permission to write items to its target container. Never give one worker full admin rights to both systems.

Data flows through this setup in two main ways. For ingestion, client apps send fast events to a NATS stream. The worker pulls a batch of events and writes them to the database. For outbound events, the worker watches the database change feed for new records. When a record changes, the worker sends a message to a NATS subject. Other apps listen to that subject and react right away.

Common failure modes and how to scope access

Teams often take shortcuts when they set up these two tools. The most common mistake is using one shared master key for all workers. If an attacker steals that key, they can read or delete all data. Another mistake is using broad NATS wildcard rules for every app. A broken app might send bad data to the wrong subject. Teams also let engineers log in to production databases from laptops with shared keys.

Azure RBAC + CosmosDB RBAC: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Unlogged database queries create another big risk for your team. When five apps share one database role, the logs show one user name. You cannot tell which app ran a slow or harmful query. The same problem happens when developers check live data during an outage. If they use a shared admin key, you lose the audit trail. You also risk leaking sensitive fields like emails, phone numbers, or card numbers.

You can fix these issues with strict role and network rules. Place your workers and message servers inside a private cloud network. Block public traffic to your database and use private network links instead. Use managed cloud identities for your workers so you never store static database keys. Set up separate NATS accounts for each team or service group. Give each worker its own NATS user key with narrow subject rules.

Protecting sensitive data across streams and storage

Event streams and databases often hold user records that need extra care. A message payload might carry national IDs, emails, or card numbers. When engineers inspect live queues or query tables, raw data can spill into logs. You should set up data masking rules to rewrite matched fields in results. This step replaces sensitive strings with safe masks during read queries. You should also log every human query with a clear user identity.

Govern statements and responses across databases and APIs

Good config and scoped roles are needed setup. They do not check single queries in flight or mask sensitive fields in returned rows. Across Postgres, MySQL, SQL Server, MongoDB, ClickHouse, Kubernetes APIs, SSH, and internal HTTP endpoints, hoop.dev runs beside each supported service as an open-source proxy, a sidecar for your database or API.

First, hoop.dev guardrails block banned queries before they run. Then hoop.dev data masking rewrites matched sensitive values in responses in memory. The audit trail records every statement and verdict per resource. Furthermore, hoop.dev holds SOC 2 Type II and is open source on GitHub.

Frequently asked questions

Can NATS write data directly into Azure Cosmos DB?

No, NATS does not have a built-in connector for this database. You must run a worker app that reads NATS messages and writes them out.

How should worker apps log in to both tools safely?

Use cloud managed identities for the database and scoped user keys for NATS. Avoid putting shared master keys or broad tokens inside your app files.

How do we stop raw user data from leaking during checks?

Use dynamic data masking to rewrite emails, phone numbers, and card numbers in results. Also require each engineer to log in with their own personal identity.

The query runs. The emails stay hidden.