Databases: SQL and NoSQL
Core Concepts (5)
- 1.Databases give durable, queryable storage
- 2.Relational databases enforce structure and support joins
- 3.NoSQL families trade structure for scale or flexibility
- 4.Access patterns should drive the data model
- 5.Indexes speed reads and cost writes
Map to Your Experience
Recall an application where data had to stay correct no matter what, such as money. Now recall one where speed or volume mattered more. Would the same database suit both?
Design Brief: Choose a Data Store
Learn
A database stores data durably and lets you find it again. The most important choice is the kind of database, and the right answer depends on how the data will be used.
Relational databases, queried with SQL, store data in tables with rows and columns. They enforce a schema, support joins between tables, and provide transactions: a group of changes that all succeed or all fail. That makes them a strong default for data that must stay correct, such as orders, accounts, and payments.
NoSQL is an umbrella term for several designs. Key-value stores look up a value by a key and are very fast. Document stores keep flexible, nested records, convenient when each item has a different shape. Wide-column stores handle huge write volumes across many machines. Graph databases make relationships the main thing. Each gains something, typically scale or flexibility, by giving up something else, often joins or strong transactions.
A useful rule is to start from access patterns. Write down the questions the application will ask: fetch an order by id, list a customer's orders, search products by name. The data model and database then follow from those questions, instead of the other way around.
Indexes are the main tool for fast reads. An index is a sorted lookup structure on one or more columns, like the index of a book. It makes finding rows quick but slows writes and uses storage, because every change must also update the index. Index what you query often, not everything.
Beware of choosing a database for fashion. A single well-run relational database handles far more load than most teams ever need. Reach for something more exotic when you can show, with numbers, that the simple choice will not meet a requirement.
Map
For each of these, decide relational, key-value, or document and give a one-line reason: a bank ledger, a user session cache, a blog with posts of varying structure. Mention one query for each that drove your choice.
Design
An online shop stores orders, customers, and a product catalog with varying attributes. List the top five queries. Choose a store for orders and a store for the catalog, and justify each. Define one index you would add to the orders data. Close by stating what you sacrificed with each choice, and under which growth numbers you would revisit it.
Requirements
- •Name the access patterns
- •Pick a store for orders and for the catalog
- •State what you gave up
Constraints
- •One team of five engineers
- •Orders must never be lost or double counted
Must Cover
Trade-off Axis
consistency and structure vs flexibility and scale
Referenced Patterns
Competencies Advanced
Ready to Design?
Open this day in the Design Studio. The studio follows this track: its days, concepts, patterns and trade-offs are loaded, and you keep one design across the whole track.
Open in Studio →