What's changed: Created DP-420 Chapter 2 (Domain 1 second-half: SDK client connectivity (gateway vs direct/singleton/emulator/preferred regions/429-transient retries); SQL queries and data access (arrays-nesting-aggregation-subqueries-functions/point op vs query/patch/Transactional Batch/Bulk/ETag optimistic concurrency/consistency override-session token/pagination-continuation token/query metrics); server-side JS (stored procedure = ACID in one logical partition/pre-post triggers/UDF)).
2.3Server-side programming (JavaScript)
Understand Cosmos DB for NoSQL server-side JavaScript: stored procedures (transactions within one logical partition), triggers (pre/post), and user-defined functions (UDFs).
Cosmos DB can run server-side JavaScript close to the data. Three kinds—stored procedures, triggers, and user-defined functions (UDFs)—are used by purpose.
2.3.1Stored procedures and transactions
A stored procedure can be written/deployed/called and processes multiple items within one logical partition as a transaction (ACID) (you specify the partition key at execution). Use it to read/write multiple items atomically in one round trip. Triggers run in conjunction with operations—pre-triggers (validate/enrich before write) and post-triggers (e.g., update aggregates after write)—and must be specified explicitly per request.
2.3.2User-defined functions (UDFs)
A user-defined function (UDF) provides reusable compute logic callable inside a query (e.g., tax calculation, string formatting), usable in SELECT/WHERE—but UDFs often bypass indexes and scan, which can raise RU. Distinguish "reusable custom compute inside a query" = UDF, "atomic processing of multiple items" = stored procedure, "hooks before/after an operation" = trigger.
Cues: "atomic multiple items within one logical partition" = stored procedure. "validation before / processing after a write" = pre/post trigger. "custom compute inside a query" = UDF. Server-side JS runs with a partition key and cannot span logical partitions.
Watch the mix-ups: (1) Stored-procedure transactions are within one logical partition only (no cross-partition). (2) Triggers are not automatic—must be specified per request. (3) UDFs often miss indexes and can raise RU—do not overuse. (4) Server-side JS has time/RU limits (combine with batch/Bulk for large work).
2.3.3Section summary
- Stored procedure = atomic (ACID) processing of multiple items in one logical partition
- Triggers = hooks before/after operations (pre/post, specified per request)
- UDF = custom compute inside a query (often misses indexes, watch RU)
Sign in to track progress — Log in.
Quick check
(just a quick review)Q1. You want to read/write multiple items in one logical partition server-side, atomically in one round trip. Best?
Q2. You want to insert validation or default enrichment automatically before an item write. Best?
Q3. You want to call reusable custom compute (e.g., tax calculation) inside a query SELECT/WHERE. Best?
Q4. What is the execution scope of Cosmos DB server-side JavaScript (stored procedures/triggers)?
Q5. What is a correct caution when overusing UDFs?

