🗄️ NoSQL data models: cheat sheet
🎨 The analogySwapping one big ruled ledger for storage furniture built for each job: pegs, filing cabinets, card catalogues.
🎓 The regular explanationThe four main non-relational data models (key-value, document, wide-column and graph), how each is modelled around its access patterns rather than normalised, and the trade-offs in joins, consistency and write cost that decide when to use one instead of a relational database.
- The clerk writes Ana's order on the next line of the ledger. Every line has the same ruled columns, so it fits neatly.
- Now an order comes with a gift note, but there is no column for it. Every page has to be ruled again before anyone can write one.
- The shop gets busier and busier, and there is only one ledger. However fast the clerk writes, it can only be in one place.
- So the shop adds filing cabinets in three rooms. Each folder can hold whatever papers it needs, gift note included, and the work is shared between the rooms.
- At the coat-peg wall, the clerk just says a number and gets back whatever hangs on that peg. Nothing could be quicker, but the number is the only way to ask.
- At the filing cabinet, one folder holds everything about Ana, so one trip brings back her address and her orders together.
- At the card catalogue, Ana's drawer already keeps her cards in date order, so the cards since February come out in one neat handful.
- At the corkboard, the clerk follows the strings from Ana's photo to her friends, then to their friends.
- The clerk puts Ana's address and her first two orders straight into Ana's folder. The product folders already hold one copy of each product.
- When someone asks about Ana, one trip to the cabinet brings back everything, because it's all in one folder.
- The third order just has a slip saying see product folder 7. That keeps one copy of each product, but the clerk now makes a second trip.
- Every new order adds a page to Ana's folder, so it keeps getting thicker until it's too heavy to carry. Things that pile up without end deserve their own folders.
- Each customer has a drawer in the card catalogue, and the cards inside stay in date order. Ana's new March order slots into her drawer in date order.
- The question this catalogue was built for, Ana's orders since February, opens one drawer and lifts out one neat handful of cards.
- But who bought the teapot would mean opening every drawer. So the shop adds a teapot drawer, and each teapot order is now written on two cards, one in each place. Ana's old teapot order is copied in too.
- Ben then cancels. The clerk fixes his drawer, but the trip to the teapot drawer goes wrong, and now the two cards disagree.
- The teapot card says £5 in all three rooms. The clerk changes it to £6 and only waits for one room to say done. The new card is still on its way to the other two rooms.
- Someone asks the price and is happy with one room's answer. The helper asks room 3, which still says £5. Quick, but out of date.
- This time they ask two rooms. Room 1 says £6 and room 3 says £5, so the helper picks the newer card and puts a fresh copy in room 3. To be sure every time, the clerk should wait for two rooms when filing too.
- Room 2 is locked. Waiting for two rooms still works, because rooms 1 and 3 answer. Waiting for all three would leave the clerk stuck.
- Ana's basket hangs on a coat peg. It changes often and only ever needs her peg number.
- The teapot's page comes from its folder in the product cabinet, with every detail in one place.
- For people who bought this also liked, the clerk follows the strings on the corkboard.
- Paying goes in the ledger, where the payment and the stock change are written together or not at all.
- The catch is that the cabinet and the corkboard must hear about the sale too. They're told just after, so for a moment they're a little behind.
🧩 Key questions
- Every new order gets added to Ana's folder. What goes wrong after a few years?
- The folder keeps getting thicker until it's slow and too heavy to carry, so orders should get their own folders. Keep together what you read together, but things that pile up without end get their own folders, with a slip back to Ana.
- The shop wants to answer "who bought the teapot?" quickly, but the catalogue only has a drawer per customer. What does it do?
- Add a drawer per product and write each order on two cards, one in each. Reading becomes one drawer again; the price is double the writing and keeping the copies in step.
- The clerk only waits for one room to say done when filing, and someone then asks one room for the price. What can happen?
- They may get the old price from a room the new card hasn't reached yet. Waiting for two rooms when filing and asking two rooms when reading means at least one always has the new card.
- Payments must take money and update stock together, and the boss keeps asking new questions nobody planned for. Which piece of furniture fits best?
- The ledger, with its strict columns and cross-references. The ledger writes things together or not at all, and its cross-references can answer questions nobody planned.