Core Concepts

SQL vs NoSQL

Choose a store by what the application reads together, what must commit together, and which workload needs to scale.

Free~14 min

Start a database design with the queries the application needs and the records it must update together. A relational database such as PostgreSQL can save an order with all its line items, retrieve past orders for a customer, and let support search across orders. Product photos on those same pages can come from object storage.

Default: Postgres

An order can live in one table and its items in another. A join combines rows from related tables, for example by matching the order ID on both. A transaction can save the order and every line item together. If part of that transaction fails, the database can roll it back so the app does not leave a partial order.

The same database can store fields with a flexible shape using JSONB, PostgreSQL's queryable binary representation of JSON, and can search text using its built-in full-text search. An application can start with those tools and add a specialized system when its queries or operating needs change. PostgreSQL JSON types · PostgreSQL text search

Before adding a store, name the reads and writes, their volume, and which records need to stay in agreement. A missing index can make the current store do far more work than the query needs. Measure that before changing the data model.

When to leave Postgres

Choose the next step based on the operation that is still struggling after tuning. If order search is slow and checkout meets its target, keep PostgreSQL in charge of orders and test a separate search index. Measure query latency and how long new orders take to appear in search.

JobStarting pointReason to change
Orders or account recordsTables, indexes, and transactionsA measured read or write limit can justify read replicas or splitting data across multiple writers.
Flexible documentsJSONB for nested fieldsMost operations retrieve one self-contained document by ID, and workload tests favor a document store such as MongoDB.
Full-text searchBuilt-in text searchRanking features or search load justify a separate index such as Elasticsearch.
Frequently requested dataRead the source recordRepeated reads cost enough to justify storing a temporary copy in a cache such as Redis.
Images or other large filesStore the object separately once it has its own access and retention needsKeep the object in storage such as S3 and store the pointer in the relational database.
High-volume writes keyed by one fieldOne writer with tuned queries and indexesWorkload tests show a limit, and a good key lets a store such as DynamoDB or Cassandra distribute the work. Sharding the relational database is another option.

Match the hot path

The hot path is the set of operations that sees the most traffic or dominates the resource we need to improve.

Document stores. If the app fetches one customer's preferences as a group, it can use the customer ID to fetch one document. Storing an entire related group in the shape a read needs is a form of denormalization. If fields appear in more than one record, edits must reach each copy or readers can see stale values.

Partitioned key-value and wide-column stores. A partition key is a field the store uses to assign data to part of the system. A key such as customer ID can make common customer-scoped reads inexpensive. A particularly busy customer can still overwhelm its partition, and queries spanning many keys may have to contact several parts of the system. DynamoDB can store nested fields and distribute data by key.

Redis. A cache can serve copies of frequently read records. The store that owns the original records supplies a value on a miss. If Redis owns an important value itself, spell out when its writes become durable and what a failover can lose.

Transactions: say the scope

List which records must commit together. For the order example, a PostgreSQL transaction can save the order and its line items in one commit. The same question applies if we model orders in another store.

DynamoDB's TransactWriteItems can act on up to 100 distinct items across tables in the same AWS account and Region, subject to a 4 MB aggregate item size limit. MongoDB supports multi-document transactions across collections and shards on supported deployments. Both have practical limits on where and how you can use them. DynamoDB transactions · MongoDB transactions

When one commit cannot include every step, decide which state is visible between commits. For example, a transactional outbox saves a state change and a record of the event to publish in the same local transaction. A separate process delivers the event later. A saga coordinates a series of commits and gives the application a way to compensate if a later step fails.

Using more than one store

A design that uses several stores for different jobs is sometimes called polyglot persistence. Every extra store brings its own failure and recovery path. If search uses a separate index, for example, the application must tolerate the time between an order update and the update appearing in search. Keep the owner of each record clear.

See also

Used in

  • Prompt Playground - PostgreSQL holds durable templates, sweeps, and cells. Redis holds cached data, streams, and coordination state.
  • ChatGPT - PostgreSQL holds turn state and recent text. Older text moves to per-conversation objects, with lookup records in PostgreSQL.
  • Batched Inference Service - the application looks up short-lived results by request ID in Redis.
  • Group Chat with Threads - storing related messages by channel keeps a thread reply and its channel update within one PostgreSQL transaction.

Done reading? Mark it so it sticks in your dashboard.

Discussion