Work with Azure Cosmos DB Flashcards
What version of the cosmos SDK are we currently using?
- version 3
- uses containers and items
- container is a collection, graph or table
- item is a document, edge/vertex or row and is the content of a container
What function creates a new cosmos client?
CosmosClient(endpoint, key)
- creates new cosmos client with the conn string
- thread safe
- only one instance recommended per lifetime
What functions are available for databases?
- CreateDatabaseIfNotExistsAsync(id, timeout); id only used to verify if theres an existing db
- readAsync()
- deleteAsync()
What functions are available for containers?
- CreateContainerIfNotExistsAsync(id, partitionkeypath, throughput)
- GetContainer(id)
- ReadContainerAsync()
- DeleteContainer()
What functions are available for items?
- CreateItemAsync(id, partitionkey)
- ReadItemAsync(id, partitionkey)
- GetItemQueryIterator(); creates a query for items under a container using a SQL statement with parameterised values
What does cosmosDB provide in terms of functions?
- provides language-integrated, transactional execution of JS that let you write stored procs, triggers and uder-defined functions
- need to be registered before being called
what are stored procedures in cosmosDB?
- can perform CRUD operations inside a container
- registered per collection
- can operate on any doc or attachment present in collection
- requires a context object which is used to access all operations that can be performed by cosmos DB as well as request and response objects
- when an item is created its inserted into container and an ID for the item is returned
What is the JS callback function in regards to cosmosDB functions?
- required when using stored procedures
- has two params; one for error object in case operation fails and another for a return value
- can either handle the exception or throw an error
- if no call provided, error will be thrown In cosmos DB runtime
What other params does stored proc function have?
- a param to set description as a bool
- when true and the description is missing the proc will throw an exception
- otherwise the rest of the proc continues to run
What is special about defining a stored proc?
- input params are always sent as a string to the stored proc
- even if you pass an array of strings the array is converted into a string
- to get around this you can define a function within proc to parse the string as an array
What must all cosmos DB operations do?
- Complete within a limited amount of time
- stored procs have limited time to run on server
- collection functions return a boolean value that represents if operation completed or not
How can we implement transactions on items within a container?
- By using a stored proc
- JS functions can implement a continuation-based model to batch or resume execution
- continuation values can be any value of your choice and apps can use this value to resume transactions from a new starting point
What triggers does cosmosDB support?
- pre and post
- pretriggers are executed before modifying a DB item and post are executed after modifying an item
- not automatically executed, must be specified for each DB operation where you want to execute them
- Need to be registered after definition by using cosmos DB SDKs
Can pretriggers have input params?
- No
- request object in the trigger is used to manipulate the request message associated with the operation
How can we specify the operations triggers can be ran with?
- When triggers are registered
- e.g. TriggerOperation.Create which means using the trigger in a replace operation isn’t permitted but in a create operation it is
How does error handling work in triggers?
- an exception during the post-trigger execution fails the whole transaction
- anything committed is rolled back and an exception returned
What is a change feed?
- persistent record of changes to a container in the order they occur
- works by listening to a container for any changes
- it then outputs the sorted list of docs that were changed in the order in which they were modified
- persisted changes can be processed asynch and incrementally
- output can be distributed across one or more consumers for parallel processing
What cant a change feed do?
- doesnt log delete operatons
- can add a soft marker on the items that are being deleted
- such as an attrib ona deleted item that’s set to true if deleted
What is the push model with change feeds?
- change feed processor pushes work to client that has business logic for processing the work
- complexity in checking for work and storing state for last processed work is handled within change feed processor
What is the pull model with change feeds?
- client has to pull work from server
- client has business logic for processing work and storing state for last processed work, handling load balancing across multiple clients processing work in parallel and handling errors
Which change feed modal is most recomended?
- push so you dont have to worry about polling change feed for future changes, storing state and other pros
When would the pull change feed modal be used?
- reading changes from a particular partition key
- controlling pace at which client receives changes for processing
- doing a one time read of existing data in change feed (e.g. data migration)
What are AZ functions cosmos triggers?
- used in push model to read from change feed
- uses change feed processor behind scenes
- can create small reactive functions that auto trigger on each new event in cosmos DB change feed
- can use change feed processors scaling and reliable event detection functionality without need to maintain any worker infrastructure
What is the change feed processor? (CFP)
- part of azure cosmos DB .NET V3 and Java V4 SDKs
- simplifies the process of reading the change feed and distributes the event processing across multiple consumers effectively
What is the first component of CFP
- monitored container
- has the data from which the feed is generated
- any inserts or updates are reflected in the change feed of the container
- point of entry
what is the second component of CFP?
- lease container
- acts as a state storage and coordinates processing the change feed across multiple workers
- can be stored in same account as monitored collection or seperate
What is the third component of CFP?
- compute instance
- hosts the CFP to listen for changes
- could be VM, kubernetes pod, app service instance or physical machine
- has unique ID that should be different in each compute instance you are deploying
What is the fourth component of CFP?
- Delegate
- code that defines what the dev wants to do with each batch of changes that the CFP reads
What is the lifecycle of the host instance?
- read change feed
- if no changes sleep for predefined amount of time and go back to first step
- if there are changes send them to delegate
- when delegate finishes processing successfully, update the lease store with the latest processed point in time and go back to first point