π What is a Key-Value Store?
A Key-Value Store is the simplest type of NoSQL database.
It stores data as a collection of key-value pairs, like a dictionary or hash map.
π§± Structure:
Key β Value
"user:123" β { "name": "Alice", "age": 24 }
"session:456" β "active"
"cart:789" β [1001, 1002, 1003]The key is a unique identifier.
The value can be:
A string
A number
A JSON object
A blob (binary data)
Or anything else
βοΈ Features of Key-Value Stores
Feature | Description |
|---|---|
π Simple model | Only keys and values |
π Super fast | Optimized for quick read/write |
βοΈ Highly scalable | Supports horizontal scaling easily |
π No schema | Very flexible structure |
π No querying by value | Can only search by key (unless extended features are used) |
π§ Use Cases
Key-Value stores are ideal for:
Use Case | Description |
|---|---|
β Caching | Store frequently accessed data |
β Session Management | User sessions, login tokens |
β Shopping Carts | Store items in a user's cart |
β User Preferences | Store user settings, likes |
β Real-Time Counters | Page views, likes, etc. |
π§° Popular Key-Value Databases
Database | Description |
|---|---|
π₯ Redis | In-memory, extremely fast, supports TTL, pub/sub |
π¨ Amazon DynamoDB | Fully managed, highly scalable key-value + document store |
π¦ Riak KV | Distributed, fault-tolerant |
β« Aerospike | Low latency, real-time analytics |
π Example: Using Redis
SET user:1001 '{"name":"Swastika","age":22}'
GET user:1001π Advantages
β Advantage | Description |
|---|---|
Ultra fast | Ideal for real-time systems |
Easy to scale | Horizontally scalable |
Flexible | No fixed schema |
Low latency | Supports caching, quick retrieval |
β Limitations
β Limitation | Description |
|---|---|
No complex queries | You canβt search by value without custom logic |
No relations | Not suited for relational data |
Over-simplified | Not ideal for complex datasets or analytics |
Limited querying | Most support only get/set by key |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Key-Value Stores</title>
<style>
.infographic-kvdeep {
--ink: #1a1a1a;
--ink-soft: #5c5c5c;
--paper: #ffffff;
--paper-alt: #f0ece4;
--accent: #2563eb;
--accent-soft:#e3ebfd;
--line: #ddd6c9;
--good: #16a34a;
--good-soft: #dcfce7;
--bad: #dc2626;
--bad-soft: #fee2e2;
--on-accent: #ffffff;
--display-font: Georgia, 'Iowan Old Style', serif;
--body-font: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
--mono-font: 'SF Mono', Consolas, monospace;
background: var(--paper);
color: var(--ink);
font-family: var(--body-font);
max-width: 920px;
margin: 0 auto;
box-sizing: border-box;
overflow-x: hidden;
}
.infographic-kvdeep[data-theme="dark"] {
--ink: #e8e6e1;
--ink-soft: #9a968c;
--paper: #121212;
--paper-alt: #1e1e26;
--accent: #5b8def;
--accent-soft:#202942;
--line: #2c313d;
--good: #4ade80;
--good-soft: #123420;
--bad: #f87171;
--bad-soft: #3a1414;
--on-accent: #101418;
}
.infographic-kvdeep * { box-sizing: border-box; }
.kd-head { text-align: center; margin-bottom: 0.25rem; }
.kd-head h2 {
font-family: var(--display-font);
font-size: 1.95rem;
margin: 0 0 0.5rem;
}
.kd-head p {
color: var(--ink-soft);
font-size: 1rem;
margin: 0 auto;
max-width: 600px;
line-height: 1.55;
}
.kd-panel { margin-top: 1.5rem; }
.kd-frame { background: var(--paper-alt); border: 1px solid var(--line); border-radius: 10px; padding: 1.25rem; }
.kd-frame-title { font-family: var(--display-font); font-size: 1.02rem; margin: 0 0 0.85rem; }
.kd-frame-desc { font-size: 0.87rem; color: var(--ink-soft); line-height: 1.55; margin: 0 0 1rem; }
/* structure list */
.kd-struct-list { display: flex; flex-direction: column; gap: 0.5rem; }
.kd-struct-row { display: flex; gap: 0.6rem; align-items: center; flex-wrap: wrap; }
.kd-struct-key { font-family: var(--mono-font); font-size: 0.8rem; background: var(--accent); color: var(--on-accent); border-radius: 6px; padding: 0.3rem 0.6rem; flex-shrink: 0; }
.kd-struct-arrow { color: var(--ink-soft); }
.kd-struct-val { font-family: var(--mono-font); font-size: 0.8rem; background: var(--paper); border: 1px solid var(--line); border-radius: 6px; padding: 0.3rem 0.6rem; flex: 1; min-width: 0; }
/* terminal */
.kd-terminal-wrap { margin-top: 1.25rem; }
.kd-terminal-label { font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.05em; color: var(--ink-soft); margin: 0 0 0.5rem; }
.kd-terminal-scroll { overflow-x: auto; }
.kd-terminal {
background: var(--ink);
color: #d4e2ff;
border-radius: 8px 8px 0 0;
padding: 0.85rem 1rem;
font-family: var(--mono-font);
font-size: 0.8rem;
line-height: 1.7;
min-height: 3rem;
}
.infographic-kvdeep[data-theme="dark"] .kd-terminal { background: #05070a; }
.kd-terminal-line { opacity: 0; animation: kd-line-in 0.25s ease forwards; }
@keyframes kd-line-in { to { opacity: 1; } }
.kd-terminal-line.cmd { color: #9cc5ff; }
.kd-terminal-line.ok { color: #4ade80; }
.kd-terminal-line.err { color: #f87171; }
.kd-terminal-placeholder { color: #7c8aa8; font-style: italic; }
.kd-terminal-input-row {
display: flex;
background: var(--ink);
border-radius: 0 0 8px 8px;
padding: 0.6rem 0.8rem;
gap: 0.5rem;
}
.infographic-kvdeep[data-theme="dark"] .kd-terminal-input-row { background: #05070a; }
.kd-prompt { color: #7c8aa8; font-family: var(--mono-font); font-size: 0.85rem; }
.kd-terminal-input {
flex: 1;
min-width: 0;
background: transparent;
border: none;
outline: none;
color: #d4e2ff;
font-family: var(--mono-font);
font-size: 0.85rem;
}
.kd-try-row { display: flex; gap: 0.5rem; flex-wrap: wrap; margin-top: 0.7rem; }
.kd-try-btn {
font: inherit;
font-size: 0.76rem;
background: var(--paper);
border: 1px solid var(--line);
border-radius: 6px;
padding: 0.35rem 0.7rem;
cursor: pointer;
color: var(--ink);
font-family: var(--mono-font);
}
.kd-try-btn:hover { border-color: var(--accent); }
@media (prefers-reduced-motion: reduce) {
.infographic-kvdeep *, .infographic-kvdeep *::before, .infographic-kvdeep *::after {
transition-duration: 0.001ms !important;
animation-duration: 0.001ms !important;
}
}
</style>
</head>
<body>
<div class="infographic-kvdeep" id="kvdeep">
<div class="kd-head">
<h2>Key-Value Stores</h2>
<p>The simplest type of NoSQL database β data stored as a collection of key-value pairs, like a dictionary or hash map.</p>
</div>
<!-- Structure -->
<div class="kd-panel is-active" data-panel="structure">
<div class="kd-frame">
<p class="kd-frame-title">Key β Value</p>
<p class="kd-frame-desc">The key is a unique identifier. The value can be a string, number, JSON object, binary blob β anything at all.</p>
<div class="kd-struct-list">
<div class="kd-struct-row"><span class="kd-struct-key">user:123</span><span class="kd-struct-arrow">β</span><span class="kd-struct-val">{"name":"Alice","age":24}</span></div>
<div class="kd-struct-row"><span class="kd-struct-key">session:456</span><span class="kd-struct-arrow">β</span><span class="kd-struct-val">"active"</span></div>
<div class="kd-struct-row"><span class="kd-struct-key">cart:789</span><span class="kd-struct-arrow">β</span><span class="kd-struct-val">[1001, 1002, 1003]</span></div>
</div>
<div class="kd-terminal-wrap">
<p class="kd-terminal-label">Try it β type a Redis-style command and press Enter</p>
<div class="kd-terminal-scroll">
<div class="kd-terminal" id="kdTerminal"><span class="kd-terminal-placeholder">Try: SET user:1001 {"name":"Swastika","age":22}</span></div>
<div class="kd-terminal-input-row">
<span class="kd-prompt">></span>
<input class="kd-terminal-input" id="kdInput" type="text" placeholder='SET user:1001 {"name":"Swastika","age":22}' autocomplete="off">
</div>
</div>
<div class="kd-try-row">
<button class="kd-try-btn" data-cmd='SET user:1001 {"name":"Swastika","age":22}'>SET user:1001 ...</button>
<button class="kd-try-btn" data-cmd="GET user:1001">GET user:1001</button>
<button class="kd-try-btn" data-cmd="GET user:999">GET user:999 (missing)</button>
<button class="kd-try-btn" data-cmd="DEL user:1001">DEL user:1001</button>
</div>
</div>
</div>
</div>
</div>
<script>
(function () {
const wrapper = document.getElementById('kvdeep');
const bodyAttr = document.body.getAttribute('data-darkmode');
let dark;
if (bodyAttr === 'true') {
dark = true;
} else if (bodyAttr === 'false') {
dark = false;
} else {
const match = document.cookie.match(/(?:^|;\s*)darkMode=([^;]+)/);
dark = match ? match[1] === 'true' : false;
}
if (dark) wrapper.setAttribute('data-theme', 'dark');
})();
(function () {
let store = {};
const term = document.getElementById('kdTerminal');
const input = document.getElementById('kdInput');
function printLine(text, cls) {
if (term.querySelector('.kd-terminal-placeholder')) term.innerHTML = '';
const line = document.createElement('div');
line.className = 'kd-terminal-line' + (cls ? ' ' + cls : '');
line.textContent = text;
term.appendChild(line);
term.scrollTop = term.scrollHeight;
}
function runCommand(raw) {
const text = raw.trim();
if (!text) return;
printLine('> ' + text, 'cmd');
const spaceIdx = text.indexOf(' ');
const cmd = (spaceIdx === -1 ? text : text.slice(0, spaceIdx)).toUpperCase();
const rest = spaceIdx === -1 ? '' : text.slice(spaceIdx + 1).trim();
if (cmd === 'SET') {
const kIdx = rest.indexOf(' ');
if (kIdx === -1) { printLine('(error) wrong number of arguments for SET', 'err'); return; }
const key = rest.slice(0, kIdx);
const val = rest.slice(kIdx + 1).trim();
store[key] = val;
printLine('OK', 'ok');
} else if (cmd === 'GET') {
const key = rest.trim();
if (key in store) {
printLine('"' + store[key] + '"', 'ok');
} else {
printLine('(nil)', 'err');
}
} else if (cmd === 'DEL') {
const key = rest.trim();
if (key in store) {
delete store[key];
printLine('(integer) 1', 'ok');
} else {
printLine('(integer) 0', 'err');
}
} else {
printLine('(error) unknown command \u2018' + cmd + '\u2019 β try SET, GET, or DEL', 'err');
}
}
input.addEventListener('keydown', (e) => {
if (e.key === 'Enter') {
runCommand(input.value);
input.value = '';
}
});
document.querySelectorAll('.kd-try-btn').forEach(btn => {
btn.addEventListener('click', () => runCommand(btn.dataset.cmd));
});
})();
</script>
</body>
</html>.demo {
font-family: system-ui, sans-serif;
padding: 16px;
}
.demo h1 {
color: #2563eb;
margin: 0 0 8px;
}
.demo p {
color: #4b5563;
margin: 0;
}document.querySelector('.demo h1')?.addEventListener('click', () => {
alert('Hello from live preview!');
});