0% found this document useful (0 votes)
2 views

Chapter 4 - Key-Value DB

Uploaded by

yorupj941
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Chapter 4 - Key-Value DB

Uploaded by

yorupj941
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Course: BIT324E1 – Advanced Database Systems

CHAPTER 4

Key-Value Databases
Instructor: VŨ THỊ MỸ HẰNG, Ph.D.

HCM, Spring 2025


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

Content
q Overview of Key-Value Databases
q DBMS: Redis
q Relational, Document, Columnar, or Key-Value Databases
BIT324E1 – Advanced Database Systems

Recall: Array Storage


q ARRAY
o An ordered collection of elements stored at contiguous memory locations.
o Access elements by index (integer indices, starting from 0 in most case).

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

DBMS for Key-Value Databases


Example of DBMS
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

o One or more nodes act as Slaves (or Replicas)


§ Handles read-only requests; reduces load on master
BIT324E1 – Advanced Database Systems

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

Keyspace Naming Convention


q Groups related keys together using prefixes.
o namespace:id
o namespace:id:field
o…
q Use lowercase and consistent format.
q Separate segments with colons.
q Use hyphens or dots for multi-word.
q Include timestamps only when needed.
q E.g., user:001; cart:user:001; user:001:address

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

Ordered Set Manipulation


Command Description Example
ZADD key score val Add to sorted set ZADD leaderboard 100 "Alice"
ZRANGE key start end Get sorted set members ZRANGE leaderboard 0 -1 WITHSCORES
ZREM key val Remove from sorted set ZREM leaderboard "Alice"
KEYS pattern Find keys matching pattern KEYS user:*
FLUSHALL Delete all keys in all DBs FLUSHALL
DBSIZE Get number of keys in DB DBSIZE
PING Check if Redis is running PING
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

You might also like