Database Architectures & Use Cases - Explained
Tags: database, architecture, webdev, beginners
Published: 2020-06-30 17:42:00

###With over 300 databases on the market, how do you determine which is right for your specific use case or skill set? 
We continue to see the common debate of SQL vs. NoSQL and other database comparisons all over social media and platforms like dev.to. In most cases, it’s not that one database is _better_ than the other, it’s that one is a _better fit for a specific use case_ due to numerous factors. 

A couple months back, our CTO Kyle Bernhardy, led an awesome talk titled [A Deep Dive Into Database Architectures] (https://www.youtube.com/watch?v=UIAfhmCZbSQ). You can watch this talk at the link, but since this is such a prominent discussion topic we thought it might be helpful to summarize. __This article will provide an overview on database architectures, including use cases and pros & cons for each of them__. 
<br>
__Let’s start with general considerations when selecting a database__. It’s important to understand things such as data type / structure, data volume, consistency, write & read frequency, hosting, cost, security, and integration constraints. The more you know about these factors, the easier it will be to pick the right database for your project. 

You may already know that there are generally __3 database hosting options__: 

* __On-Premises__
Database fully maintained by organization on servers running within their data center(s)
_More control, but usually more expensive and time consuming_


* __Cloud Hosted__
Servers are maintained by cloud providers, organizations maintain database software and operating system running on the machine
_Flexible scaling and no server upkeep, but no control over physical server and potential network limitations_


* __Database-as-a-Service (DBaaS)__
Database maintained by service provider, organizations only charged for usage of service
_Cost effective and zero upkeep, but data stewardship and potential network limitations_ 


###Now for the part you’ve been waiting for - __database architectures__. 

###Relational Databases
We’ll start with the most commonly used. Relational (SQL) Databases such as Oracle, MySQL, PostgreSQL, Microsoft SQL Server, and SQLite, organize data into tables with columns, each with a specified name and datatype. Additionally:

* Rows are identified with a unique attribute, or grouping of attributes, called a primary key (typically a single column)
* Relationships between tables are defined through foreign keys which reference primary keys
* Strict schema (data model) enforcement
* Data accessed via Structured Query Language (SQL)
* ACID compliance (Atomicity, Consistency, Isolation, and Durability)
* Extra features like Triggers & Stored Procedures

__On the plus side__, relational databases use mature technology that is widely understood and well-documented, SQL standards are well-defined, defined constraints enforce data integrity, they avoid data duplication and are highly secure and ACID-compliant. __However on the negative side__, SQL databases cannot handle unstructured or semi-structured data, their tables don’t necessarily map to objects, they require complicated ETL (Extract, Transform, Load) and maintenance, have row locking, and pricing for some products (Oracle, SAP) are out of reach for developers and some organizations. 
_Note: While some RDBMS systems can now handle JSON, they are not purpose built to do so._

There are several awesome __use cases for relational databases__; situations where data integrity is absolutely paramount (financial applications, defense and security, private health information), highly structured data, and automation of internal processes. 


###Then comes everything else… 
Relational databases are the most common database in production today, but they were not designed for the scale and agility of modern applications. About 10 years ago __the NoSQL movement caught on__ to address these concerns and changed the database landscape forever. Note: 

* NoSQL doesn’t mean anything (Non-SQL, Non-relational SQL, Not Only SQL)
* Designed to address structure, performance, data volume, and scalability
* Relational databases have since addressed many of these concerns

While databases are typically categorized as SQL or NoSQL, there are many intricacies to NoSQL databases. _Let’s get into it_. 

<br>
###Key-Value Stores
Key-value stores are often used as the underlying storage for higher level databases. For example, MongoDB uses a key value store called WiredTiger as their default storage engine. Key-Value Stores, such as Redis, DynamoDB, and Cosmos DB, are:

* Simple, basic, & schemaless
* Provide basic functionality for retrieving arbitrary data via a specific key 
* Values can be anything: single values, arrays, objects, files, etc.
* Database does not evaluate the data it is storing
* Data structure can be referred to as a dictionary or hash table

__For pros__, key value stores provide fast, low-complexity access to data, are flexible, and can scale quickly and cheaply. __However__, they have extremely limited functionality, cannot handle complex structures or query or search by anything other than key, do not scale well as data models grow, and they require more programming overhead for complex implementations. 

Example __use cases for key value stores__ would be embedded systems, URL shorteners, configuration data, application variables and flags for web applications, state information, and data represented by a dictionary or hash. 

<br>
###Document Stores
Document stores, such as MongoDB, DynamoDB, Couchbase, and Firebase, are similar to key-value stores, but the value is a document.

* Typically document formats are JSON, BSON, or XML documents
* Schemaless, no data structure enforcement (documents can be different)
* Data accessed and modified via NoSQL (or proprietary language)
* Well-suited for unstructured and semi-structured data
* Seen as easier for development

The __pros of document stores__ include flexibility and scalability, schemaless, fast writes, ideal for semi-structured and unstructured data, and developers do not need to know data structure ahead of time / it can change overtime without downtime. __The cons__ are that they are not ACID compliant, limited to querying within a document, relationships/cross references are not enforced, slow searching, cannot join documents/collections in a single query, lack of database enforcement requires developer discipline and vigilance for application level enforcement, and they typically result in data duplication. 

Great __use cases for document stores__ are unstructured or semi-structured data, content management, rapid prototyping, and collecting of high traffic data. 

<br>
###Graph Databases
Graph databases, such as Neo4j, OrientDB, and TitanDB, are ideal for when relationships or connections are top priority.

* Based on mathematical graph theory
* Represent data as a network of related nodes, edges, and properties
* Database stores data items within nodes and relationships in edges that connect nodes 
* Nodes are connected by relationships and grouped according to labels
* Facilitate data visualizations and graph analytics
* Each node contains free-form data

__On the plus side, graph databases__ have advanced features for relationship querying, traversing, and tracking, are optimized for querying related data, and they avoid row locking. __As for the negatives__, graph databases have a large ramp up time for developers, high overhead for simple use cases, lack of standardization, poor performance of aggregate queries, and devs typically need to learn a custom query language. 

__Graph databases are great for__ analysis of heterogeneous data points, fraud prevention, advanced enterprise operations, social networking, payment systems, and GeoSpatial routing/visualization. 


###Time Series Databases
_We’re almost there_! Next up is time series databases, such as InfluxDB, Kdb+, and Prometheus, which are:

* Focused on datasets that change over time
* Heavily write oriented
* Designed to handle constant streams of data
* Typically append-only (no modification after ingestion)
* Rollup/aggregation/down sampling features to lower archive data footprint

__On the positive side__, time series databases are designed for dealing with linear data over time, can handle high ingestion rates, have built-in features specifically for dealing with time-based data, a schema optimized for time-series arrays, and batch delete features. __As far as negatives__, time series databases only deal with time-series data, do not support full SQL, their read speed suffers compared to writes, they have no transaction capability and are append-only (not optimized for updates). 

__Great use cases for time series databases__ are managing infrastructure, IoT sensor collection, and log monitoring and alerting. 

<br>
###Search Engines
Last but not least is search engines, such as Elasticsearch, Splunk and Apache Solr, which are:

* Built for non-relational, document-based data 
* Arranged and optimized for storage and rapid retrieval of data 
* Indexes data across a variety of sources including: file systems, intranets, document -Management systems, e-mail, and databases

__Search engine pros__ include their focus on optimized searching, highly scalable and schemaless, and they have advanced search options like full text search, suggestions, and complex search operations. __The cons__ are that they are expensive, have low durability and [poor security] (https://www.infoworld.com/article/3268871/how-to-choose-the-right-type-of-database-for-your-enterprise.html), have no transaction support, are not efficient for writing and retrieving data outside of searching, and are difficult to manage.

__Search engines are great when__ search results are top priority, logging, product catalogs, and blogs. 
<br>

###Multi Model Considerations
It should be noted that many real life implementations choose “polyglot persistence,” which is the concept of using different data storage technologies to handle different data storage needs within a given software application. Many database technologies implement this within a single software, referred to as _multi-model_ or _data lake_ technology. This can be ideal for specific use cases, but poses inherent risk such as instability, data inconsistency and corruption, expensive / resource intensive, and data replication on disk and memory.

__A few examples of multi-model technologies are__:

* Hadoop: Software tools running on top of multiple databases
* MongoDB: Multiple data storage options in a single database software platform
* PostgreSQL: Row-oriented, column-oriented, key-value, and document-oriented data storage options

<enter>
<enter>

###Finally, as a database company... 
__It probably makes sense to discuss where HarperDB fits into the mix__. 
If for no other reason than to highlight that these database “categories” are not all black and white, and some databases take more of a “hybrid” approach by subscribing to several methodologies. Instead of falling into a single bucket, __HarperDB can be considered as a structured object store with SQL capabilities__. It features:

* Built-in REST API
* NoSQL & SQL operations including joins
* Dynamic schema
* Advanced publish-subscribe data replication 
* [Self-service management studio] (https://studio.harperdb.io/?utm_source=devtomargo&utm_medium=content&utm_campaign=dbarchitectures)
* Traditional drivers/interfaces
* [Custom Functions](https://harperdb.io/docs/custom-functions/) (serverless, highly customizable API endpoints that interact with our HarperDB Core operations)

We built [HarperDB](https://harperdb.io/?utm_source=devtomargo&utm_medium=content&utm_campaign=dbarchitectures) from the ground up to expand and blend the best capabilities of SQL, [NewSQL](https://dev.to/harperdb/deep-dive-newsql-databases-255l), and NoSQL products because we felt there were certain use cases that could be better served with another solution. We believe that providing developers with the ability to choose the right tool for the job empowers developers and spurs innovation. __Some examples where we feel that HarperDB is a better fit__ include cases where you need both NoSQL & SQL, rapid application development, integration, edge computing, distributed computing, and real-time operational analytics. Essentially, the SQL vs. NoSQL debate becomes irrelevant with HarperDB because you no longer have to choose! 

<br>
_Hopefully this database architecture overview is helpful in finding the right database for your use case. Please reply below with questions or comments - we would love to discuss!_