bigonotes
System DesignDesign Problems

Local Business Search (like Yelp)

A local business search and review site run through the Interview Approach: search by name, location and category, view a business, and rate it.

9 min readLast updated

A local business search site lets people find places near them, like a coffee shop or a plumber, open a business page, and leave a star rating with a short review. The hard parts are search that understands text and location at the same time, keeping one review per user, and keeping each business's average rating correct when many reviews arrive at once. This page runs it through the Interview Approach, one step at a time.

1. Functional requirements#

Three actions make the product:

  1. Users can search for businesses by name, location and category.
  2. Users can view a business: its name, description, photos and reviews.
  3. Users can leave a review: a rating from 1 to 5 stars plus text. Each user can leave only one review per business.

Out of scope: business owners creating or editing listings, editing or deleting reviews, review moderation, and personalized recommendations.

2. Non-functional requirements#

These three qualities shape the design:

Quality Requirement
Scale 100 million users, 10 million daily active users
Availability over consistency Search and business pages keep working when parts fail. A new review may take a few seconds to show up in search results
Low latency Search, view and review writes each complete in under 200 ms at p99

A quick estimate shows where the load is:

  • Reads: if each daily user runs about 5 searches or page views, that's 50 million a day ≈ 600 per second, around 3,000 at peak.
  • Writes: if 1 in 20 daily users writes a review, that's 500,000 a day ≈ 6 per second. Reads outnumber writes about 100 to 1.
  • Data: say 10 million businesses with 100 reviews each, about 1 billion reviews. At about 1 KB per review, that's around 1 TB. Photos are much larger, so they live outside the database.

The ratio decides the shape: make search and business pages fast, and keep review writes correct rather than fast.

3. Actors#

Actor What they do Access
Visitor Searches for businesses and views business pages Public, no sign-in
Reviewer Everything a visitor does, plus leaves one review per business Signed in, rate-limited

Visitors drive the read path. Reviewers add the small, careful write path.

4. API routing#

Actor Endpoint Response
Visitor GET /v1/businesses?query={text}&location={lat,long}&category={category} 200 with a list of businesses
Visitor GET /v1/businesses/{businessId} 200 with the business and its reviews
Reviewer POST /v1/businesses/{businessId}/reviews 201 with the created review

The view response carries the business fields and a page of reviews:

{
  "businessId": "string",
  "name": "string",
  "description": "string",
  "media": [ { "url": "string" } ],
  "avgRating": "float",
  "reviews": [ { "rating": "int (1-5)", "text": "string" } ]
}

The review request carries only the rating and the text:

{
  "rating": "int (1-5)",
  "text": "string"
}

The user ID comes from the auth token, never from the body, so nobody can post a review as someone else. Return 201 Created rather than a plain 200, since the call creates a resource. Reviews on the view page should be paginated, because a popular business can have thousands.

Every request passes through an API gateway, which handles authentication, authorization and rate limiting. It routes search and view calls to the business service, and review calls to the review service.

5. High-level design#

Take one functional requirement at a time.

Search (requirement 1). The client sends a search to the gateway, which forwards it to the business service. The business service queries the Postgres database for relevant businesses, filtering by category and location and matching the name. Indexes on name, category and location keep this workable at first. The business table holds:

Column Meaning
business_id (primary key) Unique ID
name Business name
category Category, such as restaurant or plumber
location Latitude and longitude
description Free text
media List of photo URLs
avg_rating Average star rating, kept up to date by the review service

View a business (requirement 2). The business service reads the business row by business_id, plus the first page of its reviews. Photos aren't stored in the database. They live in blob storage (S3), and the row only keeps their URLs. The service returns the URLs, and the client fetches the actual media files from S3.

Leave a review (requirement 3). The gateway sends the review to the review service, which writes it to a reviews table in the same database and updates the business's average rating:

Column Meaning
business_id The business being reviewed
user_id The reviewer, from the auth token
rating 1 to 5 stars
text Review text
created_at When it was written

The review service also reads reviews back for the business page. To update the average, it does two things: read the current average, then compute the new one and store it in the business row. Step 6 fixes the race hidden in those two steps.

6. Address the non-functional requirements#

The basic design works. Now check each quality, plus the two rules in the functional requirements, against it.

Search can filter on category, name or location. A normal B-tree index on name finds exact names and prefixes, but not "pizza" inside "Joe's Pizza Kitchen", and it doesn't rank results by relevance. Text search needs an inverted index: a map from each word to the businesses that contain it, so a query looks up its words and merges the lists.

Add Elasticsearch for this. It builds inverted indexes for text, ranks matches, and also supports geo queries, such as "within 5 km of this point", in the same request. The business service now sends searches to Elasticsearch and still reads full business pages from Postgres.

Keep Postgres as the source of truth and index asynchronously with change data capture (CDC). CDC reads Postgres's change log and an indexing worker applies every insert or update to Elasticsearch, so whatever changes in the database is reflected in search without the services writing to two stores.

Option Trade-off
Postgres indexes only (or PostGIS plus full-text search) One store, no sync. Text ranking and combined text and geo queries get slow and awkward at scale
Elasticsearch fed by CDC Fast text and geo search. One more system to run, and search lags the database by seconds
Service writes to both stores No CDC pipeline, but a crash between the two writes leaves them out of sync

Correct average rating under concurrent reviews#

Two reviews for the same business can arrive at once. Both read an average of 4.0 from 10 reviews, both compute a new value, and the second write overwrites the first. One review vanishes from the average.

Use optimistic concurrency control. Store review_count and a version number on the business row. The review service reads the row, computes the new average, and writes it only if the version hasn't changed:

UPDATE businesses
SET avg_rating = :new_avg,
    review_count = review_count + 1,
    version = version + 1
WHERE business_id = :business_id
  AND version = :version_read;

If no row was updated, someone else got there first, so re-read and retry. No lock is held while the service computes, so each writer proceeds optimistically and only the loser of a race retries. Retries are rare because one business rarely gets two reviews in the same instant. Insert the review and update the average in one transaction, so they never disagree.

A simpler alternative skips the read entirely: store rating_sum and review_count, and run SET rating_sum = rating_sum + :rating, review_count = review_count + 1. The database applies each increment atomically, and the average is rating_sum / review_count at read time. A distributed lock also works, but it's heavy for a counter.

One review per user per business#

Don't rely on the application checking "has this user already reviewed?" first. Two requests from the same user can both pass that check. Let the database enforce it: put a unique constraint on (business_id, user_id), or make that pair the primary key. When an insert repeats an existing pair, Postgres throws a unique constraint violation, and the review service turns it into a 409 Conflict. A key on user_id alone would wrongly allow only one review per user across every business.

Availability over consistency#

A single Postgres instance is a single point of failure. Add read replicas across availability zones for business pages, and run Elasticsearch as a cluster with replica shards, so search survives losing a node. Run several instances of each service behind the gateway.

The cost is lag: a new review reaches replicas and Elasticsearch a few seconds after it's written. That's the choice from step 2. Show reviewers their own review straight from the write response, so the delay isn't visible to the person who wrote it.

Scale#

Reads are 100 times the writes, so scale reads first:

  • Cache popular business pages in Redis with a short TTL, invalidated when a review lands (the cache-aside pattern).
  • Put a CDN in front of S3, so photos load from the edge and never pass through your services. Clients upload photos with pre-signed S3 URLs for the same reason.
  • Shard the reviews table by business_id when it outgrows one database. Every page view and every rating update already has the business ID, so each touches one shard (see choosing a shard key).

Whiteboard sketch#

The sketch below shows the design from steps 5 and 6: the gateway sends search and view calls to the business service and review calls to the review service, both backed by Postgres, with S3 for media and Elasticsearch fed by CDC.

Whiteboard sketch: the client calls an API gateway that handles authentication, authorization and rate limiting, and routes search(name, category, location) and view(businessId) to the Yelp service and reviews(businessId) to the review service. The Yelp service fetches actual media files from blob storage S3, and fetches relevant businesses from the Postgres DB and from Elasticsearch. The review service fetches the reviews from the Postgres DB. Postgres holds Business (businessID, name, category, location, description, media with url, avgRating) and Reviews (rating, text). An arrow from Postgres to Elasticsearch is labeled CDC: if anything changes, it is reflected in Elasticsearch

Key takeaways#

  • Three functional requirements: search by name, location and category, view a business, leave one rated review per business.
  • Reads outnumber writes about 100 to 1, so make search and business pages fast and keep review writes correct.
  • Text and geo search need an inverted index. Use Elasticsearch, fed asynchronously from Postgres by CDC, with Postgres as the source of truth.
  • Store photos in S3 and only their URLs in the database. Serve them through a CDN.
  • Protect the average rating with optimistic concurrency (a version check and retry), or with atomic rating_sum and review_count increments.
  • Enforce one review per user with a unique constraint on (business_id, user_id), not an application check.
  • Choose availability: replicas and an Elasticsearch cluster, accepting a few seconds of lag before a review appears in search.
Revision notes · 2 min

Core idea#

A local business search site lets users search businesses by name, location and category, view a business page, and leave one star-rated review per business. Reads outnumber writes about 100 to 1, so search must be fast and review writes must be correct.

Requirements#

  • Functional: search by name, location and category. View a business with photos and reviews. Leave a 1 to 5 star review with text, one per user per business.
  • Non-functional: 100 million users and 10 million daily active, availability over consistency, under 200 ms for search, view and writes.
  • Estimate: about 600 reads/s (3,000 at peak), about 6 review writes/s, around 1 TB of reviews.

Key points#

  • Actors: visitor (public, read path) and reviewer (signed in, rate-limited).
  • API: GET /v1/businesses?query&location&category, GET /v1/businesses/{businessId}, POST /v1/businesses/{businessId}/reviews with { rating, text }.
  • User ID comes from the auth token, not the body. Paginate reviews.
  • API gateway handles authentication, authorization and rate limiting, and routes to a business service and a review service.
  • Postgres holds businesses (business_id, name, category, location, description, media URLs, avg_rating) and reviews (business_id, user_id, rating, text).
  • Photos live in S3. The database stores only URLs.

Step 6 changes#

Requirement Change
Fast search Elasticsearch (inverted index plus geo queries), fed asynchronously from Postgres by CDC
Correct average Optimistic concurrency: version check and retry, or atomic rating_sum and review_count increments
One review per user Unique constraint on (business_id, user_id). Duplicate insert returns 409
Availability Postgres read replicas, Elasticsearch cluster, several service instances
Scale Redis cache for hot pages, CDN in front of S3, shard reviews by business_id

Pitfalls#

  • Read-then-write on the average loses reviews when two arrive at once.
  • Checking for an existing review in the application races. Let the database enforce uniqueness.
  • A key on user_id alone allows one review per user across all businesses.
  • Writing to Postgres and Elasticsearch from the service can leave them out of sync after a crash.
  • Proxying photo bytes through your services wastes capacity. Hand out URLs.

Short line#

Keep businesses and reviews in Postgres, search them through Elasticsearch synced by CDC, store photos in S3, enforce one review per user with a unique constraint, and update the average rating with optimistic concurrency.

Found a mistake? Email ask@bigonotes.com