nest.codes blog
Day 4 · · 3 min read

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:

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

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.

#azure#cosmos-db#azure-functions#cloudflare-r2#speech-to-text
All posts in this series
  1. Prologue · Azure Credits via NVIDIA Inception: Why We Moved
  2. Day 1 · Azure PostgreSQL Migration: Quota Isn't SKU Availability
  3. Day 2 · Azure Container Apps Serverless GPU: Real T4 ASR Cost
  4. Day 3 · Cosmos DB 429 at 1% Average RU: Retry, Don't Scale
  5. Day 4 · Cosmos DB for Transcript JSON: One Container, Not Two
  6. Day 5 · Container Apps job cold start: 8 min to 1.8 with Blob
  7. Days 6–7 · Azure service usage audit: proving each one does real work