Learn about Redis and how its data types can be used to store and associate objects, and implement time-based and geolocation-based algorithms.
Examples:
Exercises:
Context
In contrast to a relational data model, where data is stored in rows with attributes and identified by a primary key, key-value stores are designed in a more simplistic fashion. They store a value for a key.
A key is typically a string. In contrast to primary keys in the relational data model, the name of a key is selected by the developer and carries meaning:
Key | Potential Meaning |
---|---|
visitors |
The overall number of visitors. |
projects |
A list of all projects. |
project_ids |
A list of all project ids. |
project:6473 |
The project with id 6473. The colon : between the name and the id is not predefined and therefore a random choice. |
project.289.subscriber_ids |
A list of subscriber ids for project 289. Here a dot . functions a separator. |
A value can be anything as key-value stores typically do not care about the type and content of the data. Often, key-value stores will store numbers, strings or binary values. The encoding and decoding of the value is up to the developer.
Value | Encoding |
---|---|
5 |
None |
[1, 3, 5] |
JSON |
<x>hello</x> |
XML |
010111011101010101000111101 |
Binary |
Key-value stores typically store their data using hash tables or Distributed Hash Table.
Traditional hash tables are data structures used to efficiently map keys to values. Each key is hashed using a hash function, which produces a unique identifier for the input. This identifier is then used to map the key to a specific slot in an array, where the corresponding value is stored.
When a key needs to be looked up, it is passed through the hash function to produce its identifier, which is then used to directly access the corresponding value in the array. This allows for constant-time lookups, regardless of the size of the data set. In contrast to distributed hash tables, traditional hash tables are typically implemented on a single machine and are not distributed across a network.
The aggregate value in a database can be either opaque or structured, depending on the type of database. In pure key-value databases (Redis), the aggregate value is an opaque blob of data with no inherent meaning. However, databases that support structured aggregates, such as document databases (MongoDB ) and wide-column stores (DynamoDB), use values with known and inspectable structures. This enables the database system to perform queries on a subset of an aggregate.
Redis is an open source, in-memory data store used by millions of developers as a database, cache, streaming engine, and message broker. In its most basic form, it is a key-value store, but has build in support for many more data types such as lists, sets, hashes, sorted sets, streams, geospatial indexes, bitmaps, bifields and more.