The Simplest Way to Make Cosmosdb and Elasticsearch Work Like They Should
Teams pair cosmosdb and elasticsearch to combine fast document writes with deep text search. In production, a worker app reads database changes and writes them into search indexes. This setup splits your workload across two specialized engines. Your main app writes operational data into the document store with low latency. A sync worker then copies those records into the search cluster. Users can run complex text queries without blocking down core database writes. How data and keys flow betwe
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.
Teams pair cosmosdb and elasticsearch to combine fast document writes with deep text search. In production, a worker app reads database changes and writes them into search indexes.
This setup splits your workload across two specialized engines. Your main app writes operational data into the document store with low latency. A sync worker then copies those records into the search cluster. Users can run complex text queries without blocking down core database writes.
How data and keys flow between cosmosdb and elasticsearch
The link between cosmosdb and elasticsearch relies on a change feed and a sync worker. First, the primary app writes new or updated JSON records into the document store. The database records these updates in an ordered change feed log.
Next, a background worker reads that change feed on a set schedule. The worker logs in to the database using a read role or a master key. It pulls the changed records and transforms the fields to match the search schema.
Finally, the worker sends those transformed records to the search cluster in batches. The worker logs in to the search cluster using an API key or role. The search engine indexes the text fields so users can query them right away. Each system checks keys and roles at every step of this data flow.
Common failure modes and how to scope roles
Many teams take shortcuts when they set up the sync worker. They often use a root database key that has full write rights. They also give the worker a broad admin token for the search cluster. If an attacker steals the worker credentials, they can delete all production data.
Shared service accounts create another big blind spot for security teams. Engineers often use the same worker credentials to run manual test queries from laptops. When a bad query crashes a node, the logs only show the shared account. You cannot tell which person ran the command or read the records.
Elasticsearch Security + CosmosDB RBAC: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
Sensitive data exposure is also a major risk in this pipeline. The sync worker often copies every field from the source document into the search index. This moves emails, phone numbers, card numbers, and national IDs into a second system. Analysts who query the search cluster might see raw fields they do not need.
You can fix these issues with strict roles and tight network rules. Give the sync worker a read-only role on the source database. Create a dedicated write role on the search cluster that only touches one index. Drop or mask sensitive fields before the worker sends records to the search cluster.
Lock down the network path between the two systems as well. Run the sync worker inside a private network with strict firewall rules. Block public traffic to both the document store and the search cluster. Require every human engineer to log in with their own identity instead of shared keys.
Keeping schemas and audit logs in sync
Schema drift often breaks the pipeline between a document store and a search index. Developers can add new fields to JSON documents without warning the search team. When the sync worker sends those new fields, the search index might reject the batch. Teams should check field types before they deploy app changes to production.
You also need clear audit logs across both systems for security reviews. Native database logs and search logs use different formats and time zones. Collect logs from both engines in one central place and tag them by user identity. This helps your team trace every read and write across the whole data pipeline.
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
Why do teams run both databases instead of just one?
The document database handles fast single-item writes and global scaling very well. The search engine excels at full-text queries, fuzzy matching, and fast aggregations.
How do we stop sensitive data from leaking into search indexes?
Filter the document fields inside your sync worker before sending the batch. You can also use data masking to rewrite emails, phone numbers, and card numbers in results.
What permissions should the sync worker have?
Give the worker a read-only role on the source database change feed. On the search cluster, give it a role that can only write to target indexes.
Related guides
In this post