How to speed up Azure Digital Twins Queries with Caching Strategy

Published Jul 20 2021 08:00 AM 1,633 Views
Microsoft

If you are developing applications with Azure Digital Twins (ADT) which will materialize large twin graphs, this article is for you. Read on to learn how we accelerated services that need to traverse these graphs and how you can do the same using the right caching strategy.

 

As part of the CSE global engineering organization at Microsoft, our team developed an ADT-based solution together with a customer.
An essential requirement was to have low-latency responses for materializing graphs of several thousands of nodes which are infrequently updated. We achieved this goal by improving the speed of a 3000 nodes graph traversal from ~10 seconds to under a second.

 

ADT offers a powerful SQL-like query language to retrieve data out of a twin graph.
Traversing large graph sections implies the execution of many ADT-queries. This blog post presents an in-memory caching solution that we utilized to enhance the performance of twin graph traversals.

 

Prerequisites

 

 

Create your Azure Digital Twins graph

 

We want to represent the factories of a company named Contoso.

  • Create an Azure Digital Twins instance and make sure you have the Azure Digital Twins Data Owner role.
  • Open the Azure Digital Twins Explorer.
  • Download the contoso-tree.zip provided in the attachments and import the contoso-tree.json to ADT and save it. You can select the import graph icon in the explorer and select the file to import it. Then save the graph.

hierarchy-blogpost-import-graph.png 

 

You should see the following tree in the explorer.

 

hierarchy-blogpost-contoso-tree.png

 

The Azure Digital Twins Explorer shows that our Contoso company has two factories. Each factory is composed of rooms, and each room can contain machines.

 

Get the children of a twin

 

A common use case is the need to retrieve the children of a twin. For

example, we want to be able to list the rooms of a factory.

You can display the children of Factory1 by running the following query:

 

 

SELECT C FROM DigitalTwins T JOIN C RELATED T.contains WHERE T.$dtId = 'Factory1'

 

 

 

You should see the 2 following twins.

 

hierarchy-blogpost-children.png

 

The Azure Digital Twins Explorer helps you visualizing the twins. If we develop an application, we need to be able to retrieve the twins programmatically. Let’s try to retrieve the children of a node with C#.

You can start by creating a new Console Application project and include the packages Azure.DigitalTwins.Core, System.Linq.Async, Azure.Identity.

 

 

dotnet new console -lang "C#" -f netcoreapp3.1
dotnet add package Azure.DigitalTwins.Core
dotnet add package System.Linq.Async
dotnet add package Azure.Identity

 

 

 

Then we can create a simple AzureDigitalTwinsRepository class. It will use the DigitalTwinsClient to query ADT.

 

 

namespace AzureDigitalTwinsCachingExample
{
    using System.Collections.Generic;
    using Azure.DigitalTwins.Core;
    using System.Linq;
    using System.Threading.Tasks;

    public class AzureDigitalTwinsRepository
    {
        private readonly DigitalTwinsClient _client;

        public AzureDigitalTwinsRepository(DigitalTwinsClient client)
        {
            _client = client;
        }
    }
}

 

 

 

Add a method to get the children of a twin in the AzureDigitalTwinsRepository class.

 

 

public IAsyncEnumerable<BasicDigitalTwin> GetChildren(string id)
{
    return _client.QueryAsync<IDictionary<string, BasicDigitalTwin>>
            ($"SELECT C FROM DigitalTwins T JOIN C RELATED T.contains WHERE T.$dtId = '{id}'")
        .Select(_ => _["C"]);
}

 

 

 

We can use our AzureDigitalTwinsRepository to display the children of Factory1:

 

 

namespace AzureDigitalTwinsCachingExample
{
    using System;
    using System.Threading.Tasks;
    using Azure.DigitalTwins.Core;
    using Azure.Identity;

    class Program
    {
        public static async Task Main(string[] args)
        {
            var adtInstanceUrl = "https://<your-adt-hostname>";
            var credential = new DefaultAzureCredential();
            var client = new DigitalTwinsClient(new Uri(adtInstanceUrl), credential);
            // First call to avoid cold start in next steps.
            _ = client.GetDigitalTwin<BasicDigitalTwin>("ContosoCompany");
            var adtRepository = new AzureDigitalTwinsRepository(client);

            var children = adtRepository.GetChildren("Factory1");
            await foreach (var child in children)
            {
                Console.Write($"{child.Id} ");
            }
        }
    }
}

 

 

 

Get the subtree of a twin

 

Imagine that we need our application to retrieve the subtree of a twin. We want to get the twin, its descendants and the relationships between these twins. We cannot achieve that with a single ADT Query. We must make a tree traversal like a breadth-first search for example.

Add a method to get the subtree of a node in the AzureDigitalTwinsRepository class:

 

 

public async Task<IDictionary<string, (BasicDigitalTwin twin, HashSet<BasicDigitalTwin> children)>> GetSubtreeAsync(string sourceId)
{
    var queue = new Queue<BasicDigitalTwin>();
    var subtree = new Dictionary<string, (BasicDigitalTwin twin, HashSet<BasicDigitalTwin> children)>();
    var sourceTwin = await _client.GetDigitalTwinAsync<BasicDigitalTwin>(sourceId);
    subtree[sourceId] = (sourceTwin, new HashSet<BasicDigitalTwin>());
    queue.Enqueue(sourceTwin);

    while (queue.Any())
    {
        var twin = queue.Dequeue();
        var children = GetChildren(twin.Id);
        await foreach (var child in children)
        {
            subtree[twin.Id].children.Add(child);
            if (subtree.ContainsKey(child.Id)) continue;
            queue.Enqueue(child);
            subtree[child.Id] = (child, new HashSet<BasicDigitalTwin>());
        }
    }

    return subtree;
}

 

 

 

When traversing the tree, we make several consecutive queries to ADT which makes the entire operation longer. To make the operation faster, let's see how we can cache the tree in-memory.

 

Caching

 

A secondary datastore can serve as a data cache to accelerate application operations while avoiding the need to query ADT multiple times in complex operations.
We decided to implement a simple in-memory cache as the data we were interested in was small enough to load in-memory and is infrequently updated. This enabled us to avoid adding additional infrastructure complexity with a relatively simple caching approach.

The cache must contain a subset of the twin graph transformed into a data structure appropriate for the problem at hand. Depending on the use case, it might be necessary to store data as a subgraph of the twin graph. Still, there might be other situations where simpler data structures like lists or maps simplify the cache implementation. We used a simple in-memory adjacency list.

blogpost-adt-cache.png

We want to store the Contoso tree in-memory as an adjacency-list.

Let's create a caching repository. The caching repository uses the AzureDigitalTwinsRepository that we implemented to reload the cache.

 

 

namespace AzureDigitalTwinsCachingExample
{
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Azure.DigitalTwins.Core;

    public class CachingRepository
    {
        private readonly AzureDigitalTwinsRepository _adtRepository;
        private IDictionary<string, (BasicDigitalTwin twin, HashSet<BasicDigitalTwin> children)> _graph;

        public CachingRepository(AzureDigitalTwinsRepository adtRepository)
        {
            _adtRepository = adtRepository;
        }

        public async Task ReloadCache()
        {
            // Reload the tree from the root.
            _graph = await _adtRepository.GetSubtreeAsync("ContosoCompany");
        }
    }
}

 

 

 

We can add a GetSubtree method that will traverse the in-memory graph instead of making several requests to ADT. The only difference with the previous implementation is that we get the digital twin and its children from the in-memory graph.

 

 

public IDictionary<string, (BasicDigitalTwin twin, HashSet<BasicDigitalTwin> children)> GetSubtree(string sourceId)
{
    var queue = new Queue<BasicDigitalTwin>();
    var subtree = new Dictionary<string, (BasicDigitalTwin twin, HashSet<BasicDigitalTwin> children)>();
    var sourceTwin = _graph[sourceId].twin;
    subtree[sourceId] = (sourceTwin, new HashSet<BasicDigitalTwin>());
    queue.Enqueue(sourceTwin);

    while (queue.Any())
    {
        var twin = queue.Dequeue();
        var children = _graph[twin.Id].children;
        foreach (var child in children)
        {
            subtree[twin.Id].children.Add(child);
            if (subtree.ContainsKey(child.Id)) continue;
            queue.Enqueue(child);
            subtree[child.Id] = (child, new HashSet<BasicDigitalTwin>());
        }
    }

    return subtree;
}

 

 

 

We can measure the duration of the 2 GetSubtree implementations.

 

 

namespace AzureDigitalTwinsCachingExample
{
    using System;
    using System.Diagnostics;
    using System.Linq;
    using System.Threading.Tasks;
    using Azure.DigitalTwins.Core;
    using Azure.Identity;

    class Program
    {
        public static async Task Main(string[] args)
        {
            var adtInstanceUrl = "https://<your-adt-hostname>";

            // Authenticate and create a client
            var credential = new DefaultAzureCredential();
            var client = new DigitalTwinsClient(new Uri(adtInstanceUrl), credential);
            // First call to avoid cold start in next steps.
            _ = client.GetDigitalTwin<BasicDigitalTwin>("ContosoCompany");

            var adtRepository = new AzureDigitalTwinsRepository(client);
            var cachingRepository = new CachingRepository(adtRepository);
            // Reloading the cache takes some time.
            await cachingRepository.ReloadCache();

            var stopwatch = Stopwatch.StartNew();
            var subtree = await adtRepository.GetSubtreeAsync("Factory1");
            stopwatch.Stop();
            Console.WriteLine($"Got subtree with {subtree.Count()} nodes in {stopwatch.ElapsedMilliseconds} ms");

            stopwatch.Restart();
            var subtreeFromCache = cachingRepository.GetSubtree("Factory1");
            stopwatch.Stop();
            Console.WriteLine(
                $"Got subtree with {subtreeFromCache.Count()} nodes in cache in {stopwatch.ElapsedMilliseconds} ms");
        }
    }
}

 

 

 

Cache loading and invalidation

 

You can preload the cache when the service starts and invalidate it when the graph is updated.
Event notifications can be a great trigger for that. Azure Digital Twins provides different type of events.
We wanted to avoid additional dependencies, so we created an extra twin in ADT and used it as an indicator to keep track of the last graph updates. The twin indicator is updated by the service whenever it modifies the graph in ADT. Then, our service periodically checks if the indicator twin got updated and refreshes the cache if it is the case.

 

Conclusion

 

Azure Digital Twins is a powerful tool to create a digital representation of an environment, and we have seen how caching can be used to enhance the performance of twin graph traversals.
An additional advantage is ADT cost optimization. ADT pricing includes a cost per query unit. Using a cache may help you reduce the number of query units used by your system. However, reloading the cache also consumes query units. The amount you can save depends on how expensive all the operations you avoid to compute are, but the cost to refresh the cache also impacts the number of query units used. That's why you need to make your own analysis to understand what is best for your system and how this type of strategy would help your case.

Now you can try this strategy and share your experience and workarounds in the comments below!

 

Contributors


Marc Gomez
Alexandre Gattiker
Christopher Lomonico
Izabela Kulakowska
Max Zeier
Peeyush Chandel

 

%3CLINGO-SUB%20id%3D%22lingo-sub-2551153%22%20slang%3D%22en-US%22%3EHow%20to%20speed%20up%20Azure%20Digital%20Twins%20Queries%20with%20Caching%20Strategy%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2551153%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%3EIf%20you%20are%20developing%20applications%20with%20%3CA%20href%3D%22https%3A%2F%2Fazure.microsoft.com%2Fservices%2Fdigital-twins%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EAzure%20Digital%20Twins%3C%2FA%3E%20(ADT)%20which%20will%20materialize%20large%20twin%20graphs%2C%20this%20article%20is%20for%20you.%20Read%20on%20to%20learn%20how%20we%20accelerated%20services%20that%20need%20to%20traverse%20these%20graphs%20and%20how%20you%20can%20do%20the%20same%20using%20the%20right%20caching%20strategy.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EAs%20part%20of%20the%20%3CA%20href%3D%22https%3A%2F%2Fdevblogs.microsoft.com%2Fcse%2Fbtc%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3ECSE%3C%2FA%3E%20global%20engineering%20organization%20at%20Microsoft%2C%20our%20%3CA%20href%3D%22%23toc-hId--1984531400%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Eteam%3C%2FA%3E%20developed%20an%20ADT-based%20solution%20together%20with%20a%20customer.%3CBR%20%2F%3EAn%20essential%20requirement%20was%20to%20have%20low-latency%20responses%20for%20materializing%20graphs%20of%20several%20thousands%20of%20nodes%20which%20are%20infrequently%20updated.%20We%20achieved%20this%20goal%20by%20improving%20the%20speed%20of%20a%203000%20nodes%20graph%20traversal%20from%20~10%20seconds%20to%20under%20a%20second.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EADT%20offers%20a%20powerful%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fdigital-twins%2Fconcepts-query-language%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3ESQL-like%20query%20language%3C%2FA%3E%20to%20retrieve%20data%20out%20of%20a%20twin%20graph.%3CBR%20%2F%3ETraversing%20large%20graph%20sections%20implies%20the%20execution%20of%20many%20ADT-queries.%20This%20blog%20post%20presents%20an%20in-memory%20caching%20solution%20that%20we%20utilized%20to%20enhance%20the%20performance%20of%20twin%20graph%20traversals.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--409790821%22%20id%3D%22toc-hId--409790829%22%3E%3CSPAN%3EPrerequisites%3C%2FSPAN%3E%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CSPAN%3E.NET%20Core%203.1%20on%20your%20development%20machine.%3C%2FSPAN%3E%3C%2FLI%3E%0A%3CLI%3E%3CSPAN%3EBe%20familiar%20with%20C%23%2C%20Azure%20Digital%20Twins%20and%20%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fdigital-twins%2Fquickstart-azure-digital-twins-explorer%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3E%3CSPAN%3EAzure%20Digital%20Twins%20Explorer%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3E.%3C%2FSPAN%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-2077722012%22%20id%3D%22toc-hId-2077722004%22%3E%3CSPAN%3ECreate%20your%20Azure%20Digital%20Twins%20graph%3C%2FSPAN%3E%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EWe%20want%20to%20represent%20the%20factories%20of%20a%20company%20named%20Contoso.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fdigital-twins%2Fhow-to-set-up-instance-portal%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3ECreate%20an%20Azure%20Digital%20Twins%20instance%3C%2FA%3E%20and%20make%20sure%20you%20have%20the%20Azure%20Digital%20Twins%20Data%20Owner%20role.%3C%2FSPAN%3E%3C%2FLI%3E%0A%3CLI%3E%3CSPAN%3EOpen%20the%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fdigital-twins%2Fquickstart-azure-digital-twins-explorer%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EAzure%20Digital%20Twins%20Explorer%3C%2FA%3E.%3C%2FSPAN%3E%3C%2FLI%3E%0A%3CLI%3E%3CSPAN%3EDownload%20the%20contoso-tree.zip%20provided%20in%20the%20attachments%20and%20import%20the%20contoso-tree.json%20to%20ADT%20and%20save%20it.%20You%20can%20select%20the%20import%20graph%20icon%20in%20the%20explorer%20and%20select%20the%20file%20to%20import%20it.%20Then%20save%20the%20graph.%3C%2FSPAN%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%3CSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22hierarchy-blogpost-import-graph.png%22%20style%3D%22width%3A%20200px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F296040i28776D7BBF51DA9B%2Fimage-size%2Fsmall%3Fv%3Dv2%26amp%3Bpx%3D200%22%20role%3D%22button%22%20title%3D%22hierarchy-blogpost-import-graph.png%22%20alt%3D%22hierarchy-blogpost-import-graph.png%22%20%2F%3E%3C%2FSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EYou%20should%20see%20the%20following%20tree%20in%20the%20explorer.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22hierarchy-blogpost-contoso-tree.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F296041iB1A1B88A64291269%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22hierarchy-blogpost-contoso-tree.png%22%20alt%3D%22hierarchy-blogpost-contoso-tree.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20Azure%20Digital%20Twins%20Explorer%20shows%20that%20our%20Contoso%20company%20has%20two%20factories.%20Each%20factory%20is%20composed%20of%20rooms%2C%20and%20each%20room%20can%20contain%20machines.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-270267549%22%20id%3D%22toc-hId-270267541%22%3E%3CSPAN%3EGet%20the%20children%20of%20a%20twin%3C%2FSPAN%3E%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EA%20common%20use%20case%20is%20the%20need%20to%20retrieve%20the%20children%20of%20a%20twin.%20For%20%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3Eexample%2C%20we%20want%20to%20be%20able%20to%20list%20the%20rooms%20of%20a%20factory.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EYou%20can%20display%20the%20children%20of%20%3C%2FSPAN%3E%3CSPAN%3EFactory1%3C%2FSPAN%3E%3CSPAN%3E%20by%20running%20the%20following%20query%3A%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3ESELECT%20C%20FROM%20DigitalTwins%20T%20JOIN%20C%20RELATED%20T.contains%20WHERE%20T.%24dtId%20%3D%20'Factory1'%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EYou%20should%20see%20the%202%20following%20twins.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22hierarchy-blogpost-children.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F296042i9A37CF9A0828C731%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22hierarchy-blogpost-children.png%22%20alt%3D%22hierarchy-blogpost-children.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EThe%20Azure%20Digital%20Twins%20Explorer%20helps%20you%20visualizing%20the%20twins.%20If%20we%20develop%20an%20application%2C%20we%20need%20to%20be%20able%20to%20retrieve%20the%20twins%20programmatically.%20Let%E2%80%99s%20try%20to%20retrieve%20the%20children%20of%20a%20node%20with%20C%23.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EYou%20can%20start%20by%20creating%20a%20new%20Console%20Application%20project%20and%20include%20the%20packages%20Azure.DigitalTwins.Core%2C%20System.Linq.Async%2C%20Azure.Identity.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-bash%22%3E%3CCODE%3Edotnet%20new%20console%20-lang%20%22C%23%22%20-f%20netcoreapp3.1%0Adotnet%20add%20package%20Azure.DigitalTwins.Core%0Adotnet%20add%20package%20System.Linq.Async%0Adotnet%20add%20package%20Azure.Identity%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22FirstParagraph%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22FirstParagraph%22%3E%3CSPAN%3EThen%20we%20can%20create%20a%20simple%20AzureDigitalTwinsRepository%20class.%20It%20will%20use%20the%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fdotnet%2Fapi%2Fazure.digitaltwins.core.digitaltwinsclient%3Fview%3Dazure-dotnet%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EDigitalTwinsClient%3C%2FA%3E%20to%20query%20ADT.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Enamespace%20AzureDigitalTwinsCachingExample%0A%7B%0A%20%20%20%20using%20System.Collections.Generic%3B%0A%20%20%20%20using%20Azure.DigitalTwins.Core%3B%0A%20%20%20%20using%20System.Linq%3B%0A%20%20%20%20using%20System.Threading.Tasks%3B%0A%0A%20%20%20%20public%20class%20AzureDigitalTwinsRepository%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20private%20readonly%20DigitalTwinsClient%20_client%3B%0A%0A%20%20%20%20%20%20%20%20public%20AzureDigitalTwinsRepository(DigitalTwinsClient%20client)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20_client%20%3D%20client%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22FirstParagraph%22%3E%3CSPAN%3EAdd%20a%20method%20to%20get%20the%20children%20of%20a%20twin%20in%20the%20AzureDigitalTwinsRepository%20class.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Epublic%20IAsyncEnumerable%3CBASICDIGITALTWIN%3E%20GetChildren(string%20id)%0A%7B%0A%20%20%20%20return%20_client.QueryAsync%3CIDICTIONARY%3E%3CSTRING%3E%26gt%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%24%22SELECT%20C%20FROM%20DigitalTwins%20T%20JOIN%20C%20RELATED%20T.contains%20WHERE%20T.%24dtId%20%3D%20'%7Bid%7D'%22)%0A%20%20%20%20%20%20%20%20.Select(_%20%3D%26gt%3B%20_%5B%22C%22%5D)%3B%0A%7D%0A%3C%2FSTRING%3E%3C%2FIDICTIONARY%3E%3C%2FBASICDIGITALTWIN%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22FirstParagraph%22%3E%3CSPAN%3EWe%20can%20use%20our%20AzureDigitalTwinsRepository%20to%20display%20the%20children%20of%20Factory1%3A%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Enamespace%20AzureDigitalTwinsCachingExample%0A%7B%0A%20%20%20%20using%20System%3B%0A%20%20%20%20using%20System.Threading.Tasks%3B%0A%20%20%20%20using%20Azure.DigitalTwins.Core%3B%0A%20%20%20%20using%20Azure.Identity%3B%0A%0A%20%20%20%20class%20Program%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20public%20static%20async%20Task%20Main(string%5B%5D%20args)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20adtInstanceUrl%20%3D%20%22https%3A%2F%2F%3CYOUR-ADT-HOSTNAME%3E%22%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20credential%20%3D%20new%20DefaultAzureCredential()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20client%20%3D%20new%20DigitalTwinsClient(new%20Uri(adtInstanceUrl)%2C%20credential)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20First%20call%20to%20avoid%20cold%20start%20in%20next%20steps.%0A%20%20%20%20%20%20%20%20%20%20%20%20_%20%3D%20client.GetDigitalTwin%3CBASICDIGITALTWIN%3E(%22ContosoCompany%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20adtRepository%20%3D%20new%20AzureDigitalTwinsRepository(client)%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20children%20%3D%20adtRepository.GetChildren(%22Factory1%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20await%20foreach%20(var%20child%20in%20children)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Console.Write(%24%22%7Bchild.Id%7D%20%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D%0A%3C%2FBASICDIGITALTWIN%3E%3C%2FYOUR-ADT-HOSTNAME%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--1537186914%22%20id%3D%22toc-hId--1537186922%22%3E%3CSPAN%3EGet%20the%20subtree%20of%20a%20twin%3C%2FSPAN%3E%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22FirstParagraph%22%3E%3CSPAN%3EImagine%20that%20we%20need%20our%20application%20to%20retrieve%20the%20subtree%20of%20a%20twin.%20We%20want%20to%20get%20the%20twin%2C%20its%20descendants%20and%20the%20relationships%20between%20these%20twins.%20We%20cannot%20achieve%20that%20with%20a%20single%20ADT%20Query.%20We%20must%20make%20a%20tree%20traversal%20like%20a%20breadth-first%20search%20for%20example.%3CBR%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20class%3D%22FirstParagraph%22%3E%3CSPAN%3EAdd%20a%20method%20to%20get%20the%20subtree%20of%20a%20node%20in%20the%20AzureDigitalTwinsRepository%20class%3A%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Epublic%20async%20Task%3CIDICTIONARY%3E%3CSTRING%3E%20children)%26gt%3B%26gt%3B%20GetSubtreeAsync(string%20sourceId)%0A%7B%0A%20%20%20%20var%20queue%20%3D%20new%20Queue%3CBASICDIGITALTWIN%3E()%3B%0A%20%20%20%20var%20subtree%20%3D%20new%20Dictionary%3CSTRING%3E%20children)%26gt%3B()%3B%0A%20%20%20%20var%20sourceTwin%20%3D%20await%20_client.GetDigitalTwinAsync%3CBASICDIGITALTWIN%3E(sourceId)%3B%0A%20%20%20%20subtree%5BsourceId%5D%20%3D%20(sourceTwin%2C%20new%20HashSet%3CBASICDIGITALTWIN%3E())%3B%0A%20%20%20%20queue.Enqueue(sourceTwin)%3B%0A%0A%20%20%20%20while%20(queue.Any())%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20var%20twin%20%3D%20queue.Dequeue()%3B%0A%20%20%20%20%20%20%20%20var%20children%20%3D%20GetChildren(twin.Id)%3B%0A%20%20%20%20%20%20%20%20await%20foreach%20(var%20child%20in%20children)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20subtree%5Btwin.Id%5D.children.Add(child)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(subtree.ContainsKey(child.Id))%20continue%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20queue.Enqueue(child)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20subtree%5Bchild.Id%5D%20%3D%20(child%2C%20new%20HashSet%3CBASICDIGITALTWIN%3E())%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%0A%20%20%20%20return%20subtree%3B%0A%7D%3C%2FBASICDIGITALTWIN%3E%3C%2FBASICDIGITALTWIN%3E%3C%2FBASICDIGITALTWIN%3E%3C%2FSTRING%3E%3C%2FBASICDIGITALTWIN%3E%3C%2FSTRING%3E%3C%2FIDICTIONARY%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22FirstParagraph%22%3E%3CSPAN%3EWhen%20traversing%20the%20tree%2C%20we%20make%20several%20consecutive%20queries%20to%20ADT%20which%20makes%20the%20entire%20operation%20longer.%20To%20make%20the%20operation%20faster%2C%20let's%20see%20how%20we%20can%20cache%20the%20tree%20in-memory.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20class%3D%22FirstParagraph%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-950325919%22%20id%3D%22toc-hId-950325911%22%3E%3CSPAN%3ECaching%3C%2FSPAN%3E%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EA%20secondary%20datastore%20can%20serve%20as%20a%20data%20cache%20to%20accelerate%20application%20operations%20while%20avoiding%20the%20need%20to%20query%20ADT%20multiple%20times%20in%20complex%20operations.%3CBR%20%2F%3EWe%20decided%20to%20implement%20a%20simple%20in-memory%20cache%20as%20the%20data%20we%20were%20interested%20in%20was%20small%20enough%20to%20load%20in-memory%20and%20is%20infrequently%20updated.%20This%20enabled%20us%20to%20avoid%20adding%20additional%20infrastructure%20complexity%20with%20a%20relatively%20simple%20caching%20approach.%20%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EThe%20cache%20must%20contain%20a%20subset%20of%20the%20twin%20graph%20transformed%20into%20a%20data%20structure%20appropriate%20for%20the%20problem%20at%20hand.%20Depending%20on%20the%20use%20case%2C%20it%20might%20be%20necessary%20to%20store%20data%20as%20a%20subgraph%20of%20the%20twin%20graph.%20Still%2C%20there%20might%20be%20other%20situations%20where%20simpler%20data%20structures%20like%20lists%20or%20maps%20simplify%20the%20cache%20implementation.%20We%20used%20a%20simple%20in-memory%20adjacency%20list.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22blogpost-adt-cache.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F296102iE9D97BB9B8BABD7D%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22blogpost-adt-cache.png%22%20alt%3D%22blogpost-adt-cache.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EWe%20want%20to%20store%20the%20Contoso%20tree%20in-memory%20as%20an%20adjacency-list.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3ELet's%20create%20a%20caching%20repository.%20The%20caching%20repository%20uses%20the%20AzureDigitalTwinsRepository%20that%20we%20implemented%20to%20reload%20the%20cache.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Enamespace%20AzureDigitalTwinsCachingExample%0A%7B%0A%20%20%20%20using%20System.Collections.Generic%3B%0A%20%20%20%20using%20System.Linq%3B%0A%20%20%20%20using%20System.Threading.Tasks%3B%0A%20%20%20%20using%20Azure.DigitalTwins.Core%3B%0A%0A%20%20%20%20public%20class%20CachingRepository%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20private%20readonly%20AzureDigitalTwinsRepository%20_adtRepository%3B%0A%20%20%20%20%20%20%20%20private%20IDictionary%3CSTRING%3E%20children)%26gt%3B%20_graph%3B%0A%0A%20%20%20%20%20%20%20%20public%20CachingRepository(AzureDigitalTwinsRepository%20adtRepository)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20_adtRepository%20%3D%20adtRepository%3B%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20public%20async%20Task%20ReloadCache()%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Reload%20the%20tree%20from%20the%20root.%0A%20%20%20%20%20%20%20%20%20%20%20%20_graph%20%3D%20await%20_adtRepository.GetSubtreeAsync(%22ContosoCompany%22)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D%3C%2FSTRING%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22FirstParagraph%22%3E%3CSPAN%3EWe%20can%20add%20a%20GetSubtree%20method%20that%20will%20traverse%20the%20in-memory%20graph%20instead%20of%20making%20several%20requests%20to%20ADT.%20The%20only%20difference%20with%20the%20previous%20implementation%20is%20that%20we%20get%20the%20digital%20twin%20and%20its%20children%20from%20the%20in-memory%20graph.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Epublic%20IDictionary%3CSTRING%3E%20children)%26gt%3B%20GetSubtree(string%20sourceId)%0A%7B%0A%20%20%20%20var%20queue%20%3D%20new%20Queue%3CBASICDIGITALTWIN%3E()%3B%0A%20%20%20%20var%20subtree%20%3D%20new%20Dictionary%3CSTRING%3E%20children)%26gt%3B()%3B%0A%20%20%20%20var%20sourceTwin%20%3D%20_graph%5BsourceId%5D.twin%3B%0A%20%20%20%20subtree%5BsourceId%5D%20%3D%20(sourceTwin%2C%20new%20HashSet%3CBASICDIGITALTWIN%3E())%3B%0A%20%20%20%20queue.Enqueue(sourceTwin)%3B%0A%0A%20%20%20%20while%20(queue.Any())%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20var%20twin%20%3D%20queue.Dequeue()%3B%0A%20%20%20%20%20%20%20%20var%20children%20%3D%20_graph%5Btwin.Id%5D.children%3B%0A%20%20%20%20%20%20%20%20foreach%20(var%20child%20in%20children)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20subtree%5Btwin.Id%5D.children.Add(child)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(subtree.ContainsKey(child.Id))%20continue%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20queue.Enqueue(child)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20subtree%5Bchild.Id%5D%20%3D%20(child%2C%20new%20HashSet%3CBASICDIGITALTWIN%3E())%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%0A%20%20%20%20return%20subtree%3B%0A%7D%3C%2FBASICDIGITALTWIN%3E%3C%2FBASICDIGITALTWIN%3E%3C%2FSTRING%3E%3C%2FBASICDIGITALTWIN%3E%3C%2FSTRING%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22FirstParagraph%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22FirstParagraph%22%3E%3CSPAN%3EWe%20can%20measure%20the%20duration%20of%20the%202%20GetSubtree%20implementations.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Enamespace%20AzureDigitalTwinsCachingExample%0A%7B%0A%20%20%20%20using%20System%3B%0A%20%20%20%20using%20System.Diagnostics%3B%0A%20%20%20%20using%20System.Linq%3B%0A%20%20%20%20using%20System.Threading.Tasks%3B%0A%20%20%20%20using%20Azure.DigitalTwins.Core%3B%0A%20%20%20%20using%20Azure.Identity%3B%0A%0A%20%20%20%20class%20Program%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20public%20static%20async%20Task%20Main(string%5B%5D%20args)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20adtInstanceUrl%20%3D%20%22https%3A%2F%2F%3CYOUR-ADT-HOSTNAME%3E%22%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Authenticate%20and%20create%20a%20client%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20credential%20%3D%20new%20DefaultAzureCredential()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20client%20%3D%20new%20DigitalTwinsClient(new%20Uri(adtInstanceUrl)%2C%20credential)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20First%20call%20to%20avoid%20cold%20start%20in%20next%20steps.%0A%20%20%20%20%20%20%20%20%20%20%20%20_%20%3D%20client.GetDigitalTwin%3CBASICDIGITALTWIN%3E(%22ContosoCompany%22)%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20adtRepository%20%3D%20new%20AzureDigitalTwinsRepository(client)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20cachingRepository%20%3D%20new%20CachingRepository(adtRepository)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Reloading%20the%20cache%20takes%20some%20time.%0A%20%20%20%20%20%20%20%20%20%20%20%20await%20cachingRepository.ReloadCache()%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20stopwatch%20%3D%20Stopwatch.StartNew()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20subtree%20%3D%20await%20adtRepository.GetSubtreeAsync(%22Factory1%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20stopwatch.Stop()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20Console.WriteLine(%24%22Got%20subtree%20with%20%7Bsubtree.Count()%7D%20nodes%20in%20%7Bstopwatch.ElapsedMilliseconds%7D%20ms%22)%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20stopwatch.Restart()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20subtreeFromCache%20%3D%20cachingRepository.GetSubtree(%22Factory1%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20stopwatch.Stop()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20Console.WriteLine(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%24%22Got%20subtree%20with%20%7BsubtreeFromCache.Count()%7D%20nodes%20in%20cache%20in%20%7Bstopwatch.ElapsedMilliseconds%7D%20ms%22)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D%3C%2FBASICDIGITALTWIN%3E%3C%2FYOUR-ADT-HOSTNAME%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--857128544%22%20id%3D%22toc-hId--857128552%22%3E%26nbsp%3B%3C%2FH2%3E%0A%3CH2%20id%3D%22toc-hId-1630384289%22%20id%3D%22toc-hId-1630384281%22%3E%3CSPAN%3ECache%20loading%20and%20invalidation%3C%2FSPAN%3E%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EYou%20can%20preload%20the%20cache%20when%20the%20service%20starts%20and%20invalidate%20it%20when%20the%20graph%20is%20updated.%3CBR%20%2F%3E%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fdigital-twins%2Fconcepts-event-notifications%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3E%3CSPAN%3EEvent%20notifications%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3E%20can%20be%20a%20great%20trigger%20for%20that.%20Azure%20Digital%20Twins%20provides%20different%20%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fdigital-twins%2Fconcepts-route-events%23types-of-event-messages%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3E%3CSPAN%3Etype%20of%20events%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3E.%3CBR%20%2F%3EWe%20wanted%20to%20avoid%20additional%20dependencies%2C%20so%20we%20created%20an%20extra%20twin%20in%20ADT%20and%20used%20it%20as%20an%20indicator%20to%20keep%20track%20of%20the%20last%20graph%20updates.%20The%20twin%20indicator%20is%20updated%20by%20the%20service%20whenever%20it%20modifies%20the%20graph%20in%20ADT.%20Then%2C%20our%20service%20periodically%20checks%20if%20the%20indicator%20twin%20got%20updated%20and%20refreshes%20the%20cache%20if%20it%20is%20the%20case.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--177070174%22%20id%3D%22toc-hId--177070182%22%3E%3CSPAN%3EConclusion%3C%2FSPAN%3E%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EAzure%20Digital%20Twins%20is%20a%20powerful%20tool%20to%20create%20a%20digital%20representation%20of%20an%20environment%2C%20and%20we%20have%20seen%20how%20caching%20can%20be%20used%20to%20enhance%20the%20performance%20of%20twin%20graph%20traversals.%3CBR%20%2F%3EAn%20additional%20advantage%20is%20ADT%20cost%20optimization.%20%3CA%20href%3D%22https%3A%2F%2Fazure.microsoft.com%2Fpricing%2Fdetails%2Fdigital-twins%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EADT%20pricing%3C%2FA%3E%20includes%20a%20cost%20per%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fdigital-twins%2Fconcepts-query-units%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Equery%20unit%3C%2FA%3E.%20Using%20a%20cache%20may%20help%20you%20reduce%20the%20number%20of%20query%20units%20used%20by%20your%20system.%20However%2C%20reloading%20the%20cache%20also%20consumes%20query%20units.%20The%20amount%20you%20can%20save%20depends%20on%20how%20expensive%20all%20the%20operations%20you%20avoid%20to%20compute%20are%2C%20but%20the%20cost%20to%20refresh%20the%20cache%20also%20impacts%20the%20number%20of%20query%20units%20used.%20That's%20why%20you%20need%20to%20make%20your%20own%20analysis%20to%20understand%20what%20is%20best%20for%20your%20system%20and%20how%20this%20type%20of%20strategy%20would%20help%20your%20case.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3ENow%20you%20can%20try%20this%20strategy%20and%20share%20your%20experience%20and%20workarounds%20in%20the%20comments%20below!%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--1984524637%22%20id%3D%22toc-hId--1984524645%22%3E%3CSPAN%3EContributors%3C%2FSPAN%3E%3C%2FH2%3E%0A%3CP%3E%3CSPAN%3E%3CBR%20%2F%3EMarc%20Gomez%3CBR%20%2F%3EAlexandre%20Gattiker%3CBR%20%2F%3EChristopher%20Lomonico%3CBR%20%2F%3EIzabela%20Kulakowska%3CBR%20%2F%3EMax%20Zeier%3CBR%20%2F%3EPeeyush%20Chandel%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-2551153%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22digital-twins.jpg%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F296145i7DE3EDB2B0359D18%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22digital-twins.jpg%22%20alt%3D%22digital-twins.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ERead%20about%20how%20to%20accelerate%20Azure%20Digital%20Twins%20graph%20traversals.%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2551153%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAzure%20Digital%20Twins%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EAzure%20IoT%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EIoT%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Co-Authors
Version history
Last update:
‎Jul 21 2021 03:49 PM
www.000webhost.com