Data Subscriptions

In this post, I’m going to share with you one of the RavenDB 4 features that I like the most: Data Subscriptions.

Data Subscriptions?

It’s simpler to explain this concept with an example. Consider the following query:

from Orders 
where Lines.length > 5

This would retrieve all the big orders from your database. But, what if a new big order is added after you run this query. What if you want to be notified whenever a big order occurs? YEAH! THIS IS WHAT I WAS TALKING ABOUT.

Creating a Data Subscription

I assume that, at this point, you know how to open the Studio and the Northwind database. If this is not the case, I would recommend you to read this post where I teach you, step-by-step, the basics.

Do that! I will wait…

Done?!

To create a Data Subscription, you need to open the Settings section of your database, click on the Manage Ongoing Tasks option, then click on the Add Task button.

Then, click on Subscription. RavenDB will ask you a Task Name (let’s call it Big Orders) and a query. In this demo, I will provide the very same query we used before.


The user interface allows me to do a test before save (I did it!).

Subscribing the Data Subscription

Now, I will show you how to subscribe and consume a data subscription.

  • Data subscriptions are consumed by clients, called subscription workers.
  • In any given moment, only one worker can be connected to a data subscription.
  • A worker connected to a data subscription receives a batch of documents and gets to process it.
  • When it’s done (depends on the code that the client gave the worker, can take from seconds to hours), it informs the server about the progress and the server is ready to send the next batch.

Subscriptions are consumed by processing batches of documents received from the server.

static void Main(string[] args)
{
    var subscriptionWorker = DocumentStoreHolder.Store
        .Subscriptions
        .GetSubscriptionWorker("Big Orders");

    var subscriptionRuntimeTask = subscriptionWorker.Run(batch =>
    {
        foreach (var order in batch.Items)
        {
            // business logic here.
            Console.WriteLine(order.Id);
        }
    });

    WriteLine("Press any key to exit...");
    ReadKey();
}

How do Data Subscriptions work?

There are some important facts that you need to know to use this feature correctly.

  • Documents that match pre-defined criteria are sent in batches from the server to the clients
  • The client sends an acknowledgment to the server once it is done with processing the batch.
  • The server keeps track of the latest document that was acknowledged by the client so that processing can be continued from the latest acknowledged position if it was paused or interrupted.

It’s time to action

Pretty exciting feature. Am I right? So, please, share the scenarios where you would like to use it. I would be glad to help you if you need.

Compartilhe este insight:

Elemar Júnior

Sou fundador e CEO da EximiaCo e atuo como tech trusted advisor ajudando diversas empresas a gerar mais resultados através da tecnologia.

Elemar Júnior

Sou fundador e CEO da EximiaCo e atuo como tech trusted advisor ajudando diversas empresas a gerar mais resultados através da tecnologia.

Mais insights para o seu negócio

Veja mais alguns estudos e reflexões que podem gerar alguns insights para o seu negócio:

Há anos eu conheço e aceito a ideia de que devemos buscar melhoria contínua. Sei que é natural e aceitável...
Neste post, compartilho mais algumas ideias que tenho adotado, com êxito, em meus projetos envolvendo Microsserviços e que podem ajudar...
The example which motivated this post comes from the excellent book Designing Distributed Systems by Brendan Burns (co-founder of the...
07 de julho de 2016, aproximadamente 8:30 – Eu iria palestrar no TDC de São Paulo naquele dia. Aterrizamos em...
Our goal is to fill a two-dimensional array with 1’s. using BenchmarkDotNet.Attributes; using BenchmarkDotNet.Running; namespace ToArrays { public class Program...
O ano era 2001 ou 2002. Não lembro ao certo. Eu era um jovem programador, pai recente, tentando “encontrar meu...
Oferta de pré-venda!

Mentoria em
Arquitetura de Software

Práticas, padrões & técnicas para Arquitetura de Software, de maneira efetiva, com base em cenários reais para profissionais envolvidos no projeto e implantação de software.

× Precisa de ajuda?