Guide

Most database mistakes start with picking a store before knowing the queries. An access pattern is one concrete query: what it looks up by, what it returns, how often it runs, and how fast it must answer. Write the list down before you draw any boxes.

Concept

Before you start. p99 latency is the time that 99 out of 100 requests finish within. An OLTP workload is many small reads and writes that need transactions. An OLAP workload scans and aggregates many rows, and runs less often.

The big idea

Think of a shopping list written before you walk into the store. You do not wander the aisles hoping to find what you need. You plan the trip from the list.

The list breaks down when the questions change. That is why the list needs a review each quarter, and why new questions can still arrive later.

Walking through it

Here is the access pattern list for an e-commerce order system.

#Access patternRateLatency target
1Get an order by ID, with its line items2,000 per secondp99 under 20 ms
2List a customer's orders, newest first, paginated500 per secondp99 under 50 ms
3Create an order and decrement stock atomically200 per secondp99 under 100 ms
4List orders pending shipment for one warehouse5 per secondp99 under 500 ms
5Revenue by category per day, last 90 days10 per hourseconds is fine
6Search products by free text1,000 per secondp99 under 100 ms

Patterns 1 to 4 are OLTP. They are small, frequent and indexed, and they need transactions. Pattern 5 is OLAP. It is rare but heavy, and it scans millions of rows. Pattern 6 needs relevance ranking, so it goes to a search index.

For each pattern, record these:

  • Lookup shape: point lookup, range scan, join, full-text, aggregation, graph traversal or nearest neighbor.
  • Read/write ratio and absolute rates.
  • Consistency needs: must a read see a write that just happened?
  • Data size: 50 GB fits in memory on one machine. 50 TB does not.

Common mistakes

  • Picking a store first. You then fit the queries to the store, not the other way round.
  • Writing "list orders" with no rate or latency. Without those numbers, you cannot choose an index or a store.
  • Skipping consistency needs. A read that must see its own write needs different handling from a dashboard.

Check yourself

  1. Which pattern in the table is OLAP, and why? Pattern 5. It aggregates many rows over 90 days, so it is heavy, but it runs only 10 times an hour.
  2. Why record the latency target with each pattern? It decides whether a scan is acceptable, or whether you need an index or a separate store.

Key takeaways

  • An access pattern is a concrete query with a rate, a latency target and a consistency need.
  • Write the list before choosing a database.
  • Most patterns are OLTP, and a few are OLAP. Keep them apart in your design.
  • Review the list when the product's questions change.

Further learning

Next, see how indexes make these patterns fast, and what they cost, in indexing strategy.

For the wider context, see databases and data modeling.