Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

RavenDB 4.1 Features: Counting My Counters

DZone's Guide to

RavenDB 4.1 Features: Counting My Counters

Read this article in order to learn more about RavenDB 4.1 features such as counters, and read about what these counters do.

· Database Zone ·
Free Resource

RavenDB vs MongoDB: Which is Better? This White Paper compares the two leading NoSQL Document Databases on 9 features to find out which is the best solution for your next project.  

Image title

Documents are awesome. They allow you to model your data in a very natural way. At the same time, there are certain things that just don’t fit into the document model.

Consider the simple case of counting. This seems like it would be very obvious, right? However, you need to also consider concurrency and distribution. Look at the image above. What you see there is a document describing a software release. In addition to tracking the features that are going into the release, we also want to count various statistics about the release. In this example, you can see how many times a release was downloaded, how many times it was rated, etc.

I’ll admit that the stars rating is a bit cheesy, but it looks good and actually tests that we have good Unicode support.

Except for a slightly nicer way to show numbers on the screen, what does this feature give you? It means that RavenDB now natively understands how to count things. This means that you can increment (or decrement) a value without modifying the whole document. It also means that RavenDB will be able to automatically handle concurrency on the counters, even when running in a distributed system. This makes the feature suitable for cases where you:

  • want to increment a value
  • don’t care (and usually explicitly desire) concurrency
  • may need to handle a very large number of operations

The case of the download counter or the rating votes is a classic example. Two separate clients may increment either of these values at the same time a third user is modifying the parent document. All of that is handled by RavenDB, and the data is updated, distributed across the cluster, and the final counter values are tallied.

Counters cannot cause conflicts and the only operation that you are allowed to do to them is to increment/decrement the counter value. This is a cumulative operation, which means that we can easily handle concurrency at the local node or cluster level by merging the values.

Other operations (deleting a counter, deleting the parent document) are of course noncumulative, but are much rarer and don’t typically need any sort of cooperative concurrency.

Counters are not standalone values but are strongly associated with their owning document. Much like the attachments feature, this means that you have a structured way to add additional data types to your documents. Use counters to, well… count. Use attachments to store binary data, etc. You are going to see a lot more of this in the future since there are a few things in the pipeline that we are already planning to add.

You can use counters as a single operation (incrementing a value) or in a batch (incrementing multiple values, or even modifying counters and documents together). In all cases, the operation is transactional and will ensure full ACIDity.

Do you pay to use your database? What if your database paid you? Learn more with RavenDB.

Topics:
database ,ravendb

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}