It worked great on my desktop, but did it run in production?

Published: 2025-09-17

URL: https://wonderingaboutai.substack.com/p/i-created-a-vector-database-with


Disclosure: This article was written by me, an under-caffeinated human, with light editing and a technical review from Claude. I was walking through a WalMart Supercenter with my kids the other day to pick up school supplies. We had a short list, and the store offered an almost unlimited selection.

It was the perfect recipe for decision paralysis and overthinking. My daughter spent 15 minutes trying to pick just the right notebook design while my son got distracted by a rubber chicken that someone left beside an eraser display. Overall, the experience was stressful, overwhelming, and a lot longer than it had to be.

It also reminded me a bit of what it's like to find the right content on Substack. Tons of great newsletters are being launched and updated daily. But, aside from the bestseller lists and a few article recommendations from the Substack app, there is no really good way to search the entire ecosystem for the content you'd like to see and potentially subscribe to.

Why keyword search is kind of meh I tried to address this somewhat in my newsletter digest app by allowing users to search the web for Substack newsletters based on keyword match. But users reported the results were iffy, surfacing irrelevant content and failing to surface what they really wanted. Honestly, it wasn't the best experience.

This is because traditional keyword search treats words in isolation rather than understanding the context and meaning of entire phrases. For example, if you search for “AI agent,” a keyword search might return results about real estate agents who happen to mention AI in passing, or articles about FBI agents that discuss artificial intelligence briefly. Trying semantic search with machine learning And then it occurred to me…isn’t contextually relevant semantic search what machine learning transformers were built for?

I'd learned about them last year when I took Google’s Machine Learning Crash Course, and I’d been meaning to try them out with a vector database filled with one of my clients’ content libraries. I decided that this was the opportunity I’d been waiting for. I decided I would learn about semantic search by adding a vector database for my app and attempting to run it in my production environment.

What are transformers? While my son knows Transformers as action figures, they are also a type of neural network, introduced in 2017, that revolutionized natural language processing. Unlike traditional models that process text sequentially (word by word), transformers can analyze all words in a sentence simultaneously, understanding how each word relates to every other word through something called “attention mechanisms.” This allows them to capture context and meaning in a way that previous approaches couldn't.

When you feed text into a transformer model, it converts that text into a numerical representation, known as a vector, that captures not just the words themselves but their semantic meaning. Similar concepts end up with similar vector representations, even if they use completely different words.

This is why a transformer can understand that “car,” “automobile,” and “vehicle” are related concepts, or that “running a company” and “managing a business” mean essentially the same thing. How is a vector database different from a relational database? Until AI came along, I thought all databases were relational.

This kind of database stores data in tables with rows and columns. You can query it with SQL to find records that contain the exact word or phrase you’re searching for and also meet other requirements. If you want to find all articles about “machine learning,” you'd search for that exact phrase in specific fields.

The database either finds a match or it doesn’t. There's no concept of “close enough” or “semantically similar.” Vector databases, on the other hand, store data as high-dimensional vectors (lists of numbers) and are specifically designed for similarity searches. Instead of looking for exact matches, they find vectors that are geometrically close to each other in this multi-dimensional space.

When you search, the database converts your query into a vector and finds the stored vectors nearest to it. This means you can search for “building AI systems” and get results about “machine learning engineering” or “developing neural networks” because their vector representations are similar in meaning, even though the exact words differ. Mapping out the feature My goal was to build a vector database of Substack newsletters and articles that would allow users to search for content using contextually aware statements rather than keywords.

In search results, I decided to rank semantically surfaced newsletters and articles only by relevance, allowing smaller newsletters’ content to surface as well as bestsellers. For digests, which are intended to surface the highest-quality content, I opted to combine relevance (70%) with other quality signals (30%) like likes, comments, and length to rank articles.

In theory, this feature should let you find meaningful Substack native content on virtually any topic. But for its initial, proof-of-concept deployment, I planned to limit the initial database to around one or two thousand newsletters and articles from the past few months. Choosing the technology Because my app is currently running on Postgres, it was an easy decision to add pgvector, a PostgreSQL extension that supports vector similarity search capabilities. After some light research and conversations with Claude, I decided to use a popular (and free) sentence transformer model from Hugging Face (all-MiniLM-L6-v2) to handle the semantic search logic.

Hugging Face is a hub for open-source machine learning models, and the models you find there can be downloaded and used without any licensing fees, API costs, or usage restrictions for most applications. Sentence transformers are specialized models designed specifically to convert sentences or short paragraphs into dense vector representations. Unlike general-purpose language models like GPT that generate text, sentence transformers are optimized for only one job: creating embeddings that capture semantic meaning.

They're typically much smaller than generative models, ranging from 20MB to a few hundred MB, making them practical to run on modest hardware. I was also careful to keep in mind the capabilities and limitations of my production hosting provider, which is currently Heroku.

For example, I was initially concerned about the stateless nature of Heroku dynos, but pgvector addresses that by storing everything in the database. I also figured I would set up the initial vector database locally, and then copy it to Heroku to start.

And I assumed that sentence transformers, since they could easily fit on my hard drive, would be no problem to install and run in production. Building the database The first step was gathering a representative sample of Substack newsletters to vectorize. I started with my own subscriptions, which now include more than 700 newsletters mostly on AI, software development, and writing topics. I then grew it by scraping Substack's bestselling and rising lists until I reached approximately 2,000 newsletters.

While I still need to analyze the database for category coverage (e.g., I almost certainly need to add a lot more non-technical content), I figured 2,000 would be a solid starting point to test whether the semantic search approach would actually work.

The data collection process involved using Substack's undocumented API to grab JSON data for newsletters and their recent articles. This API, while not officially supported, returns structured data including newsletter descriptions, author information, and article metadata, exactly what I needed to build meaningful vector representations.

For each newsletter, I pulled the title, description, and the last 10-15 articles. Then I used the MiniLM-L6-v2 model to create 384-dimensional vector embeddings. This means it converts each piece of text into a list of 384 numbers that represent its semantic meaning.

These dimensions don't correspond to anything human-interpretable; they're abstract features the model learned during training that capture semantic relationships between concepts. Getting it to work locally With the vector database populated, I updated my search code to use semantic search powered by sentence transformers instead of keyword matching.

Here's how it works: When a user enters a search query like “newsletters about climate change and technology,” the system first converts this query into a 384-dimensional vector using the same sentence transformer model. Then it queries the pgvector database to find the vectors with the highest cosine similarity to the query vector. Cosine similarity measures how similar two vectors are regardless of their magnitude, which is great for comparing semantic meanings. Testing locally, it was immediately clear that this was a major upgrade from keyword matching.

Searching for “AI safety” returned not just newsletters with those exact words, but also ones discussing systems design, risk, and other related topics. The system understood conceptual relationships that keyword search would have missed entirely. I was also happy to see that response times were good and the relevance scoring surfaced some hidden gems. Often, smaller newsletters with highly relevant content scored better than popular newsletters that only tangentially mentioned the search topic.

A simple plan After more testing, I decided the app was ready for testing in production. Because the introduction of a new database is a big change, I scheduled the migration for Monday, rather than running it Friday, and risk spending the weekend fixing bugs.

But I was cautiously optimistic. My plan was appealingly simple and straightforward: backup the production database, install the sentence transformers on Heroku, copy over the vector database from the local environment, embeddings and all, and deploy.

What could possibly go wrong? Attack of the giant slug The first roadblock I hit was that sentence transformers, while not using too much disk space on their own, have lots of dependencies that do indeed absorb a ton of disk space.

The PyTorch library alone is several hundred megabytes, and combined with the transformer models and other dependencies, I was way over Heroku's 500MB limit. (Heroku’s storage limit for your code, dependencies, complied assets, Python is known as a ‘slug,’ and no, I have no idea why.) I tried various approaches, such as using CPU-only versions of PyTorch, attempting to lazy-load models, even trying to split the app into microservices, but nothing fit within Heroku's constraints. Big data, big problems The second issue emerged when I tried to migrate the vector database from local to production.

My initial approach of exporting all the embeddings as a JSON file and then dropping them into the newly migrated database did not work. The file was large (137 MB), and so it could be loaded into memory without causing my worker dynos to crash and restart. I also tried splicing the file and loading it piece by piece, an approach that generated lots of JSON parsing errors.

These failed attempts also added some malformed, partial records to the database, which caused it to throw errors and crash the app. Fortunately, I’d backed up the production database, so I dropped the old database, re-ran the migrations, and restored all the user data. While Claude offered to build a tool that could chunk the file and feed it into memory but by bit for processing, I said no.

That sounded like a lot of work, and I’d chosen to copy over the development database only because I misguidedly thought it would be easy. Instead, I had it build a script to regenerate my vector database based on a CSV of newsletter URLs.

The big pivot: moving to OpenAI After battling with dependencies and migration issues for two days, I realized I was solving the wrong problem. The goal was semantic search, not specifically to use sentence transformers. I asked Claude about alternatives to sentence transformers, and, after assuring it that I could afford a few cents for API calls, it told me about OpenAI's embedding API OpenAI's text-embedding-ada-002 model creates 1,536-dimensional embeddings, four times more dimensions than my local sentence transformer, potentially capturing even more semantic nuance.

At $0.0001 per 1,000 tokens (roughly 750 words), embedding my entire database would cost less than a coffee. And the best part was that I could remove sentence transformers and its bulky dependencies from my slug. Of course, it also meant that I would have to update a whole bunch of code, including semantic search and article categorization functions. Instead of loading a model locally, they now send text to OpenAI's API and receive back the relevant vector embeddings. I was also glad to see that, in local testing, OpenAI’s API actually performed better than sentence transformers, probably because the embeddings cover more dimensional space.

As with any code change that impacts multiple functions, the switch away from sentence transformers introduced some bugs. But, after a few hours of tinkering, I was able to get it working in my local environment. Huzzah! I immediately committed the code and pushed it to production. Then I started the batch-processing script to regenerate the embeddings and went to bed. Testing in production The next morning, I checked the database and my OpenAI API account balance. I found 2,100+ newsletters and 32,000+ articles now had embeddings, and all this had cost me a total of 36 cents.

I immediately logged onto the app and started testing. I confirmed that all my newsletter data seemed to be intact, tested adding and deleting newsletters, and ran several types of digest, including a “no AI” digest with more than 400 records to make sure the app was ready to support the She Writes AI Community. What’s in the current release With the vector database and a few additional enhancements, the app now supports: Substack newsletter and article discovery through semantic search Topical digests powered by semantic search Engagement scoring that includes likes as well as comments More accurate engagement data pulled from JSON via Substack’s undocumented API Cleaner digest formatting I also improved validation for newsletter imports, making it much less likely you import a user profile or article instead of a newsletter by mistake.

A few lessons for next time Looking back on this adventure in vector databases and production deployment, several lessons stand out: Don't assume local equals production. Just because something runs beautifully on your development machine doesn't mean it will even start in production. I’d never even thought about Heroku's 500MB slug limit until I tried to install PyTorch and its dependencies.

Next time, I'll investigate production constraints before making any major platform decisions. Data migration is sometimes harder than data generation. I spent hours trying to cleverly migrate my local vector database to production, dealing with JSON parsing errors, memory crashes, and corrupted records.

In the end, regenerating everything from scratch via an overnight batch process was the best option. Sometimes the "inefficient" solution is actually the most efficient. API costs can be surprisingly reasonable. I initially resisted using OpenAI's embedding API because I assumed it would be expensive at scale.

But embedding 32,000 articles cost only 36 cents. For a side project, the simplicity of an API often outweighs the "free" option that requires complex infrastructure. Semantic search really is so much better for content discovery.

The difference between keyword matching and semantic search is huge. The Hugging Face ecosystem is amazing, but know when to use it. Having free access to state-of-the-art models is incredible for prototyping and learning.

But for production deployment with infrastructure constraints, sometimes a paid API is the more practical choice. The beauty is that you can prototype with free models, then make an informed decision about production. Always back up your production database before you start making changes. You will feel less panicked and, if things go badly, you can always rollback your app and try again later.

The biggest lesson? Sometimes the journey from “it works on my machine” to “it works in production” requires throwing out your original approach entirely. And that's okay. The goal isn't to stick to the plan; it's to ship something that works.

And that rubber chicken my son found? We bought it. Sometimes the best discoveries are the ones you weren't searching for. p.s. If you’re curious about the app, you can see it at stackdigest.io p.p.s. As always, a huge 🙏 THANK YOU to the following very kind and patient folks whose feedback and insights have helped me shape and evolve the StackDigest project: