Work with Azure Cosmos DB Flashcards

1
Q

What version of the cosmos SDK are we currently using?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What function creates a new cosmos client?

A

CosmosClient(endpoint, key)
- creates new cosmos client with the conn string
- thread safe
- only one instance recommended per lifetime

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What functions are available for databases?

A
  • CreateDatabaseIfNotExistsAsync(id, timeout); id only used to verify if theres an existing db
  • readAsync()
  • deleteAsync()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What functions are available for containers?

A
  • CreateContainerIfNotExistsAsync(id, partitionkeypath, throughput)
  • GetContainer(id)
  • ReadContainerAsync()
  • DeleteContainer()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What functions are available for items?

A
  • CreateItemAsync(id, partitionkey)
  • ReadItemAsync(id, partitionkey)
  • GetItemQueryIterator(); creates a query for items under a container using a SQL statement with parameterised values
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does cosmosDB provide in terms of functions?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

what are stored procedures in cosmosDB?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the JS callback function in regards to cosmosDB functions?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What other params does stored proc function have?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is special about defining a stored proc?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What must all cosmos DB operations do?

A
  • 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How can we implement transactions on items within a container?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What triggers does cosmosDB support?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Can pretriggers have input params?

A
  • No
  • request object in the trigger is used to manipulate the request message associated with the operation
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How can we specify the operations triggers can be ran with?

A
  • 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How does error handling work in triggers?

A
  • an exception during the post-trigger execution fails the whole transaction
  • anything committed is rolled back and an exception returned
17
Q

What is a change feed?

A
  • 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
18
Q

What cant a change feed do?

A
  • 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
19
Q

What is the push model with change feeds?

A
  • 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
20
Q

What is the pull model with change feeds?

A
  • 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
21
Q

Which change feed modal is most recomended?

A
  • push so you dont have to worry about polling change feed for future changes, storing state and other pros
22
Q

When would the pull change feed modal be used?

A
  • 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)
23
Q

What are AZ functions cosmos triggers?

A
  • 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
24
Q

What is the change feed processor? (CFP)

A
  • 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
25
Q

What is the first component of CFP

A
  • 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
26
Q

what is the second component of CFP?

A
  • 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
27
Q

What is the third component of CFP?

A
  • 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
28
Q

What is the fourth component of CFP?

A
  • Delegate
  • code that defines what the dev wants to do with each batch of changes that the CFP reads
29
Q

What is the lifecycle of the host instance?

A
  • 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