Chapter 4 - Key-Value DB
Chapter 4 - Key-Value DB
CHAPTER 4
Key-Value Databases
Instructor: VŨ THỊ MỸ HẰNG, Ph.D.
Content
q Overview of Key-Value Databases
q DBMS: Redis
q Relational, Document, Columnar, or Key-Value Databases
BIT324E1 – Advanced Database Systems
Content
q Overview of Key-Value Databases
q DBMS: Redis
q Relational, Document, Columnar, or Key-Value Databases
BIT324E1 – Advanced Database Systems
q ASSOCIATIVE ARRAY
o A collection of key-value pairs.
o Access elements (values) by keys (strings, numbers).
o Fast lookups using hashing. KEY VALUE
BIT324E1 – Advanced Database Systems
Key-Value Databases
q In-memory storage consisting of key-value pairs.
o Key: Unique, string or any binary sequence used to identify a value in the database.
o Value: Data associated with a key, can be a simple string or number, or a complex
structure like a list, set, hash, or sorted set.
Link: https://redis.io/nosql/key-value-databases/
BIT324E1 – Advanced Database Systems
Content
q Overview of Key-Value Databases
q DBMS: Redis
q Relational, Document, Columnar, or Key-Value Databases
BIT324E1 – Advanced Database Systems
Redis
q Stand for Remote Dictionary Server.
q Developed by Salvatore Sanfilippo, an Italian Software Engineer in 2009.
q Widely used for:
• Caching
• User profile management
• Message queues
• Leaderboards
• Session management
q Redis Cheat Sheet: https://redis.io/learn/howtos/quick-start/cheat-sheet
BIT324E1 – Advanced Database Systems
Redis Architecture
q Master-Slave Distributed Model (vs Peer-to-peer).
o One node acts as the Master (or Primary)
§ Accepts writes/updates, replicates data to slaves
Data Types
q String: A sequence of bytes storing anything up to 512 megabytes in one string.
o Suitable for simple data like session tokens, page view counters.
q Hash: A collection of key-value pairs.
o Suitable for structured data like user profiles, configuration objects, products.
q List: An ordered collection of strings, sorted by insertion order.
o Suitable for data accessed as a stack or a queue like logs, chat messages.
q Set: An unordered collection of unique strings (no duplicates).
o Suitable for individual tracking.
q Sorted Set (ZSet): An ordered collections of strings, sorted by scores.
o Suitable for ranked data in ranking systems or recommendation systems.
BIT324E1 – Advanced Database Systems
Keyspace Management
q Any binary sequence can be use as a key.
q Rules about keys:
o Avoid very long key
L High memory usage, costly key-comparisons.
o Avoid very short key or unreadable key
LAmbiguity, hard to manage.
o Use consistency schema
Use keys that are clear, concise, and consistently structured – balance
readability and performance.
Link: https://redis.io/docs/latest/develop/use/keyspace/
BIT324E1 – Advanced Database Systems
Link: https://redis.io/docs/latest/develop/use/keyspace/
BIT324E1 – Advanced Database Systems
String Manipulation
Command Description Example
SET key value Set value to key SET name "Redis"
GET key Retrieve value GET name
DEL key Delete key DEL name
EXPIRE key s Set TTL (in seconds) EXPIRE session 30
TTL key Get remaining TTL TTL session
INCR key Increment value INCR counter
DECR key Decrement value DECR counter
APPEND key val Append value to existing string APPEND name "DB"
STRLEN key Get string length STRLEN name
BIT324E1 – Advanced Database Systems
Hash Manipulation
Command Description Example
HSET key field value Set hash field HSET user:1 name "Alice"
HGET key field Get hash field HGET user:1 name
HGETALL key Get all hash fields HGETALL user:1
BIT324E1 – Advanced Database Systems
List Manipulation
Command Description Example
LPUSH key val Push to list head LPUSH tasks "task1"
RPUSH key val Push to list tail RPUSH tasks "task2"
LPOP key Pop first list element LPOP tasks
RPOP key Pop last list element RPOP tasks
LRANGE key start end Get list slice LRANGE tasks 0 -1
BIT324E1 – Advanced Database Systems
Set Manipulation
Command Description Example
SADD key val Add to set SADD fruits apple
SREM key val Remove from set SREM fruits apple
SMEMBERS key List all set members SMEMBERS fruits
SISMEMBER key val Check if value is in set SISMEMBER fruits banana
BIT324E1 – Advanced Database Systems
Content
q Overview of Key-Value Databases
q DBMS: Redis
q Relational, Document, Columnar, or Key-Value Databases
BIT324E1 – Advanced Database Systems
Relational DB
BIT324E1 – Advanced Database Systems
Relational DB (2)
BIT324E1 – Advanced Database Systems
Document DB
BIT324E1 – Advanced Database Systems
Columnar DB
BIT324E1 – Advanced Database Systems
Key-Value DB
BIT324E1 – Advanced Database Systems
THANK YOU
FOR YOUR ATTENTION!
BIT324E1 – Advanced Database Systems