Get startedGet started for free

Introduction to key-value databases

1. Introduction to key-value databases

In the final chapter of this course, we'll explore two more NoSQL data storage tools. First up are key-value databases.

2. Key-value databases

Key-value databases use simple key-value pairs to store data, similar to dictionaries in Python. In this course, we'll use Redis to interact with key-value data. On the right is the most basic example of key-value data. The "weather" key points to a value of "sunny". The "sunny" value can only be retrieved via the "weather" key. This holds true for all key-value databases; values can only be returned by querying the key. This makes key-value databases unique, as they are the only NoSQL data storage tool we've explored that can't be queried by the value of the data stored, but rather, only by the key that stores that data. More on that later! Key-value databases support several data types, including string, hash, or lists, in addition to a number of other types, depending on the provider. Key-value databases are incredibly performant for simple reads and writes of data due to their efficient data retrieval mechanism and in-memory architecture. Traditional key-value databases, like Redis, store data in memory instead of on disk. This helps to increase read and write speed but means that data won't be persisted indefinitely. While Redis can be configured to store data to disk, the default behavior is to store data in memory. These characteristics, along with the simplicity of the data being stored, help to distinguish key-value databases from document databases.

3. Key-value database use-cases

Key-value databases are commonly used in web applications, for tasks like session management, and for caching frequently accessed data. Key-value databases may even be used to track user preferences and behavior during a web-based experience, which can then be used for ad or product recommendations. While these use cases are not traditional analytics workflows, data may flow downstream from key-value databases, such as Redis, and into more traditional tools, such as Snowflake or AWS Redshift. Here, analysts may develop models or build graphics that help to drive business decisions, where the process may come full-circle by impacting the user experience where this data was first generated.

4. Redis

As mentioned above, we'll use Redis to work with key-value data. Redis is an open-source key-value database widely used across the world to store and retrieve key-value data. It has support for a number of programming languages, including Python. We'll use the redis library to interact with Redis using Python. First things first, a Redis cluster needs to be running in order to start working with a key-value database. In this example, we're running a Redis cluster on "localhost" and exposing it over port 6379. To connect to this cluster, we'll use the Redis function from the imported redis library. We'll pass "localhost" to the host parameter and 6379 as the port. To ensure that we're not receiving data as bytes, we'll set decoded-responses to True. And just like that, we're ready to start working with key-value data. Later, we'll take a closer look at storing and retrieving key-value pairs using Redis' set and get functions.

5. Let's practice!

Excellent! It's time to reinforce all that we've discussed with a few exercises. Good luck!