Home›Design›Product catalog
Design doc · Product catalog

Product catalog with varied attributes

Where should a catalog go when every product type has different attributes?

Applications like this: shops with shoes, laptops and food in one catalog. Real estate listings. Car parts. This page is written the way I write a database design doc: the workload first, then where each kind of data goes and why, then how it grows, what breaks, and what I rejected.

The short answerPostgreSQL with common columns plus a JSONB column for the varied attributes, or MongoDB with one document per product. A search engine on top for browsing and filtering.
1 · The workload

What the application asks of the database

DimensionThis application
WritesLow. Merchants edit products a few times a day.
ReadsVery high. Every page view, every search, every filter.
Read to writeHundreds to one, or more.
ConsistencyLoose. A price change can take a second to appear in search.
LatencyProduct pages from cache in milliseconds. Search under 200 ms.
GrowthModerate. Thousands to millions of products.
Connected

Real failures behind this design

These incidents are why the choices below matter. Each one is written up with its numbers and sources.

2 · What goes where

Each kind of data, its store, and why

ApplicationThe product of recordPostgreSQL with JSONB, or MongoDBSOURCE OF TRUTHBrowsing, filters, facets, full-textElasticsearch or OpenSearchCOPYProduct pagesA cache or a CDNCOPY

Solid arrows: the application writes there. Dashed arrows: a copy fed from the source of truth (change data capture, an outbox, or a cache fill), which can be rebuilt from it.

DataWhere it goesWhy
The product of recordPostgreSQL with JSONB, or MongoDBShared fields (price, title, stock link) as columns or fields. Type-specific fields in JSONB or the document.
Browsing, filters, facets, full-textElasticsearch or OpenSearchCounting matches per color, size and brand across millions of products is what an inverted index is for.
Product pagesA cache or a CDNPages change rarely and are read constantly.

How each family of store behaves under traffic: Database families.

3 · Data model

The core tables and the queries they serve

CREATE TABLE products (
  id          bigint PRIMARY KEY,
  sku         text UNIQUE NOT NULL,
  title       text NOT NULL,
  price_cents bigint NOT NULL,
  category    text NOT NULL,
  attrs       jsonb NOT NULL DEFAULT '{}'   -- {"ram_gb":16, "color":"black"}
);
CREATE INDEX ON products USING gin (attrs jsonb_path_ops);

SELECT id, title FROM products
WHERE category = 'laptops' AND attrs @> '{"ram_gb": 16}';
4 · How it grows

Start simple, change one thing at a time

  1. One databaseFilters with SQL on a few thousand products are fine.
  2. Facets and full-text get slowAdd a search engine, fed by change data capture or by an outbox table.
  3. Global trafficCache product pages at the edge. The database sees mostly writes and cache misses.

Scale it: watch this design grow

Pick a stage, then throw something at it. The numbers are a model to show the shape of each change, not a measurement; the measured numbers are in the lab notebooks.

5 · What breaks first

The failures to plan for

What happensWhat you seeWhat I do
LIKE %term% on titlesSequential scans in the slow logMove text search to a search engine, or use full-text search in PostgreSQL for small catalogs.
The search index out of syncA product shows a price it no longer hasWrite changes to an outbox table in the same transaction, and have one process ship them to the index.
Every attribute in JSONBJoins and sorts on JSON fields are slow and hard to indexPromote frequently used attributes to columns.
6 · Alternatives

What I considered and did not pick

Cost. Search clusters are often the largest line on a catalog bill. Size them for the index you have, and keep replicas at what the query rate needs.

How this page was writtenFrom the engines' documentation and common practice. Measured on real servers: one lookup with and without an index, in the notebooks. Before you build: check the anti-patterns, size it with the capacity calculator, and write your own version from the design doc template. Want a second opinion? Submit it for review.
7 · Where to run it

On-prem, managed, EC2 or Kubernetes

StageWhere it runs
Where I would startManaged relational, plus a managed search service
When it growsThe same, with a CDN in front

For a team without a dedicated database team. Who does which job in each option, and the full-cost comparison: Where to run the database.

8 · Estimate it

The numbers, worked live

The back-of-the-envelope numbers I would want before choosing anything. Move the assumptions to match your application. The link keeps your numbers, so a colleague sees the same ones.

Part 3 of 4 · Design · 24 pagesAll pages

Which database does my application need? What kind of application data goes in which database, and why, as design docs, plus the anti-patterns and the decisions behind them.