• 2 min read

Boost Your Application’s Performance with Server-Side JavaScript in Azure DocumentDB

In this post, we will take a closer look at DocumentDB’s interpretation of stored procedures.

Integrated JavaScript as a Modern Day T-SQL

Did you know Azure DocumentDB supports the transactional execution of JavaScript logic natively inside the database engine? The approach of using JavaScript as a modern day T-SQL provides developers a rich and familiar interface to implement business logic. This is expressed in the form of stored procedures, triggers, and user-defined functions (UDFs). In this post, we will take a closer look at DocumentDB’s interpretation of stored procedures.

High Performance Batching and Sequencing Operations

A major advantage with stored procedures is that they enable applications to perform complex batches and sequences of operations directly inside the database engine, closer to the data. Developers can group and sequence operations (like inserts, updates, or deletes) and submit them together. The benefit here is that the network traffic latency costs for batching and sequencing operations can completely avoided.

A sequence of database operations

A sequence of database operations

A sequence of operations using Stored Procedures

A sequence of operations using Stored Procedures

Another major advantage to using stored procedures is that they get implicitly precompiled to the byte code format upon registration, avoiding script compilation costs at the time of each invocation. The fact that JSON is intrinsically mapped to the Javascript language type system and is also the basic unit of storage in DocumentDB also allows for further optimizations behind the scenes, like the lazy materialization of JSON documents in the buffer pool and making them available on-demand to the executing code. This ensures invocations of stored procedures are fast and have a low footprint.

Multi-Document Transactions

DocumentDB offers full ACID (Atomicity, Consistency, Isolation, and Durability) guarantees for all operations that take part of a single stored procedure. In other words, stored procedures enable applications to combine related operations in a single batch so that either all of them succeed or none of them succeed. This is notable, because few NoSQL databases provide ACID transactions across multiple documents / records. So how do you commit or rollback a transaction? The answer is simple: all operations within a Stored Procedure are automatically wrapped under a single transaction. If the JavaScript completes without any exceptions, the operations to the database are committed. If there is any exception propagated from within the script, the JavaScript runtime will automatically roll back the entire transaction. In effect, throwing an exception is equivalent to a “ROLLBACK TRANSACTION” in DocumentDB. It’s important to note that stored procedures have bounded execution, in which all operations must complete within the server specified request timeout duration. If an operation does not complete with that time limit, the transaction is automatically rolled back. In order to simplify development to handle time limits, all CRUD (Create, Read, Update, and Delete) operations return a Boolean value that represents whether that operation will complete. This Boolean value can be used a signal to wrap up execution and for implementing a continuation based model to resume execution (this is illustrated in our code samples below).

Video Walkthrough, Code Samples, and More!

Can’t wait to get started? We’ve prepared a brief 5-minute video to walking you through the creation and execution of a stored procedure, available on Channel 9.

Azure Demo: A Quick Intro to Azure DocumentDB's Server-Side Javascript
Azure Demo: A Quick Intro to Azure DocumentDB's Server-Side Javascript

 

We’ve also prepared a variety of new stored procedure examples including upsert, bulk-delete, update, and bulk-import, available on our Github repo. Want to share your awesome stored procedure? Please, send us a pull-request! We’d love to feature and spotlight you on our Github and Twitter accounts. To learn how to get started with DocumentDB or for more information, please check out our website. For more information regarding DocumentDB’s server-side programming model, check out our documentation here.