BM
Bashar ModallalSOFTWARE · AI · SYSTEMS
NoSQL and Distributed Data Modeling

Beginner · Lesson 1 of 12

Software Architecture

Start with Access Patterns

Use three camera-shop queries to compare key-value, document, wide-column and graph models. Trace the reads, then change a lens price and test the result.

A camera and two lenses with inventory tags on a camera shop counter.

A customer walks into a camera shop with a camera body and AED 900 to spend. She points to two lenses on the shelf. “Which one fits this camera?” The assistant scans a label, opens the lens department on a tablet, then checks which lenses the shop has tested with that body. All three screens say catalog. Yet a system that answers the scan instantly may have to read every item in the shop to answer the other two questions.

An access pattern describes a recurring request in terms you can test. What does the caller know at the beginning? Which records and fields should come back? In what order, how many, and how fresh must they be? You can write the answer before choosing a database. That short description gives you a way to compare key-value, document, wide-column and graph designs against the same job.

Give the shop a catalog you can check by hand

The shop stocks camera body C10 and four lenses. All prices are in dirhams. The product codes are permanent stock keeping units, or SKUs. The shop maintains a list of body–lens combinations it has tested. A missing combination means compatibility is unconfirmed by this shop. A lens might still fit physically. Making that distinction prevents an empty search result from turning into bad advice at the counter.

SKU Item Price Shop tested with C10?
C10 Camera body 2400 —
L20 Street prime 650 Yes
L21 Portrait prime 850 Yes
L22 Travel zoom 850 No record
L23 Studio zoom 1200 Yes

Write three requests on separate cards. A. Given an exact SKU, return one product card with its name and current catalog price. B. Given the lens department, return its two cheapest items, then the next two, ordered by price and SKU. C. Given a camera SKU and a maximum price, return the shop-tested lenses for that body within the budget, in the same price order. Here each query is about the catalog. Available stock and the price honored at checkout are separate business decisions.

Now run the requests on paper. A scan of L21 returns Portrait prime at 850. The first department page contains L20 and L21. L21 and L22 tie on price, so the SKU breaks the tie. The second page starts with L22 and ends with L23. For C10 and a budget of 900, three lenses are affordable, but only L20 and L21 also pass the shop test. L22 is the useful counterexample: a query that returns it has answered “cheap enough,” while the customer asked for two conditions together.

What you can do with a known key

Imagine a cabinet with a drawer labelled for each SKU. If the caller says L21, you already know which drawer to open. A key-value design can store the display card at a key such as product:L21. With Redis strings, for instance, the value can be a serialized card and GET retrieves that value by its key. Decide what the scanner should show when a label has no matching key. A missing product and a product with a price of zero have quite different meanings. Redis GET reference

The department request starts with “lenses,” not with a product key. If the only stored facts are individual cards, the application has no ready-made ordering of lens prices. Reading every card, discarding the bodies and sorting the rest works for five items. Write down what happens at fifty thousand. The tablet still needs two results, but the amount of data examined grows with the entire catalog. A response limit on its own puts no limit on the search work.

You can maintain a second structure listing lens SKUs in price order and a third listing tested lenses by body. For the first department page, read two identifiers and fetch the corresponding cards. This design pays some of the cost when the catalog changes. A price edit must update both the product card and its position in the ordered listing. If you run an asynchronous updater, define how long the listing may lag and what repairing a missed update involves. These supporting structures are part of the design; include their reads and writes when comparing it with another option.

What belongs in one document?

A product document can bring the fields on one card together. A lens might have a focal length, while a camera body has a sensor size. Both still have a unique SKU, department, display name and numeric price. Store and validate those shared fields consistently. If one shop assistant enters the string “850 AED” and another enters the number 650, you have made ordering and comparisons harder than the original question. Format the currency for the screen after retrieving the numeric value. MongoDB data modeling guide

For A, look up the document by its SKU. For B, use an index beginning with department, followed by numeric price and SKU. Within the lens department this arranges records in the order the tablet requests. To continue after L21, carry both 850 and L21 in the page cursor. Accept prices above 850 or a price equal to 850 with a SKU after L21. A cursor containing price alone would either skip L22 or repeat an item at the boundary. This is a stable ordering of unchanged data; prices moving between requests can still change what appears on later pages.

For C, a small catalog might keep the list of tested body identifiers on each lens document. Query lenses containing C10 and costing at most 900. Before calling that design fast, inspect how many candidates it examines on representative data. A store selling thousands of bodies may prefer a separate collection of tested pairs, particularly if a pair has its own test date, technician and approval status. The card and the test certificate have different owners and can change at different times. Those facts help decide what to embed and what to reference.

Design an ordered group of rows

In a wide-column database such as Cassandra, design a table around the request that reads it. Think of the partition key as the address of a group and the clustering columns as the order of rows inside it. One table keyed by SKU can serve the scanner. For B, a small shop could use a table partitioned by department, with price and SKU as ascending clustering columns. Supply the department key, read the first two rows in that partition, and keep the continuation information for the next request. Apache Cassandra data modeling introduction

For C, consider a separate table of tested lenses partitioned by camera body and ordered by lens price and SKU. For partition C10 the test records appear at 650, 850 and 1200. A range ending at 900 selects the first two. L22 never enters that partition. The merchant gains a simple read, but changing L21's price also changes an ordering key in each affected listing. Remove the old entry and insert the new one, with a repair strategy if a write is interrupted. A popular department can also concentrate too many reads or rows in one partition. Splitting it can relieve pressure while making the first page a merge across groups. Test the largest department before adopting that layout.

Follow the relationship the customer named

A graph design represents products as nodes and approved tests as relationships. Start at the uniquely identified C10 node, follow outgoing TESTED_WITH relationships to lenses, filter by price, and order the survivors by price and SKU. Three relationships lead to L20, L21 and L23. Only the first two meet the budget. The path itself can explain a recommendation if the relationship carries the date and evidence of the shop test. The missing edge to L22 still means “no recorded test.” Neo4j graph database concepts

The same graph can answer A using an indexed SKU lookup. Query B still needs a way to find and order department items. Being able to traverse a relationship does not make every department page cheap. The tradeoff becomes more interesting if customers ask about adapters connecting a body to a lens. Then the relationship type, direction and allowed number of steps become part of the request. Two products sharing a nearby node does not, by itself, establish that the resulting physical combination works.

Make a first choice you can revise

For this shop, suppose most requests are scans and department pages, tests are few per lens, and staff edit product cards several times a day. Start by testing the document design with the SKU and department indexes. It keeps one editable price per product and expresses all three current queries. Give the other designs the same input, page sizes and expected answers. Count records examined, round trips to fetch cards and places that a price edit must update. Repeat on a popular body and the largest department. A working five-item demonstration establishes meaning; measurements on realistic traffic establish whether the cost is acceptable.

Exercise. The merchant reduces L23 from 1200 to 800. Before running any queries, write the two items on the first department page and every tested lens under the customer's 900-dirham limit. Then name one stale result that would expose a missed update in a duplicated listing.

Answer. Page one is L20, L23; the tested list is L20, L23, L21. L22 remains outside the tested list despite its price. If a direct scan shows L23 at 800 but the tested list still excludes it, inspect the body-specific listing and its previous 1200 entry. Your design passes this exercise when a fresh read under the agreed freshness rule gives the same answers across all three screens.

اقرأ الدرس بالعربية

YOUR TAKE

What did you think?

Choose one reaction for this article.

Was this useful?

A cookie helps limit repeat responses. Your feedback is private.

Share this noteLinkedIn ↗X ↗Email ↗

Keep exploring.