Optimistic Concurrency in DocumentDB

Optimistic Concurrency in DocumentDBI’ve started working on new side project using ASP.NET Core.  I wanted to try a new datastore and decided to give Azure’s DocumentDB a try.

This project isn’t doing anything complicated but does contain enough real world use cases that can give me an idea of how the API works.

Concurrency

The first thing I needed to implement was how to handle concurrency.  Specifically optimistic concurrency.

In an optimistic concurrency model, a violation is considered to have occurred if, after a user receives a value from the database, another user modifies the value before the first user has attempted to modify it.

This is pretty typical when dealing with multi user environments like a web application.  Specifically in my case is:

  • Fetching out a document from DocumentDB
  • Mutating the data of the document
  • Sending the document back to to DocumentDB to be replaced

In my web application, the concurrency issue arises if the same document is being modified by multiple users at the same time.

Without having any type of concurrency, we are what is called a “Last Wins” mode.  Meaning, the last process/user that sends the document back to DocumentDB is what will be persisted.

ETags

Each document within DocumentDB has an ETag Property.

The ETag or entity tag is part of HTTP, the protocol for the World Wide Web. It is one of several mechanisms that HTTP provides for web cache validation, which allows a client to make conditional requests.

You may be familiar with ETag’s related caching.  A typical scenario is a user makes an HTTP request to the server for a specific resource.  The server will return the response along with an ETag in the response header.  The client then caches the response along with the associated ETag.

ETag: "686897696a7c876b7e"

If they client then makes another request to the same resource, it will pass a If-Non-Match header with the ETag it received.

If-None-Match: "686897696a7c876b7e"

If the resource has not changed and the ETag represents the current version, then the server will return a 304 Not modified status.  If the resource has been modified, it will return the appropriate 2XX status code along with the content new ETag header.

AccessCondition

DocumentDB uses ETags for handling optimistic concurrency.  When we retrieve a document from DocumentDB, it always contains an ETag property as apart of our document.

When we then want to send our request to replace a document, we can specify an AccessCondition with the ETag we received when we fetched out our document.

If the ETag we send is not current, the server will return a 412 Precondition Failed status code.  In our .NET SDK, this is wrapped up in a DocumentClientException.

Here is a full an example.

Demo Source Code

I’ve put together a small .NET Core sample with an XUnit test from above. All the source code for this series is available on GitHub.

Are you using DocumentDB? I’d love to hear your experiences so far along. Let me know on twitter or in the comments.


 

Collaborating with other User Groups

Collaborating with other User GroupsA few months ago I decided that collaborating with other user groups might be a really good way to add new ideas and an outside perspective on the local .NET Developers Group that I organize.

Back in November 2016 I was attending Detroit Dev Day.  I was planning on meeting with with Reid Evans who was doing a talk “A Developer’s Journey From OO to FP”.

Like most conferences, the best part was the hallway discussions.  I was sitting around and talking about user groups with Reid, Ken Cenerelli and Tom Walker.

Tom mentioned that he had recently done meetup with a presentation done by a remote speaker.

Remote Speaker

The night after the conference, I was thinking about the conversations and realized that Reid’s talk which touched on F# would probably be a really good for for my Windsor-Essex .NET Developers group.

Our group tends to focus on C# however there always seems to be interesting when the conversation revolves around F# or functional programming.

Turns out Reid was interested in doing it.  He also suggested in return that I do a talk for his Functional Knox group.

Part 1

Since our  initial conversation many months ago, we worked out a date. Reid did a Getting Started with Functional Programming in F# which was fantastic.

I’ve been given great feedback from our group about how the experience was.  Everyone enjoyed the talk and we really had no issues.

The session was done using Google Hangouts On Air with YouTube Live.  Our user group watched the talk on TV’s with great quality.

Next month I’ll be returning the favor and doing a talk for the Functional Knox group.  I will update this blog post with more details after it happens.

Collaboration

This has spawned some interesting ideas in terms of having user groups collaborate by occasionally having remote speakers.

If you run a user group and are interested in possibly collaborating in a similar way please let me know!  Also if you have any suggestions about what your user groups have done with success (or failure) please leave a comment or let me know on Twitter.


Getting Started with Functional Programming in F#

Functional Programming in F#At last night’s Windsor-Essex .NET Developers Group, we had Reid Evans presenting a talk on Getting Started with Functional Programming in F#.

I absolutely loved this talk and wanted to share it along with some of my thoughts and takeaways.

I’ve been over the last year or two going back and forth with learning F# and understanding more functional programming concepts.  So I was really looking forward to this talk.

Video

This is the live stream we had to our user group.  If you are interested in functional programming and F#, I highly recommend giving this a watch.

 

Takeaways

I thought Reid’s intended takeaways were all met.  I came out of that talk with exactly what he intended.  As a speaker, I think this is the ultimate goal.  What you are trying to convey is achieved.

That’s not to say people can find other takeaways, but knowing what you are trying to present and is received is great.

Defaults matter & No more null!

I think the hardest idea to fully “get” is how defaults matter.  I think this is really expressed when you see the difference of not being able to have null.

The alternative of using the Option Type (a Union Type) as an alternative way of thinking about the problem of something not existing.  In Reid’s example, a record not existing in the database.

What I love about is how you handle the resulting Option Type with pattern matching.  What I take from this is forcing the caller to property handle either Some or None.

I think Reid said it really well that if your language allows null, you must check for null everywhere.  If it does not allow null, and uses something like an Option type, then that is very explicit.

Type Providers == Awesome

They are truly awesome.  It was really interesting to see how a type from a column of a database could be then be used to infer the argument of a method.  This is kind of mind blowing.

I can see the value this would provide a developer in terms of not making simple accidental mistakes.  Plus having a built in integration test at compile time is just fantastic.

Composing Small Functions

Although the idea of writing small classes and methods are familiar to you in C#, I think the concept of you use those functions in a functional language is what differs.

I loved the example of how the validation was tied together as it gives a great real world example.  As with everything I still need more understanding on this and where this can take you.

Other Thoughts

What I loved about this talk is it used real world simple examples of things you would do building an app like data access and validation.

It was very easy to transfer the content into something you could relate to.  I think the fact that Reid often went down the road of making the comparisons to something I was already familiar with was a great help.

If you watched the video, I’d love to hear your comments.   Reid would probably love feedback as well.  Please post a comments or on Twitter.