Exploring the Landscape of Database Paradigms - A Comprehensive Guide - Key-Value Databases
Tags: database, architecture
Published: 2024-05-01 15:30:00

_Disclaimer: This post is created with the help of AI and AT tools_

Navigating the complex landscape of database technologies can often be a daunting task, especially with the number of paradigms available today. Each database paradigm has distinct features and optimizations, making it ideal for certain types of data and use cases.

From key-value stores built for simplicity and speed to full data warehousing solutions for complicated querying over massive datasets, the options are vast and diverse.

This post seeks to explain the many database paradigms, which include key-value, wide column, document, relational, graph, search engine, multi-model, time series, and data warehousing databases. We'll look at the core design of each type, examine their benefits and drawbacks, and present a full comparison to help you understand the essential distinctions. Additionally, this guide will help you select the best database paradigm for your specific needs, ensuring that your tech decisions are both informed and purposeful.

Whether you are a database specialist, a software developer, or simply a tech enthusiast, this post will provide you with the knowledge you need to confidently navigate the evolving database field.

In this first installment we are going to discuss Key-Value Stores.
## Key-Value Stores
Key-value databases are often considered the simplest of the NoSQL databases. This simplicity makes key-value stores and databases quick, user-friendly, portable, scalable, and flexible. Key-value stores are actually pretty straightforward. A value, which can be basically any piece of data or information, is stored with a key that identifies its location. In fact, this is a design concept that exists in pretty much every piece of programming as an array or map object. The difference here is that it’s stored persistently in a database management system.

### How Key Value Databases Work
A key-value database has sets of key-value pairs, where the key is the identifier and the value is the data in question. Each key in the database has to be unique. It acts as an identifier, and it is usually a string or integer. The value of the database is a sort of opaque blob. It can be a simple data type, like a string or an integer, or a more complex data type, like an array, a list, or even a serialized object.

| **Username**  | **Last Login Time**    |
| ------------- | ---------------------- |
| jonhb         | 01/05/2024-12:32:45.75 |
| alicx         | 01/05/2024-13:32:32.84 |
| cyberduckling | 01/05/2024-14:11:43:23 |


Under the hood, key-value databases keep an in-memory data structure that is mapped to the data stored on disc. RAM is much faster than accessing data from the disc, so most databases will have an algorithm for keeping frequently accessed data in RAM and only fallback to the disc if the index isn't already stored in memory. That way, the key-value database combines the speed of RAM with the resiliency of the disc.

"Pure" key-value databases do not use a query language, but they do offer a simple set of commands for retrieving, saving, and deleting data.

- **SET:** Add a new key-value pair to the database or update the value of an existing key.
- **GET:** Retrieve the value associated with a specific key. If the key does not exist, the operation returns an error or a null value, depending on the database's design.
- **DELETE:** Remove a key-value pair from the database. If the key does not exist, the operation returns an error or does nothing, depending on the database's design.

### Features of Key-Value Databases
The most basic feature that any key-value database should provide is the ability to create, read, update, and delete data using keys. But most popular databases provide features beyond the basics. These features may include support for different data types, indexing capabilities, and the ability to perform transactions.

#### Data Type Support
Although key-value databases are meant to be simple, they can store a wide range of data types, from simple strings and numbers to more complex structures like hashes, lists, and sets. Redis and other systems like it add support for JSON objects and other specialised data types through modules. This lets you store complex structures directly. Others, like Amazon DynamoDB, handle lists, maps, and sets natively, so they can be used for a wide range of data handling tasks. The type of data that a key-value store supports can have a big effect on what kinds of applications it can work with. It's important to think about how the complexity of data tasks affects performance.

#### Key Sorting
One common feature of key-value stores is the ability to sort keys in a specific order, such as numerical or alphabetical, so that the keys can be efficiently iterated over. Some common use cases for this include:

- Getting all the keys starting with a certain letter
- Getting all the keys within a range of numbers
- Getting all the keys less than or greater than a certain number
- Getting all the keys within a certain period of time if the key is a timestamp

#### Replication and Partitioning
A lot of key-value systems come with advanced scaling features that you can use right away. With replication, the same data can be found on more than one machine. This helps both with disaster recovery and with being able to add more nodes. If one node goes down, you still have your data.

The way your data is spread out across nodes is called _partitioning_. Many databases not only provide an algorithm for partitioning out of the box but also give you a way to define your own partitioning. One simple way to show this would be to use the first letter of each key as the split. This would make 26 partitions, one for each letter of the alphabet.

Key-value databases that are more advanced will automatically be able to spread your database across different data centers. This makes your application more reliable and faster because you can respond to user queries around the world that are close by by using local data centers.

### Key-Value Database Consistency Models
While being renowned for their speed and simplicity, key-value databases also offer a range of consistency models to control the timeliness and correctness of the data they hold. Concurrent data accesses or network outages are just two examples of situations in which the consistency model selected can affect database behavior.

- **Strong Consistency**: The system guarantees that any successful write operation will be immediately visible to all subsequent read operations. This model ensures that all users will see the same data at the same time.
- **Eventual Consistency**: The system guarantees that if no new updates are made to a given data item, eventually all accesses will return the last updated value. Note that the time it takes to reach consistency can vary.
- **Casual Consistency**: The system ensures that if a certain operation causally depends on another, then every node in the system will see them in that order. It's weaker that strong consistency but stronger than eventual consistency.
- **Session Consistency**: The system ensures that within a single session (a series of operations performed by a single client), the client will always see its writes. If a client writes a value, then subsequent reads (within the same session) will always see that value or newer.
- **Monotonic Read Consistency**: The system ensures that if a read operation has been performed, any subsequent reads will always see that data or newer data. It guarantees that the data will not revert to an older state.
- **Read-your-writes Consistency**: The system ensures that any data written by a client can be immediately read back by the same client. This is particularly important in user session scenarios.

### Advantages of key-value Databases
Traditional relational databases store data in the form of tables containing rows and columns. They enforce a rigid structure on data and are not optimal for every use case. On the other hand, key-value databases are NoSQL databases. They allow flexible database schemas and improved performance at scale for certain use cases.
#### Scalability
Key-value databases and NoSQL databases in general are marketed mostly for their capacity to scale more than relational databases. These databases gained popularity when big tech companies like Google and Amazon revealed the databases they developed in-house to address scaling issues.  
  
Usually, databases become the main software bottleneck, and many developers have experienced the difficulties in putting replication, sharding, and other scaling-out techniques into practice. Key-value databases have become so popular quickly because many tech companies found it appealing to be able to abstract that away and concentrate on building software that generates commercial value.
#### Ease of Use
Key-value databases use the object-oriented model, which lets programmers connect real-world objects directly to software objects. Several computer languages, like Java, and C# also work in this way. Engineers don't have to map their code objects to various underlying tables. Instead, they can make key-value pairs that match their code objects. This makes key-value stores more intuitive for developers to use.
#### Performance
Key-value databases handle constant read and write tasks with few extra server calls. Better performance at scale comes from lower reaction time and better latency. Instead of having many tables that are connected to each other, they are based on simple, single-table designs. Key-value databases are much faster than relational databases because they don't need to do resource-intensive table joins.

### Key-value Database Use Cases
#### Performance-Sensitive Applications
Using a key-value database such as Redis to enhance an application's read performance is a widely used design technique. When data is written and then pushed out to several key-value database nodes spread out geographically, a relational database can serve as the source of truth. This makes an app more scalable and dependable and reduces latency because the data is closer to the users.  
  
Pre-computed data that's essential to the user experience can also be stored in key-value databases. Twitter is one example of this, producing users' news feeds in advance and caching them to enable speedier homepage loads for users.

#### Storage Engine for Higher-Level Databases
Because of their inherent performance and ability to reduce development time by not having to create the wheel, key-value databases are often used as the storage engine in databases. RocksDB is an open-source embedded key-value database created by Facebook that has been used by or is supported by MySQL, Cassandra, MariaDB, MongoDB, YugabyteDB, and InfluxDB.

#### Internet of Things
Sensors and related technologies are being used by numerous companies across numerous industries to gather more operational data. It could have to do with product development and manufacturing, or it could have to do with using a service model to get data for clients. Companies might be collecting data about supplier and vending contracts, and how those operations work.
  
New communications models, such as the Internet of Things, which leverage a higher number of devices to transfer data across a business network, also have a comparable advantage. Data on the Internet of Things is "always in transit"—filtered through more hardware hops, with all the potential logistical complications that entails.  
  
As a result, modern engineering has come up with ideas for processing data points closer to their source. The concept of computing "close to the edge" with a NoSQL database—that is, in the data storage environment where the devices are gathering data—is frequently promoted by experts. This type of data operation is complemented by key-value databases. They also enable better and more capable handling of this dynamic activity because of their flexibility.  
  
In order to increase business efficiencies, key-value databases can be used in conjunction with other tactics. For instance, more effective use of time-stamped data can complement business-insight-producing data visualization.  
  
Connecting these kinds of NoSQL database configurations to vendor service models' dynamic resources is another approach to get more done with them. One excellent illustration of this is the serverless function. The business user can supplement the powerful database systems around the time-stamped data in an efficient manner by using AWS Lambda or another serverless function provider.

### Key Value Database Examples
#### Berkeley DB
One of the earliest examples of a key-value database is Berkeley DB. It replaced the proprietary DBM equivalent written by AT&T for Unix and was developed in 1991 at the University of California, Berkeley for their BSD operating system. Berkeley DB is distinct because it is an embedded key-value store, which means that it was designed to be embedded within an application and did not by default enable network access. Numerous architectural choices made for the purpose of efficiency and ease of use might be seen as forerunners of NoSQL.  
  
Similar embedded key-value databases named RocksDB and LevelDB were developed by Facebook and Google as a result of Berkeley DB.

#### DynamoDB
Dynamo was a very influential paper published by Amazon about their internal key-value database which was used to scale their Amazon Marketplace. While many of the concepts used by Dynamo had been around for decades, Amazon brought them into the mainstream and proved that there was commercial value in using NoSQL-type databases.

#### Redis
Redis is a fully in-memory key-value database. This means that all data is stored in RAM rather than on disk, which drastically improves performance of reads and writes because RAM is generally 50x faster for sequential data reads and up to 100,000x faster for random access data. The downside is that holding data in RAM is significantly more expensive than storing data on a hard drive. Redis is typically used alongside another database as a cache to handle read requests.

## Conclusion
In conclusion, key-value databases exemplify the simplicity and efficiency that NoSQL technologies bring to modern computing. Their straightforward design, allowing for quick and intuitive data storage and retrieval using unique keys, aligns well with the requirements of today's fast-paced, data-intensive applications. 

By eliminating the complexities associated with relational databases, key-value stores offer scalability, performance, and flexibility that are essential for handling large volumes of diverse data types and high-traffic environments. From enhancing the performance of internet applications to serving as the backbone for IoT infrastructures, key-value databases have proven to be indispensable in the digital age.

As technology continues to evolve and the demand for more efficient data processing grows, key-value databases are likely to remain a fundamental component of database management, heralding a future where data can be managed more swiftly and effectively to meet the ever-expanding needs of businesses and consumers alike.

In the next post, we are going to discuss about Wide Column Stores. Stay tuned!