Cosmos DB for Transcript JSON: One Container, Not Two
If your subtitle files live only in object storage, how do you look up what was said in one episode without downloading and parsing the file every time? You don't. Today we kept the files in Cloudflare R2 and added one JSON document per transcript to Azure Cosmos DB. We didn't create a new container for it. We split the data by a type field inside a container that was already running.
Why weren't files enough?
Our speech transcription pipeline writes SRT files from Container Apps and MAI transcription into Azure Blob. Today we also copied those files to R2, because of the rule from Day 0: data should be readable from more than one cloud.
Once the files were in R2, we could see the lookup problem. Fetching a whole transcript is easy if you know the path. But answering "what was said around minute three of this episode?" means downloading and parsing the file every time. So we decided to keep a queryable copy as documents.
Three options, and why we reused a container
We had three options:
- A jsonb table in PostgreSQL, which we already run.
- A dedicated Cosmos DB container. It's clean, but it carries its own minimum of 400 RU/s (Cosmos DB's throughput unit). That's roughly $23 more per month.
- Our existing Cosmos container, which the job queue already uses at a fixed 600 RU/s, with a
qfield to tell the two kinds of data apart.
We judged them on added fixed cost, per-transcript lookup, and how much existing code we could reuse. We picked the third. The queue code already writes to this container, so we could keep the same client and permissions. A document looks like this:
{
"q": "sub",
"w": "tv:<id>:<season>:<episode>",
"source": "<asr-model>",
"r2": "<r2-key>",
"n": 180,
"lines": [[0.0, 2.4, "first line"], [2.4, 5.1, "second line"]]
}
A lookup is one query: WHERE c.q='sub' AND c.w='...'. Long-form recording transcripts, which are a different kind of data, go into the same container under a different q value. Honestly, PostgreSQL jsonb would probably have worked just as well. We chose Cosmos because the path was already built, not because it performs better.
What we had to handle in return
First, indexing. By default, Cosmos DB indexes every property. If it indexed the whole lines array, every write would cost a lot more RU. So we added the lookup key w to the index and kept everything else excluded:
{
"includedPaths": [{ "path": "/q/?" }, { "path": "/w/?" }],
"excludedPaths": [{ "path": "/*" }]
}
In our setup, the data-plane key couldn't change the indexing policy. We made the change through the management plane with the az CLI.
Second, the 2 MB document limit. A very long transcript can go over it. For those, we drop the lines and store only the R2 key.
Third, 429s. The queue and the transcripts now share the same RU budget, so bulk writes can slow the queue down. For the backfill we used two concurrent writers and backed off whenever we got a 429. That's the same pattern as in Day 3. The backfill took about 1 hour 20 minutes, with zero failures.
New transcripts are handled by an Azure Functions timer. Every 2 minutes it scans Blob for files that haven't been copied yet. It uploads each one to R2, records the URL in our metadata database, and writes the Cosmos document. We didn't change a single line of the transcription code in Container Apps. If a Cosmos write fails, the file is already in R2, so we can backfill that document later.
One feeling from today: chaining transcription, storage, and indexing with one timer is really convenient. But I can't see it running every 2 minutes, and that makes me a little nervous. So I added a line for this step to the daily report email.
Also today
- We queried Front Door request counts in a single one-day bucket and got 1 request. Counting again by hour showed thousands per day. Check the aggregation grain before you trust a metric.
- Functions Flex Consumption bills for on-time × instance size. A schedule override meant for a single day stayed in place, and that day's execution time came out about 3× normal. We deleted the expired setting.
- We tested transcript proofreading with a GPT model on Azure OpenAI. It cost about $0.11 per hour of audio. Our transcription rules require numbers to be written out in Korean, so we had to spell that rule out in the prompt.
Here's what we don't know yet. On a heavy transcription day, will the queue sharing the same 600 RU/s fall noticeably behind? If it does, database-level shared throughput might be a better answer than a second container. We haven't measured that yet.