Select Page

What is Key-Value Store?

A key-value store is a pattern for storing data. The pattern is simply a collection of key-value pairs.

What’s a key-value pair?

A key-value pair is simply that. The “key” is the identifier, and the “value” can be anything from a simple string to a multi-dimensional array containing inner objects with more key-value pairs. Take a look at the code examples below.

//sample contact object containing key value pairs and an 
//array of objects that contain additional key value pairs

}
  firstname: "John",
  lastname: "Fritz",
  phone: [
    {type: "home", number: "(123) 123-4567"},
    {type: "mobile", number: "(123) 987-6543"}
  ]
}
//sample contact object containing key value pairs and an 
//array of objects that contain additional key value pairs

public class Phone
{
    public string type { get; set; }
    public string number { get; set; }
}

public class Contact
{
    public string firstname { get; set; }
    public string lastname { get; set; }
    public List phone { get; set; }
}

Quiz Time

#1 Is key-value store a database or a pattern?

#2 What type of value can a key-value pair have?

finish

Results

Why is it growing in popularity?

As the need for more efficient data storage and retrieval techniques increases, so does the value of key-value stores. Data is increasing steadily in quantity, and a key-value store provides the speed and simplicity needed to manipulate this data at high proficiency. The significant advantages of a key-value store are its simplicity, scalability, speed, and flexibility.

Simplicity

Because of its simple yet effective pattern, a key-value store can easily integrate with solutions that require a mechanism for storing data.

Flexibility

The greatest attribute of the key-value store for developers is that it doesn’t require a schema. Let’s elaborate on this by going over what a schema is, using a contacts database as an example.

Historically developers would use a structured database, and it would resemble something like the images below.

 

Table A contains the contact’s basic information such as First Name and Last Name. Table B contains the list of phone numbers for that one contact. The schema is a combination of the column names, data types (string, integer, etc.), and table relations.

You can see that table A’s “ID” column has a one-to-many relationship with table B’s “Contact_ID” column.

 

Now let’s say we wanted to collect the contacts address. We can add additional columns to the basic contact table (table A). But what happens if the contact has multiple addresses? In this case, we’ll need to define a new table “addresses” and create another one-to-many relationship between the two tables.

 

The downside to this is performance. Whenever we want to query the data, an additional JOIN statement will have to be added to every query where we would want to obtain the address(es) along with the contact information. This negatively impacts performance and creates more overhead.

Let’s see how we would accomplish this same task using a key-value store.

//key-value store javascript object example

{
    id: "1",
    firstName: "Joseph",
    lastName: "Frazier",
    phone: [
        { type: "home", phone: "(123) 456-7890" },
        { type: "mobile", phone: "(949) 123-4567" }
    ],
    addresses: [
        {
            description: "Home",
            street: "1234 Something Lane",
            city: "Los Angeles",
            state: "CA",
            postal: "90001"
        },
        {
            description: "Business",
            street: "321 Main St",
            city: "Los Angeles",
            state: "CA",
            postal: "90001"
        }
    ]
}

Speed

Because a key-value pair can contain multi-dimensional arrays, we can house the entire contact in a single object. How neat is that! The positive performance benefit here is the fact that you don’t have to JOIN additional tables in each query.

Key-value stores use simple operational commands such as get, put, and delete, which makes them highly proficient at processing constant streams of read/write operations. The path requests they use are shorter and more direct in length, meaning that more operations can be performed in a given amount of time compared to other database models.

Scalability

Key-value stores handle size well and are good at processing a constant stream of read/write operations. This property makes it highly scalable.

Key-value stores scale-out by implementing partitions, replication, and auto-recovery. They can scale up by maintaining the database in RAM. By avoiding locks, latches, and low-overhead server calls, key-value stores can minimize the effects of ACID guarantees, which guarantee that transactions persist somewhere once they are committed.