{"id":5305,"date":"2022-12-08T10:00:29","date_gmt":"2022-12-08T18:00:29","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/cosmosdb\/?p=5305"},"modified":"2022-12-08T09:51:38","modified_gmt":"2022-12-08T17:51:38","slug":"altgraph-graph-workloads-with-azure-cosmos-db-for-nosql","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cosmosdb\/altgraph-graph-workloads-with-azure-cosmos-db-for-nosql\/","title":{"rendered":"AltGraph \u2013 Graph workloads with Azure Cosmos DB for NoSQL"},"content":{"rendered":"<p><a href=\"https:\/\/github.com\/cjoakim\/azure-cosmosdb-altgraph\" target=\"_blank\" rel=\"noopener\">AltGraph<\/a> is the idea of implementing \u201cgraph workloads\u201d not with a graph database, but with the more general-purpose <a href=\"https:\/\/azure.microsoft.com\/products\/cosmos-db\/#overview\" target=\"_blank\" rel=\"noopener\">Azure Cosmos DB for NoSQL<\/a> (formerly known as the \u201cSQL API\u201d).\u00a0 Azure Cosmos DB for NoSQL is highly performant and offers the best integration with the rest of Azure.\u00a0 It also offers the lowest learning curve since most of the industry already understands SQL (Structured Query Language), the query language used by this database.<\/p>\n<p>AltGraph was the topic of episode #59 of <a href=\"https:\/\/developer.azurecosmosdb.com\/tv\" target=\"_blank\" rel=\"noopener\">Azure Cosmos DB Live TV<\/a>, and has since been expanded to include the v2 IMDb graph.<\/p>\n<p>&nbsp;<\/p>\n<h2><strong>Perception<\/strong><\/h2>\n<p>Many customers I speak with initially visualize their use-cases as a graph, such as the following diagram.\u00a0 They then reason that since the application is <em>visualized<\/em> as a graph means that the underlying datastore needs to be <em>implemented<\/em> as a graph database.\u00a0 It is my assertion that this is not true \u2013 the underlying data can be stored and traversed in alternative and possibly much more efficient datastores, such as Azure Cosmos DB for NoSQL.\u00a0 JavaScript libraries can be used to create the graph visualizations.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2022\/12\/sample-graph.png\"><img decoding=\"async\" class=\"aligncenter wp-image-5310 size-large\" src=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2022\/12\/sample-graph-1024x892.png\" alt=\"sample graph diagram\" width=\"640\" height=\"558\" srcset=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2022\/12\/sample-graph-1024x892.png 1024w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2022\/12\/sample-graph-300x261.png 300w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2022\/12\/sample-graph-768x669.png 768w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2022\/12\/sample-graph.png 1408w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<h2><strong>Application Architecture<\/strong><\/h2>\n<p>A graph application is typically built with <em>much more than just a database<\/em> at the center of the solution.\u00a0 A typical solution integrates several services, and this is where Azure and Azure Cosmos DB shines \u2013 the ease of integrations.\u00a0 In the diagram below, the native integrations that Azure Cosmos DB supports are shown in <span style=\"color: #993300;\"><strong>red<\/strong><\/span>.\u00a0 These are implemented in a reliable and scalable way by the Azure PaaS services themselves, so you, the customer, don\u2019t have to write \u201cglue code\u201d or explicit ETL to integrate them.<\/p>\n<p>Four of these integrations I\u2019d like to call out here are <a href=\"https:\/\/azure.microsoft.com\/products\/functions\/#overview\" target=\"_blank\" rel=\"noopener\">Azure Functions<\/a>, <a href=\"https:\/\/azure.microsoft.com\/products\/cognitive-services\/#overview\" target=\"_blank\" rel=\"noopener\">Azure Cognitive Search<\/a>, <a href=\"https:\/\/learn.microsoft.com\/azure\/cosmos-db\/synapse-link\" target=\"_blank\" rel=\"noopener\">Azure Synapse Link for Azure Cosmos DB<\/a>, and <a href=\"https:\/\/azure.microsoft.com\/products\/synapse-analytics\/#overview\" target=\"_blank\" rel=\"noopener\">Azure Synapse Analytics<\/a>.\u00a0 With Azure Functions you can observe the Azure Cosmos DB change-stream, and thus have event-driven logic in your application for real-time processing.\u00a0 Azure Cognitive Search supports automatic indexing of Azure Cosmos DB containers.\u00a0 For some applications their queries\/searches can be much more fluent and cost-effective by using this search engine.\u00a0 Azure Synapse Link implements the HTAP pattern (Hybrid Transactional Analytic Processing) to automatically copy your documents to a low-cost datastore for processing in a \u201cBig Data\u201d batch environment such as Apache Spark in Azure Synapse Analytics.<\/p>\n<p>Thus, the total Azure and Azure Cosmos DB-based graph application can leverage the \u201cbest tool for the job\u201d, as well as reduce costs, with these efficiencies.<\/p>\n<p>Conversely, it is an antipattern to use one database for both real-time and batch\/analytic purposes, as the DBMS often won\u2019t scale under the workload, and typically offers sub-optional support for one or more of these access patterns.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2022\/12\/altgraph-integrations-1.png\"><img decoding=\"async\" class=\"aligncenter wp-image-5341 size-large\" src=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2022\/12\/altgraph-integrations-1-1024x704.png\" alt=\"altgraph architecture and integrations diagram\" width=\"640\" height=\"440\" srcset=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2022\/12\/altgraph-integrations-1-1024x704.png 1024w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2022\/12\/altgraph-integrations-1-300x206.png 300w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2022\/12\/altgraph-integrations-1-768x528.png 768w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2022\/12\/altgraph-integrations-1-1536x1056.png 1536w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2022\/12\/altgraph-integrations-1.png 1554w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<h2><strong>So, what again is AltGraph?<\/strong><\/h2>\n<p>AltGraph isn\u2019t a product, or another Azure PaaS service.\u00a0 Instead, <em>it\u2019s a set of reference applications that demonstrate how to build \u201cgraph applications\u201d with Azure Cosmos DB for NoSQL<\/em> and leverage its\u2019 features to achieve performant, integrated, and low-cost solutions.\u00a0 It currently demonstrates two different graph use-cases, with two different implementations.<\/p>\n<p>The first is a Bill-of-Materials use-case that uses data from the NPM (Node Package Manager) ecosystem.\u00a0 The implementation uses the concept of RDF graph database \u201ctriples\u201d, but implemented with Azure Cosmos DB.\u00a0 Roughly 20-million triples can be stored efficiently in a single Azure Cosmos DB logical partition, which makes their queries both fast and low-cost.\u00a0 Optionally, these can be cached using <a href=\"https:\/\/learn.microsoft.com\/azure\/azure-cache-for-redis\/cache-overview\" target=\"_blank\" rel=\"noopener\">Azure Cache for Redis<\/a> or the Azure Cosmos DB integrated cache.\u00a0 Additionally, in-memory graph traversal logic can be utilized that is 1000x faster than DBMS disk IO.<\/p>\n<p>A \u201cTriple\u201d is a data structure that links a subject, an object, and a predicate.\u00a0 For example, in the relationship \u201cChris works at Microsoft\u201d the subject is Chris, the object is Microsoft, and the predicate is \u201cworks at\u201d.\u00a0 Triples can be visualized like this:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2022\/12\/rdf-triple.png\"><img decoding=\"async\" class=\"aligncenter wp-image-5311 size-large\" src=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2022\/12\/rdf-triple-1024x228.png\" alt=\"rdf triple representation\" width=\"640\" height=\"143\" srcset=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2022\/12\/rdf-triple-1024x228.png 1024w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2022\/12\/rdf-triple-300x67.png 300w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2022\/12\/rdf-triple-768x171.png 768w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2022\/12\/rdf-triple.png 1084w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>In the AltGraph reference implementation, the Triple documents look like the following.\u00a0 They have additional attributes to enable fast vertex document point-reads in Azure Cosmos DB.\u00a0 The optional subjectTags and objectTags allow the application to efficiently \u201cpeek\u201d into the adjacent vertex documents, for their primary attributes, without actually reading those documents.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2022\/12\/express-triple-document.png\"><img decoding=\"async\" class=\"aligncenter wp-image-5312 size-large\" src=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2022\/12\/express-triple-document-1024x979.png\" alt=\"sample triple document\" width=\"640\" height=\"612\" srcset=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2022\/12\/express-triple-document-1024x979.png 1024w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2022\/12\/express-triple-document-300x287.png 300w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2022\/12\/express-triple-document-768x734.png 768w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2022\/12\/express-triple-document-24x24.png 24w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2022\/12\/express-triple-document.png 1042w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>The second reference application is a rich Social Network graph that uses IMDb (Internet Movie Database) data.\u00a0 This large dataset is very relatable and can be used to explore the \u201cSix Degrees of Kevin Bacon\u201d graph challenge.\u00a0 The implementation of this reference app uses the <a href=\"https:\/\/github.com\/jgrapht\/jgrapht\" target=\"_blank\" rel=\"noopener\">JGraphT<\/a> Java library which offers very fast in-memory processing as well as a comprehensive set of graph traversal algorithms such as Dijkstra Shortest Path, Centrality, and Page Rank.\u00a0 <em>Over 1-million movie and actor vertices are stored in the memory of the JVM, as well as 3.9 million edges <\/em>in just 1.5GB of memory.\u00a0 Larger in-memory graphs are possible.\u00a0 Using a combination of in-memory graph traversals, and point-reads to Azure Cosmos DB, can result in a very performant and cost-effective solution.<\/p>\n<p>This reference application screenshot shows \u201cTwo degrees of Kevin Bacon\u201d and uses D3.js for interactive JavaScript visualizations.\u00a0 The graph traversal <em>took only 3 milliseconds to execute and consumed zero Azure Cosmos DB request units<\/em> (RUs).<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2022\/12\/network-kevin-bacon-2.png\"><img decoding=\"async\" class=\"aligncenter wp-image-5314 size-large\" src=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2022\/12\/network-kevin-bacon-2-1024x525.png\" alt=\"network diagram for two degrees of kevin bacon\" width=\"640\" height=\"328\" srcset=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2022\/12\/network-kevin-bacon-2-1024x525.png 1024w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2022\/12\/network-kevin-bacon-2-300x154.png 300w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2022\/12\/network-kevin-bacon-2-768x393.png 768w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2022\/12\/network-kevin-bacon-2-1536x787.png 1536w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2022\/12\/network-kevin-bacon-2-2048x1049.png 2048w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>Are other implementations of \u201cAltGraph\u201d possible?\u00a0 Yes!\u00a0 I\u2019m sure that each customer will create their own implementations, perhaps inspired by the examples in this GitHub repository.\u00a0 The Microsoft team is here to support you in those efforts and ensure that you are successful.<\/p>\n<p>&nbsp;<\/p>\n<h2><strong>Get started:<\/strong><\/h2>\n<ul>\n<li><a href=\"https:\/\/azure.microsoft.com\/products\/cosmos-db\/\" target=\"_blank\" rel=\"noopener\">Learn about Azure Cosmos DB and get started free<\/a><\/li>\n<li>Find technical documentation about <a href=\"https:\/\/learn.microsoft.com\/azure\/search\/search-howto-index-cosmosdb\" target=\"_blank\" rel=\"noopener\">Azure Cognitive Search<\/a> and\u00a0<a href=\"https:\/\/learn.microsoft.com\/azure\/azure-cache-for-redis\/cache-overview\" target=\"_blank\" rel=\"noopener\">Azure Cache for Redis<\/a><\/li>\n<li>Learn about\u00a0<a href=\"https:\/\/jgrapht.org\/\" target=\"_blank\" rel=\"noopener\">JGraphT\u00a0<\/a><\/li>\n<li>Visit\u00a0<a href=\"https:\/\/github.com\/cjoakim\/azure-cosmosdb-altgraph\" target=\"_blank\" rel=\"noopener\">the AltGraph GitHub repository\u00a0<\/a><\/li>\n<\/ul>\n<p><iframe src=\"\/\/www.youtube.com\/embed\/SGih_Kj_1yk\" width=\"560\" height=\"314\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<h2>About Azure Cosmos DB<\/h2>\n<p><a href=\"https:\/\/azure.microsoft.com\/products\/cosmos-db\/developer-resources\/#overview\" target=\"_blank\" rel=\"noopener\">Azure Cosmos DB<\/a> is a fast, distributed NoSQL and relational database built for high-performance applications of any size or scale. It&#8217;s a fully-managed database with support for open-source PostgreSQL, MongoDB, and Apache Cassandra, <a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/cosmos-db\/request-units\" data-bi-an=\"content-overview-01\" data-bi-tn=\"undefined\">automatic and instant scalability<\/a>, and <a href=\"https:\/\/azure.microsoft.com\/en-us\/support\/legal\/sla\/cosmos-db\/v1_4\/\" data-bi-an=\"content-overview-01\" data-bi-tn=\"undefined\">SLA-backed<\/a> availability and speed. Take advantage of <a href=\"https:\/\/learn.microsoft.com\/azure\/cosmos-db\/optimize-dev-test\" target=\"_blank\" rel=\"noopener\">free dev\/test offers<\/a>\u00a0to start building today.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>AltGraph is the idea of implementing \u201cgraph workloads\u201d not with a graph database, but with the more general-purpose Azure Cosmos DB for NoSQL (formerly known as the \u201cSQL API\u201d).\u00a0 Azure Cosmos DB for NoSQL is highly performant and offers the best integration with the rest of Azure.\u00a0 It also offers the lowest learning curve since [&hellip;]<\/p>\n","protected":false},"author":107627,"featured_media":5341,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[14],"tags":[],"class_list":["post-5305","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-core-sql-api"],"acf":[],"blog_post_summary":"<p>AltGraph is the idea of implementing \u201cgraph workloads\u201d not with a graph database, but with the more general-purpose Azure Cosmos DB for NoSQL (formerly known as the \u201cSQL API\u201d).\u00a0 Azure Cosmos DB for NoSQL is highly performant and offers the best integration with the rest of Azure.\u00a0 It also offers the lowest learning curve since [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/posts\/5305","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/users\/107627"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/comments?post=5305"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/posts\/5305\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/media\/5341"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/media?parent=5305"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/categories?post=5305"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/tags?post=5305"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}