Sub topic 1
When Database Queries Are Not Enough
Most systems start with search done in the database. A LIKE query on a product name works fine on a small table. This chapter helps you decide when that stops being true and a dedicated search service is worth its cost.
Prerequisites
This chapter assumes you know:
- Basic relational database and SQL knowledge, including indexes.
- How services talk to each other over REST APIs.
- What caching and message queues do.
- The basic idea of horizontal scaling and replication.
What a database query does well
A relational database is built for exact lookups and structured filters. A B-tree index finds WHERE id = 42 or WHERE price <= 100 quickly. It also gives you transactions, so a read sees committed data.
Text search is a different job. A user typing "red running shoes" expects results that match the words, in any order, ranked by how well they match.
Where database queries break down
Consider this query:
SELECT id, name
FROM products
WHERE name LIKE '%running shoes%';It has four problems:
- Speed. A pattern with a leading
%cannot use a B-tree index, so the database scans every row. Cost grows with table size. - Matching. It matches only that exact phrase. "Shoes for running" and "Running shoe" do not match.
- Ranking. Rows come back unordered. The database has no notion of a better match.
- Typos and variants. "runing" finds nothing, and plurals and accents are not handled.
Database full-text features (such as PostgreSQL tsvector or MySQL FULLTEXT) fix several of these. They are often enough, and Chapter 5 compares them with dedicated engines.
Signals that you need a search service
Look for several of these together, not one alone:
| Signal | Why a database struggles |
|---|---|
| Free-text queries over large data | Scans are slow, and ranking is weak or missing |
| Relevance ranking matters | You need scoring such as BM25, not just filtering |
| Typo tolerance, synonyms, stemming | These need text analysis at index time and query time |
| Facets and counts (brand, price range) | Many aggregations on every request load the primary database |
| Search load competes with transactions | Heavy reads slow down writes and checkout |
| Many fields searched at once | Multi-column matching and boosting get awkward in SQL |
What you pay for it
A search service is a second copy of your data, so it adds costs:
- Sync. You must move changes from the source of truth into the index.
- Staleness. The index lags behind the database. This is eventual consistency.
- Operations. You now run, monitor, scale, and upgrade another cluster.
- Failure modes. The search service can be down while the database is healthy.
The database stays the source of truth. The search index is a derived, rebuildable view.
Decision flow
Summary
Use database queries for exact matches and small data. Consider database full-text search for modest text needs. Add a dedicated search service when you need ranking, typo tolerance, facets, or scale, and when you can accept the cost of sync and eventual consistency.
Exercise
A team's admin page filters 20,000 orders by status and date range. A manager asks for a search service "to make it faster". What do you recommend, and why?
Show answer
Keep the database. The queries are exact filters on structured columns, which B-tree indexes on status and date handle well, and 20,000 rows is small. A search service would add sync, staleness, and operations cost without solving a real limitation. Check the query plan and add a composite index first.
Exercise
Name three costs a dedicated search service adds to a system design.
Show answer
Any three of: keeping the index in sync with the database, eventual consistency between them, running and scaling another cluster, and a new failure mode where search is down while the database is up.