Skip to content
Tutorials Center
My learning

Topics

Lesson 35 of 37 ยท Projects and Practice

Project: Order Management System

Build an order management data model in DynamoDB step by step: access patterns, single-table keys, a status index, stock transactions and a runnable stand-in.

In this project you design the data layer of a small shop from scratch, following the process from earlier lessons. You will write down access patterns, pick keys, add one global secondary index, and implement the tricky operations (place an order, pay, ship) with conditions and transactions. Because DynamoDB is not available in the browser, you will build and run a Python stand-in that enforces the same key rules, and you will see the real API calls as read-only code.

The goal is not the code. It is the habit: every operation must be one GetItem, one Query, or one conditional write.

Step 1: the access patterns

# Pattern Frequency
A1 Get a customer by ID high
A2 Place an order for a customer (reduce stock, never oversell) high
A3 Get one order with its lines high
A4 List a customer's orders, newest first, paged high
A5 Mark an order as paid (only from PENDING) medium
A6 List PAID orders waiting for shipment, oldest first warehouse job, steady
A7 Get a product (price, stock) very high

See designing from access patterns for the method.

Step 2: the key design

Entity PK SK GSI1PK GSI1SK
Customer CUSTOMER#<id> PROFILE
Product PRODUCT#<id> META
Order header ORDER#<id> META STATUS#<status> <createdAt>#<id>
Order line ORDER#<id> LINE#<nnn>
Customer's order pointer CUSTOMER#<id> ORDER#<createdAt>#<id>

Why this works:

  • A3: Query PK = ORDER#<id> returns header and lines (an item collection; see single-table design).
  • A4: Query PK = CUSTOMER#<id>, begins_with(SK, "ORDER#"), reversed, with Limit and LastEvaluatedKey for pages. The pointer item holds a small summary (total, status) so the list needs no second read.
  • A6: a sparse index on GSI1PK = STATUS#PAID, sorted by creation time. When the status changes, GSI1PK changes, and the order moves between index partitions.
  • A1, A7: plain GetItem.

Order flow

Place Transaction: order + lines + stock
Pay Conditional update PENDING to PAID
Ship Query the PAID index
Done Status SHIPPED

Every step is one safe operation.

1 / 2

Step 3: placing an order

Placing an order must create the header, the lines and the customer pointer and decrement stock for each product, or nothing. A transaction does that (limit 100 actions, so cap lines per order at about 40: two items per line plus extras):

Python
# boto3 client sketch (read-only here)
actions = [
  {"Put": {"TableName": "Shop", "Item": header, "ConditionExpression": "attribute_not_exists(PK)"}},
  {"Put": {"TableName": "Shop", "Item": pointer}},
  *[{"Put": {"TableName": "Shop", "Item": line}} for line in lines],
  *[{"Update": {"TableName": "Shop", "Key": product_key(l),
                "UpdateExpression": "SET stock = stock - :q",
                "ConditionExpression": "stock >= :q",
                "ExpressionAttributeValues": {":q": {"N": str(l["qty"])}}}} for l in lines],
]
client.transact_write_items(TransactItems=actions, ClientRequestToken=order_id)

If any product lacks stock, nothing is written. See transactions.

Step 4: run it with a stand-in

The stand-in implements a table with PK/SK, a derived GSI and an atomic multi-item write. Study how each operation maps to the real API.

Python Order system on a stand-in table

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

You should see order o3 rejected without leaving half an order behind, the second payment refused, and the shipment list sorted by creation time.

Step 5: finishing touches to try

  1. Paging (A4): limit to 1 order per call with the after cursor and loop until empty.
  2. Ship (new pattern): add ship(order_id) that moves STATUS#PAID to STATUS#SHIPPED, only if it is PAID.
  3. Cancel: return stock in a transaction, only from PENDING.
  4. Expire unpaid orders: add a TTL attribute to PENDING orders and remove it when paid (TTL). Consider archiving expired ones with a stream.
  5. Hot product: if one product is bought thousands of times a second, its stock item becomes hot. Split the stock into several counters (sharding).

In a real project, create the table from a CloudFormation or CDK definition and test the repository against DynamoDB Local (local development).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why is order creation a transaction in this design?

    Without a transaction a stock failure could leave an order without stock reserved, or stock reduced without an order.
  2. 2. How is 'list PAID orders oldest first' served?

    The status is part of the index key, so a Query reads only paid orders, sorted by time.
  3. 3. How is a double payment prevented?

    The condition is evaluated atomically with the update, so the second payment fails.
  4. 4. What limits the number of lines in one transactional order?

    A transaction holds at most 100 actions, and each line uses a put plus a stock update.

Summary

  • Start from the access-pattern table and derive the keys.
  • One item collection per order serves header and lines in a single Query.
  • A GSI keyed by status serves the warehouse list; the key moves when status changes.
  • Use a transaction to place an order and a conditional update to pay.
  • Extend the project with paging, shipping, cancel, TTL and sharding.

Next: Project: Leaderboard and Sessions