Capstone - Enterprise Order Processing

Article · 4:00

PL/SQL packages, concurrency, recovery exercises and a capacity plan that names its licence costs.

The system. An enterprise order processing application: customers with credit limits, orders with lines, inventory that must not go negative, and reporting over all of it. The business logic lives in PL/SQL packages and the application has EXECUTE and nothing else.

Three invariants must hold:

  1. An order's total_amount equals the sum of its lines.
  2. Inventory never goes negative under any interleaving of concurrent orders.
  3. No order exists for a customer whose credit limit it exceeded at the time of placing.

What is assessed. 20% knowledge checks, 35% laboratory portfolio, 15% troubleshooting practical, 30% capstone. Three things carry the capstone.

Correctness under concurrency. Your test must place orders for the same product from many sessions at once and show inventory never going negative. The naive read-then-write fails this, and showing it failing before showing it fixed is worth marks.

Measured performance against a declared baseline. Declare it first: instance size, dataset, workload mix, and p50/p95/p99 for order placement and your three slowest reports. Include the row-by-row versus FORALL measurement from Module 3, because it is the clearest demonstration of PL/SQL performance in the course.

A defensible account of failure modes, operational limits and cost. Oracle is the one engine in this programme where cost is a first-class engineering constraint, and the capacity plan must state it: which edition, which separately licensed options your design depends on, and what the design would look like without them. A design that silently requires Partitioning, Active Data Guard and the Diagnostics Pack is a design with an undisclosed price.

Deliverables: architecture diagram; data model with the argument for the chosen model; PL/SQL source; reproducible test dataset; benchmark report; security review; recovery runbook with RPO and RTO from an actual RMAN restore; capacity and licence plan; and a short design defence.

-- ================= Invariants, as runnable assertions ==============
-- Schedule these. Run them after every deployment and every recovery.

-- 1. Order totals agree with their lines.
SELECT o.order_id, o.total_amount AS recorded,
       NVL(SUM(l.quantity * l.unit_price), 0) AS derived
FROM ord_owner.orders o
LEFT JOIN ord_owner.order_line l ON l.order_id = o.order_id
GROUP BY o.order_id, o.total_amount
HAVING o.total_amount <> NVL(SUM(l.quantity * l.unit_price), 0);
-- Expected: no rows.

-- 2. Inventory is never negative.
SELECT product_id, quantity_on_hand FROM ord_owner.inventory
WHERE quantity_on_hand < 0;
-- Expected: no rows.

-- 3. Nothing exceeded its credit limit.
SELECT c.customer_id, c.credit_limit, SUM(o.total_amount) AS outstanding
FROM ord_owner.customer c JOIN ord_owner.orders o ON o.customer_id = c.customer_id
WHERE o.status IN ('pending','shipped')
GROUP BY c.customer_id, c.credit_limit
HAVING SUM(o.total_amount) > c.credit_limit;
-- Expected: no rows.

-- ================= The concurrency test that matters ===============
-- Many sessions, ONE product, simultaneously. This is where the naive
-- read-then-write fails, and showing it fail before fixing it is
-- worth marks.
--
--   SELECT quantity_on_hand INTO v_qty FROM inventory
--    WHERE product_id = :p;                       -- no lock
--   IF v_qty >= :n THEN
--     UPDATE inventory SET quantity_on_hand = v_qty - :n ...
--
-- Two sessions both read 5, both decrement by 5, and the result is 0
-- having shipped 10. The fix:
--   SELECT quantity_on_hand INTO v_qty FROM inventory
--    WHERE product_id = :p FOR UPDATE;            -- <- the lock
-- or, better, make the database enforce it and let it fail:
--   UPDATE inventory SET quantity_on_hand = quantity_on_hand - :n
--    WHERE product_id = :p AND quantity_on_hand >= :n;
--   IF SQL%ROWCOUNT = 0 THEN RAISE_APPLICATION_ERROR(-20010,'no stock'); END IF;

-- ================= Declare the baseline BEFORE measuring ===========
--
--   Edition:   23ai Free (2 CPU, 2 GB) - state yours
--   Dataset:   1,000,000 customers, 10,000,000 orders, 40,000,000 lines
--   Workload:  60% reads, 40% order placement
--   Claim:     place_order           p99 < 60 ms at 100 sessions
--              monthly revenue       p99 < 3 s
--              top products 90 days  p99 < 8 s
--              RMAN restore RTO      <= 45 minutes, measured
--              archive log RPO       <= 15 minutes
--
--   Plus the PL/SQL measurement that teaches the most:
--              100,000 rows row-by-row   ___ s
--              100,000 rows via FORALL   ___ s      ratio ___

-- ================= The LICENCE position ============================
-- Oracle is the engine where cost is an engineering constraint. State
-- it, with the design that avoids each option:
--
--   Partitioning        EXTRA on EE. Without it: manual range tables
--                       and a UNION ALL view, or accept full scans.
--   Active Data Guard   EXTRA. Without it: the standby cannot serve
--                       reports; report from the primary or a copy.
--   Diagnostics Pack    EXTRA. Without it: Statspack instead of AWR.
--   Advanced Compression EXTRA. Without it: plan for the full size.
--   RAC                 EXTRA, and significant. Without it: Data Guard
--                       for availability, and accept the failover time.
--
SELECT name, detected_usages, currently_used, first_usage_date
FROM dba_feature_usage_statistics
WHERE detected_usages > 0
  AND name IN ('Partitioning (user)','Advanced Compression',
               'Real Application Clusters','Diagnostic Pack',
               'Tuning Pack','Active Data Guard','Transparent Data Encryption')
ORDER BY name;
-- Run this before anything reaches production. A design that silently
-- requires three paid options has an undisclosed price.

-- ================= Failure modes ===================================
--   instance failure mid-order    crash recovery replays redo; the
--                                 in-flight transaction rolls back
--   FRA fills                     archiving stops, so the DATABASE
--                                 stops. Alert on fra_pct.
--   ORA-01555 on a long report    undo too small for the longest
--                                 query; size from v$undostat
--   standby apply lag grows       RPO rising silently; alert on it
--   wrong UPDATE in production    Flashback Query to inspect,
--                                 Flashback Table to rewind, RMAN
--                                 PITR if it has gone too far
--
-- Verify after EVERY recovery with the three assertions above. "The
-- instance opened" is not verification.

Where to go from here

Longer reading about this subject, written alongside the course.

Course guide How to work through this course Why Module 1 is vocabulary, what to build alongside each module, the licensing habit to form, and what to skip on a first pass. 3 min read

Discussion

No questions on this topic yet. Sign in to ask the first one.