Skip to main content

whereq.com 开源了:地图、爬虫,以及它读到的所有数据

· 8 min read
whereq.comwhereq.com

一枚图钉,其实只是一句很小的话​

站在多伦多的一条街上,看着眼前这个门牌号,地图只会告诉你一句话:这里有一枚图钉。一家咖啡馆,一间医院,一所学校,也许还有一个停车场。

但一个地方本身是一大堆话,而图钉是其中最不重要的那句。

这个地址属于哪个学区 catchment?走路能到图书馆吗,是哪一家?街角加油站今天收 1.42 还是 1.79?这栋楼落在 ward 里、neighbourhood 里,还是两者都算?上周这条街上出过什么事?

whereq.com is open source — the map, the crawler, and everything it reads

· 8 min read
whereq.comwhereq.com

A pin is a very small sentence​

Stand on a street in Toronto and look at the address in front of you. A map gives you one claim about it: there is a pin here. A coffee shop. A hospital. A school. Maybe a parking garage.

But the place itself is a pile of claims, and the pin is the least interesting one of them.

Which school catchment is that address in? Can I walk to a library, and which one? Is the fuel station on the corner charging 1.42or1.42 or 1.79? Is that building inside a ward, or inside a neighbourhood, or in a ward and a neighbourhood? Did anything happen on this street last week?

Inside the WhereQ Universe: One Identity, Many Apps, Open APIs

· 7 min read

The WhereQ Universe is not a single product — it is a living ecosystem of independent applications that share one identity layer, one set of engineering conventions, and an increasingly open set of APIs. This post walks through how the pieces fit together: why authentication lives in a single place, how the apps expose data to customers, and where LLM agents and MCP fit into the picture.

Hermes Agent vs OpenClaw:全面对比

· 37 min read
Hermes Agent

Hermes

vs🦞 OpenClaw

从所有维度进行全面深度对比 — 架构、安全、记忆、技能、平台、成本等
更新于 2026 年 4 月

01 起源与哲学

Hermes Agent创建者:Nous Research发布时间:2026 年 2 月语言:Python许可证:MIT设计哲学:"学习最大化"通过自我进化技能、深度记忆、持续学习让智能体随时间变得更强🦞 OpenClaw创建者:Peter Steinberger发布时间:2025 年 11 月(Clawdbot)语言:Node.js(TypeScript)许可证:MIT设计哲学:"连接最大化"连接一切、无处不在,最大化平台覆盖、广泛生态、社区技能
图 1:两种截然不同的理念 — Hermes 押注认知深度,OpenClaw 押注连接广度。

OpenClaw 定义了个人 AI 智能体品类。最初名为 Clawdbot(2025 年 11 月),因商标问题先后改名为 Moltbot 和 OpenClaw。创始人 Peter Steinberger 于 2026 年 2 月加入 OpenAI,项目转由非营利基金会社区治理。

Hermes Agent 两个月后由 Nous Research 发布 — 该实验室是 Hermes、Nomos、Psyche 模型家族的创造者。OpenClaw 优化的是连接尽可能多的平台和工具,而 Hermes 优化的是深度:让智能体通过使用真正学习和进化。

02 架构设计

架构核心对比Hermes:Agent 循环 = 核心AIAgent (run_agent.py)记忆层技能工具网关(可选,扇出分发)• 同步编排引擎• 网关是外围组件,不是核心• 学习循环嵌入 Agent 周期OpenClaw:Gateway = 核心Gateway(控制平面)会话路由频道Pi AgentWebSocket ws://127.0.0.1:18789• Gateway 是"神经系统"• 所有消息通过 Gateway 流转• Agent 运行时是 Gateway 子系统
图 2:Hermes 以 Agent 循环为中心;OpenClaw 以 Gateway 控制平面为中心。
维度 Hermes🦞 OpenClaw
核心AIAgent 循环(Python,约 9,200 行)Gateway 控制平面(Node.js/TS)
入口hermes(CLI)+ hermes gatewayopenclaw CLI + Gateway 守护进程
状态存储SQLite + FTS5 + 扁平文件JSON 配置 + 工作区文件
协议直接 LLM API 调用WebSocket 控制平面
插件模型Python 工具 + MCP 服务器Node.js 技能 + ClawHub 注册表
技术栈PythonNode.js / TypeScript

03 学习与自我进化 HERMES 胜出

这是最大的差异化特性。Hermes 拥有其他智能体框架无法复制的闭环学习循环。OpenClaw 的技能是静态的 — 你写了它们,除非手动更新,否则永远不变。

Hermes:闭环学习循环执行任务评估结果更新记忆创建/改进技能↻自主 • 持续 • 复利效应OpenClaw:静态技能人工编写技能Agent 使用技能技能永远不变除非人工编辑手动 • 静态 • 无复利效应
图 3:Hermes 自主创建并改进技能;OpenClaw 完全依赖人工编写的静态技能。
能力 Hermes🦞 OpenClaw
技能创建复杂任务后自主创建(5+ 工具调用)仅手动(人工编写 SKILL.md)
技能改进使用过程中自我改进无(静态文件)
用户建模Honcho 辩证建模,跨会话积累SOUL.md 静态身份文件
周期性提示提示 Agent 反思并持久化有用信息无等效机制
跨会话召回FTS5 搜索 + LLM 摘要按助手持久记忆
实测改进重复任务速度提升 40%无复利效果

04 记忆系统 HERMES 胜出

层级 Hermes🦞 OpenClaw
持久事实MEMORY.md(约 2,200 字符,会话开始冻结)按助手存储记忆
用户画像USER.md(偏好、风格)SOUL.md(身份和人格)
程序性知识技能库(自动创建、可搜索)技能(人工编写、静态)
情景记忆SQLite + FTS5 全文搜索,无限量按助手的会话持久化
摘要LLM 驱动的历史会话摘要无等效功能
外部提供商7 个可插拔提供商(Hindsight、PLUR 等)无可插拔记忆系统
上下文压缩有损摘要引擎(可插拔)通过 Gateway 管理上下文
提示词缓存Anthropic 前缀缓存,冻结快照特定提供商缓存
团队共享PLUR 记忆碎片跨实例共享按助手隔离(内置)

Hermes 将记忆视为分层系统:始终在上下文中的事实、按需加载的程序性技能、可搜索的情景历史。OpenClaw 的记忆更简单但对团队隔离有效 — 每个助手有自己独立的存储,防止多代理设置中的交叉污染。

05 技能生态 OPENCLAW 胜出

维度 Hermes🦞 OpenClaw
技能总数70+ 内置,社区增长中ClawHub 13,700+(报称 44K)
技能格式agentskills.io 开放标准(Markdown)自定义 SKILL.md 格式(Markdown + 脚本)
技能来源Agent 自动创建 + 社区 + Skills Hub人工编写 + ClawHub 市场
加载方式渐进式披露(L0/L1/L2)启动时加载
自我改进技能在使用中演化写完就是静态的
供应链风险Tirith 预扫描 + 第三方需 --force高 — 发现 1,467+ 恶意技能(ClawHavoc)
审查扫描判定默认阻止危险技能VirusTotal 扫描(危机后添加),早期审查极少

OpenClaw 的生态系统在绝对数量上远超 Hermes。ClawHub 有数千个社区技能,覆盖从 Gmail 到 Kubernetes 的各种场景。但这种规模带来了严重的安全代价 — ClawHavoc 供应链攻击和 1,467+ 确认的恶意技能代表严重的信任问题。Hermes 用广度换安全:更少的技能,但有预执行扫描和更安全的默认姿态。

06 消息平台支持 OPENCLAW 胜出

平台 Hermes(15+)🦞 OpenClaw(25+)
核心平台Telegram、Discord、Slack、WhatsApp、SignalTelegram、Discord、Slack、WhatsApp、Signal
企业通讯企业微信 (WeCom)、钉钉、飞书企业微信、飞书、Microsoft Teams、Google Chat
中国生态企业微信、微信(HermesClaw 桥接)、钉钉、飞书微信(官方腾讯插件)、飞书
日韩东南亚无LINE、Zalo、Zalo Personal
开源/极客Matrix、MattermostMatrix、Mattermost、IRC、Nostr、Tlon、Twitch
苹果生态BlueBubbles (iMessage)BlueBubbles + iMessage (legacy) + macOS App
物联网Home AssistantSynology Chat、Nextcloud Talk
邮件/短信Email (IMAP/SMTP)、SMS (Twilio)WebChat(内置)
原生应用无macOS 菜单栏、iOS App、Android App
跨平台连续性语义上下文恢复(核心优势)基于 Gateway 的会话持久化

OpenClaw 支持 LINE、Google Chat、Microsoft Teams、IRC、Twitch、Nostr、Zalo 以及原生 iOS/Android/macOS 配套应用 — Hermes 目前均不支持。Hermes 的优势在于跨平台对话连续性(语义上下文恢复)和更深的单平台集成(Discord 语音频道、Telegram Webhook 模式、企业微信 WebSocket)。

07 模型支持 HERMES 胜出

维度 Hermes🦞 OpenClaw
提供商数量18+ 内置提供商主要提供商(Claude、GPT、Gemini、Groq、Mistral、xAI)
OpenRouter200+ 模型统一计费支持
本地模型Ollama、vLLM、SGLang、llama.cppOllama + 自定义端点
中国提供商MiniMax、Kimi/月之暗面、智谱 GLM、DashScope、小米 MiMo有限支持
MiniMax一流合作伙伴(M2.7 直接集成)通过 OpenRouter
切换hermes model — 零代码更改编辑配置文件 + 重启
故障转移错误时自动切换备用模型(会话内)无自动故障转移
每代理路由单模型 + 备用不同代理可用不同模型
最低上下文要求 64K tokens无强制最低要求
辅助 LLM独立模型用于视觉、摘要使用主模型

08 安全性 HERMES 胜出

这是对比中最触目惊心的部分。OpenClaw 经历了 2026 年最严重的 AI 安全危机之一。

0
Hermes Agent CVE 数
138+
OpenClaw 已追踪 CVE
9.9
最高 CVSS 评分 (OpenClaw)
1,467
ClawHub 恶意技能
42K+
暴露的 OpenClaw 实例
63%
未启用认证
安全维度 Hermes🦞 OpenClaw
CVE 数量0 个智能体特定 CVE138+ 已追踪(7 个严重,49 个高危)
最严重 CVE无CVE-2026-32922(CVSS 9.9)— 权限提升
供应链Tirith 预执行扫描,第三方需 --forceClawHavoc:1,467+ 恶意技能,AMOS 窃取器分发
默认绑定安全默认值早期版本绑定到 0.0.0.0(所有网络接口)
认证默认需要允许列表/配对部分版本无认证运行
容器加固只读根 FS、丢弃 caps、命名空间隔离(默认)可选 Docker 沙箱
文件系统保护自动检查点 + 回滚基于工作区的隔离
命令扫描Tirith 预执行扫描器Exec 审批系统(CVE 后添加)
记忆注入条目扫描威胁模式无等效扫描
遥测零 — 架构级设计零 — 本地优先设计
政府警告无中国、比利时、韩国发布限制令

重要背景:Hermes 更年轻,攻击面更小(64K vs 345K Stars)。它还没有经受同等级别的审查。但其安全优先的架构决策 — Docker 加固作为默认而非选项、第三方技能强制预扫描、记忆注入扫描 — 代表了一种从根本上更注重安全的设计。

09 部署与基础设施 HERMES 胜出

维度 Hermes🦞 OpenClaw
终端后端6 种:Local、Docker、SSH、Daytona、Singularity、ModalLocal、Docker
ServerlessDaytona + Modal(空闲近零成本)无原生 Serverless
安装一行 curl 脚本npm install -g openclaw(需 Node.js 22+)
OS 支持Linux、macOS、WSL2、Termux (Android)Linux、macOS、Windows(原生)
Windows 原生不支持(仅 WSL2)原生支持
HPC 集群Singularity/Apptainer 支持无 HPC 支持
托管服务社区提供商涌现中DigitalOcean 一键部署($24/月)、OpenClaw VPS、OneClaw
服务安装hermes gateway install(systemd/launchd)Systemd / LaunchAgent
配套应用仅 CLI(ACP 用于编辑器)macOS 菜单栏、iOS App、Android App

10 开发者体验 平手

功能 Hermes🦞 OpenClaw
CLI/TUI完整 TUI,多行编辑、自动补全、流式传输CLI + 引导向导
斜杠命令CLI 和网关共享跨频道共享
MCP 支持原生(stdio + HTTP、重连、采样)原生 MCP 支持
IDE 集成ACP 服务器(VS Code、Zed、JetBrains)无原生 IDE 集成
浏览器控制网页搜索 + 浏览器自动化 + 视觉CDP 浏览器控制(Chrome/Chromium)
语音模式CLI 麦克风、TTS 回复、Discord 语音频道Voice Wake、推说话、Talk Mode 浮层
编程 APIfrom run_agent import AIAgentWebSocket API + REST 端点
配置文件hermes profile create — 隔离环境通过 Gateway 多代理路由
诊断hermes doctoropenclaw doctor

11 成本分析 HERMES 胜出

组成 Hermes🦞 OpenClaw
框架$0(MIT)$0(MIT)
托管$5/月 VPS 或约 $0 Serverless(Modal/Daytona)$5/月 VPS 或 $24/月 托管(DigitalOcean)
LLM API$15–80/月 典型$15–80/月 典型
每次调用开销约 73% 固定开销(工具定义)类似开销比例
空闲成本Modal/Daytona 休眠约 $0全额 VPS 费用,即使空闲
典型总计$10–25/月(个人)$20–40/月(个人,无托管)

Hermes 的 Serverless 后端(Modal、Daytona)对于不需要 24/7 运行智能体的用户来说具有显著成本优势。如果你的智能体每天运行 8 小时,你只为 8 小时付费,而不是 24 小时。

12 社区与生态 OPENCLAW 胜出

指标 Hermes🦞 OpenClaw
GitHub Stars64K+(2 个月)345K+(5 个月)
贡献者142+数千
技能市场Skills Hub(增长中)ClawHub(13,700+ 技能)
企业支持无(仅社区)NVIDIA NemoClaw 合作
YouTube 内容增长中大量教程和评测
治理Nous Research(活跃实验室)非营利基金会(创始人已加入 OpenAI)
增长轨迹v0.8.0 后单日 6,400+ Stars初始病毒式传播 72 小时 60K+ Stars

OpenClaw 的社区是 AI 智能体领域最大的。大量的教程、第三方托管提供商、技能和集成形成了网络效应。但 Hermes 的增长速度表明它可能在一年内挑战 OpenClaw — 尤其是随着开发者因安全问题而迁移。

13 企业与团队使用 OPENCLAW 胜出

功能 Hermes🦞 OpenClaw
多代理子代理并行工作Gateway 多代理路由(按工作区隔离)
团队聊天共享 Discord/Slack 频道Gateway 路由到按频道/账户隔离的代理
数据隔离按 Profile 隔离按助手隔离存储(内置)
NVIDIA 支持无NemoClaw 合作
托管服务社区提供商DigitalOcean、OpenClaw VPS、多个托管方
配套应用无macOS、iOS、Android 配套应用
可视化工作区无Live Canvas(代理驱动的可视化工作区)

14 研究能力 HERMES 胜出

能力 Hermes🦞 OpenClaw
批量轨迹内置并行轨迹生成无等效功能
RL 训练Atropos RL 环境无 RL 基础设施
轨迹导出压缩 + 导出用于微调无训练数据导出
可配置工作者批量大小、工具集分布无批处理
构建者Nous Research(模型训练实验室)社区(创始人已加入 OpenAI)

Hermes 由模型训练实验室构建,从一开始就设计为研究基础设施 — 不仅仅是面向用户的产品。OpenClaw 没有等效的研究能力。

15 迁移路径

OpenClaw → Hermes:内置迁移工具

hermes claw migrate 可导入你的人格(SOUL.md)、记忆、技能(自动转换为 agentskills.io 格式)、消息平台配置、API 密钥和 TTS 资源。支持交互式预览和 dry-run。技能冲突通过 skip/overwrite/rename 选项处理。可以在过渡期间同时运行两个代理。

🦞 Hermes → OpenClaw:手动过程

无内置迁移工具。需要手动映射记忆文件、技能格式转换和频道重新配置。自我进化技能系统在 OpenClaw 中没有等效物 — 你会失去复利自动化价值。预估工作量:12–21 小时。

16 最终结论

各类别胜出汇总学习与自我进化记忆系统安全性模型支持部署选项成本效率研究 / RL平台覆盖技能生态规模社区规模企业 / 团队配套应用开发者体验迁移工具HERMESHERMESHERMESHERMESHERMESHERMESHERMESOPENCLAWOPENCLAWOPENCLAWOPENCLAWOPENCLAW平手HERMESHermes:8 项胜出OpenClaw:5 项胜出
图 4:14 个对比维度的逐类别胜出汇总。

选择 Hermes 当你...

  • 希望智能体随时间真正变得更强
  • 优先考虑安全性(零 CVE,Docker 默认加固)
  • 需要数月交互的深度记忆
  • 想要 Serverless 部署,空闲近零成本
  • 使用中国 LLM 提供商(MiniMax、GLM、Kimi)
  • 是需要 RL 训练 / 轨迹生成的研究者
  • 想从 OpenClaw 迁移(内置迁移工具)

🦞 选择 OpenClaw 当你...

  • 需要最广泛的平台覆盖(25+)
  • 想要庞大的技能生态(ClawHub 13,700+)
  • 需要企业支持(NVIDIA NemoClaw)
  • 需要原生 Windows 支持
  • 想要配套应用(macOS、iOS、Android)
  • 需要多代理路由和按助手数据隔离
  • 已有成熟的 OpenClaw 部署
🤝 或者两个一起用

由于 Hermes 使用 Python 而 OpenClaw 使用 Node.js,它们在运行时不冲突。你可以将不同频道路由到不同代理,用 OpenClaw 覆盖广泛的消息平台,同时用 Hermes 处理受益于其学习循环的深度工作任务。hermes claw migrate 工具支持你准备好时的渐进式迁移。

Hermes Agent vs OpenClaw: Full Comparison

· 39 min read
Hermes Agent

Hermes

vs🦞 OpenClaw

A comprehensive comparison from every perspective — architecture, security, memory, skills, platforms, cost, and more.
Updated April 2026

01 Origins & Philosophy

Hermes AgentCreated by: Nous ResearchReleased: February 2026Language: PythonLicense: MITPhilosophy:"Learning Maximalist"Make the agent smarter over time throughself-improving skills, deep memory, persistent learning🦞 OpenClawCreated by: Peter SteinbergerReleased: November 2025 (as Clawdbot)Language: Node.js (TypeScript)License: MITPhilosophy:"Connectivity Maximalist"Connect to everything, everywhere. Maximumplatform reach, broad ecosystem, community skills
Fig 1: Two fundamentally different bets on what an AI agent should prioritize.

OpenClaw defined the personal AI agent category. Originally called Clawdbot (Nov 2025), it was renamed Moltbot and then OpenClaw after trademark concerns. Creator Peter Steinberger joined OpenAI in February 2026, and the project moved to community governance under a non-profit foundation.

Hermes Agent arrived two months later from Nous Research — the lab behind the Hermes, Nomos, and Psyche model families. Where OpenClaw optimized for connecting to as many platforms and tools as possible, Hermes optimized for depth: making the agent genuinely learn and improve through use.

02 Architecture

Architectural Core ComparisonHermes: Agent Loop = CoreAIAgent (run_agent.py)Memory LayerSkillsToolsGateway (optional, fan-out)• Synchronous orchestration engine• Gateway is a peripheral, not the core• Learning loop embedded in agent cycleOpenClaw: Gateway = CoreGateway (control plane)Session RouterChannelsPi AgentWebSocket ws://127.0.0.1:18789• Gateway is the "nervous system"• All messages flow through Gateway• Agent runtime is a Gateway subsystem
Fig 2: Hermes centers on the agent loop; OpenClaw centers on the gateway control plane.
Aspect Hermes🦞 OpenClaw
CoreAIAgent loop (Python, ~9,200 lines)Gateway control plane (Node.js/TS)
Entry pointshermes (CLI) + hermes gatewayopenclaw CLI + Gateway daemon
State storageSQLite + FTS5 + flat filesJSON config + workspace files
ProtocolDirect LLM API callsWebSocket control plane
Plugin modelPython tools + MCP serversNode.js skills + ClawHub registry
CodebasePython (primary)Node.js / TypeScript

03 Learning & Self-Improvement HERMES WINS

This is the single biggest differentiator. Hermes has a closed learning loop that no other agent framework replicates. OpenClaw skills are static — you write them, they stay the same forever unless you manually update them.

Hermes: Closed Learning LoopExecute TaskEvaluate ResultUpdate MemoryCreate/Refine Skill↻Autonomous • Continuous • CompoundingOpenClaw: Static SkillsHuman Writes SkillAgent Uses SkillSkill never changesunless human edits itManual • Static • No compounding
Fig 3: Hermes creates and refines skills autonomously; OpenClaw relies entirely on human-authored static skills.
Capability Hermes🦞 OpenClaw
Skill creationAutonomous after complex tasks (5+ tool calls)Manual only (human writes SKILL.md)
Skill improvementSelf-improving during useNone (static files)
User modelingHoncho dialectic modeling across sessionsSOUL.md static identity file
Periodic nudgesAgent prompted to reflect and persistNo equivalent
Cross-session recallFTS5 search + LLM summarizationPersistent memory per assistant
Reported improvement40% speed improvement on repeated tasksNo compounding effect

04 Memory Systems HERMES WINS

Layer Hermes🦞 OpenClaw
Persistent factsMEMORY.md (~2,200 chars, frozen at session start)Per-assistant memory storage
User profileUSER.md (preferences, style)SOUL.md (identity & personality)
ProceduralSkills library (auto-created, searchable)Skills (human-authored, static)
EpisodicSQLite + FTS5 full-text search, unlimitedSession persistence per assistant
SummarizationLLM-powered summarization of past sessionsNo equivalent
External providers7 pluggable providers (Hindsight, PLUR, etc.)No pluggable memory system
Context compressionLossy summarization engine (pluggable)Context management via Gateway
Prompt cachingAnthropic prefix caching, frozen snapshotProvider-specific caching
Team sharingPLUR engrams across instancesPer-assistant isolation (built-in)

Hermes treats memory as a layered system: always-in-context facts, on-demand procedural skills, and searchable episodic history. OpenClaw's memory is simpler but effective for team isolation — each assistant has its own separate storage, preventing cross-contamination in multi-agent setups.

05 Skills Ecosystem OPENCLAW WINS

Aspect Hermes🦞 OpenClaw
Total skills70+ bundled, growing community13,700+ on ClawHub (44K reported)
Skill formatagentskills.io open standard (Markdown)Custom SKILL.md format (Markdown + scripts)
Skill sourceAuto-created by agent + community + Skills HubHuman-authored + ClawHub marketplace
LoadingProgressive disclosure (L0/L1/L2)Loaded at startup
Self-improvementSkills evolve during useStatic once written
Supply chain riskTirith pre-scan + --force for third-partyHigh — 1,467+ malicious skills found (ClawHavoc)
VettingScan verdicts block dangerous skills by defaultVirusTotal scanning (added post-crisis), minimal early vetting

OpenClaw's ecosystem is far larger in raw numbers. ClawHub has thousands of community skills covering everything from Gmail to Kubernetes. But that scale came with severe security costs — the ClawHavoc supply chain attack and 1,467+ confirmed malicious skills represent a serious trust problem. Hermes trades breadth for safety: fewer skills, but with pre-execution scanning and a safer default posture.

06 Messaging Platform Support OPENCLAW WINS

Platform CoverageHermes — 15+ platformsTelegramDiscordSlackWhatsAppSignalMatrixMattermostEmail (IMAP)SMS (Twilio)DingTalkFeishu / LarkWeCom 企业微信Weixin 微信BlueBubblesHome Assistant🦞 OpenClaw — 25+ platformsTelegramDiscordSlackWhatsAppSignalGoogle ChatMS TeamsiMessageBlueBubblesMatrixMattermostIRCLINEFeishuWeChatNostrTlonTwitchZaloZalo PersonalSynology ChatNextcloud TalkWebChatmacOS AppiOS / Android
Fig 4: OpenClaw covers ~25 platforms; Hermes covers 15+ with deeper integration per platform.

OpenClaw has LINE, Google Chat, Microsoft Teams, IRC, Twitch, Nostr, Zalo, and native iOS/Android/macOS companion apps — none of which Hermes currently supports. Hermes's advantage is cross-platform conversation continuity with semantic context restore, and deeper per-platform integration (Discord voice channels, Telegram webhook mode, WeCom WebSocket).

07 Model Support HERMES WINS

Aspect Hermes🦞 OpenClaw
Provider count18+ built-in providersMajor providers (Claude, GPT, Gemini, Groq, Mistral, xAI)
OpenRouter200+ models via unified billingSupported
Local modelsOllama, vLLM, SGLang, llama.cppOllama + custom endpoints
China providersMiniMax, Kimi/Moonshot, z.ai/GLM, DashScope, XiaomiLimited
MiniMaxFirst-class partner (M2.7 direct integration)Via OpenRouter
Switchinghermes model — zero code changeConfig file edit + restart
Fallback modelAuto-fallback mid-session on errorNo automatic fallback
Per-agent routingSingle model + fallbackDifferent models per agent
Minimum context64K tokens requiredNo enforced minimum
Auxiliary LLMSeparate model for vision, summarizationUses primary model

08 Security HERMES WINS

This is where the comparison becomes most stark. OpenClaw has faced one of the most significant AI security crises of 2026.

0
Hermes Agent CVEs
138+
OpenClaw CVEs Tracked
9.9
Highest CVSS Score (OpenClaw)
1,467
Malicious ClawHub Skills
42K+
Exposed OpenClaw Instances
63%
With Auth Disabled
Security Aspect Hermes🦞 OpenClaw
CVE count0 agent-specific CVEs138+ tracked (7 critical, 49 high)
Worst CVENoneCVE-2026-32922 (CVSS 9.9) — privilege escalation
Supply chainTirith pre-execution scan, --force requiredClawHavoc: 1,467+ malicious skills, AMOS stealer distribution
Default bindingSafe defaultsEarly versions bound to 0.0.0.0 (all interfaces)
Auth defaultAllowlist/pairing requiredSome versions ran without auth
Container hardeningRead-only root FS, dropped caps, namespace isolation (defaults)Optional Docker sandboxing
Filesystem protectionAutomatic checkpoints + rollbackWorkspace-based isolation
Command scanningTirith pre-execution scannerExec approval system (added post-CVE)
Memory injectionEntries scanned for threat patternsNo equivalent scanning
TelemetryZero — architectural designZero — local-first design
Government warningsNoneChina, Belgium, South Korea issued restrictions

Important context: Hermes is younger and has a smaller attack surface (64K stars vs 345K). It hasn't faced the same level of scrutiny. However, its safer-by-default architectural decisions — Docker hardening as defaults rather than options, mandatory pre-scan of third-party skills, memory injection scanning — represent a fundamentally more security-conscious design.

09 Deployment & Infrastructure HERMES WINS

Aspect Hermes🦞 OpenClaw
Terminal backends6: Local, Docker, SSH, Daytona, Singularity, ModalLocal, Docker
ServerlessDaytona + Modal (near-zero idle cost)No native serverless
InstallationOne-line curl scriptnpm install -g openclaw (Node.js 22+)
OS supportLinux, macOS, WSL2, Termux (Android)Linux, macOS, Windows (native)
Windows nativeNot supported (WSL2 only)Supported natively
HPC clustersSingularity/Apptainer supportNo HPC support
Managed hostingCommunity providers emergingDigitalOcean 1-Click ($24/mo), OpenClaw VPS, OneClaw
Service installhermes gateway install (systemd/launchd)Systemd / LaunchAgent
Companion appsCLI only (ACP for editors)macOS menu bar, iOS app, Android app

10 Developer Experience TIE

Feature Hermes🦞 OpenClaw
CLI/TUIFull TUI with multiline, autocomplete, streamingCLI with onboarding wizard
Slash commandsShared across CLI and gatewayShared across channels
MCP supportNative (stdio + HTTP, reconnection, sampling)Native MCP support
IDE integrationACP server (VS Code, Zed, JetBrains)No native IDE integration
Browser controlWeb search + browser automation + visionCDP browser control (Chrome/Chromium)
Voice modeCLI mic, TTS replies, Discord voice channelsVoice Wake, Push-to-Talk, Talk Mode overlay
Programmatic APIfrom run_agent import AIAgentWebSocket API + REST endpoints
Profileshermes profile create — isolated environmentsMulti-agent routing via Gateway
Diagnosticshermes doctoropenclaw doctor

11 Cost Analysis HERMES WINS

Component Hermes🦞 OpenClaw
Framework$0 (MIT)$0 (MIT)
Hosting$5/mo VPS or ~$0 serverless (Modal/Daytona)$5/mo VPS or $24/mo managed (DigitalOcean)
LLM API$15–80/mo typical$15–80/mo typical
Overhead per call~73% fixed overhead (tool definitions)Similar overhead profile
Idle cost~$0 with Modal/Daytona hibernationFull VPS cost even when idle
Typical total$10–25/mo (personal)$20–40/mo (personal, no managed)

Hermes's serverless backends (Modal, Daytona) give it a significant cost edge for users who don't need their agent running 24/7. If your agent runs 8 hours/day, you pay for 8 hours instead of 24.

12 Community & Ecosystem OPENCLAW WINS

Metric Hermes🦞 OpenClaw
GitHub stars64K+ (2 months)345K+ (5 months)
Contributors142+Thousands
Skills marketplaceSkills Hub (growing)ClawHub (13,700+ skills)
Enterprise supportNone (community only)NVIDIA NemoClaw partnership
YouTube contentGrowingExtensive tutorials and reviews
GovernanceNous Research (active lab)Non-profit foundation (founder left for OpenAI)
Growth trajectory6,400+ stars in single day (post v0.8.0)60K+ stars in 72 hours (initial viral wave)

OpenClaw's community is the largest in the AI agent space. The sheer volume of tutorials, third-party hosting providers, skills, and integrations creates a network effect. However, Hermes is growing at a pace that suggests it could challenge OpenClaw within a year — especially as developers migrate due to security concerns.

13 Enterprise & Team Use OPENCLAW WINS

Feature Hermes🦞 OpenClaw
Multi-agentSubagents for parallel workMulti-agent routing via Gateway (per-workspace isolation)
Team chatShared Discord/Slack channelsGateway routes to isolated agents per channel/account
Data isolationPer-profile isolationPer-assistant isolated storage (built-in)
NVIDIA supportNoNemoClaw partnership
Managed hostingCommunity providersDigitalOcean, OpenClaw VPS, multiple managed providers
Companion appsNonemacOS, iOS, Android companion apps
Visual workspaceNoLive Canvas (agent-driven visual workspace)

14 Research Capabilities HERMES WINS

Capability Hermes🦞 OpenClaw
Batch trajectoriesBuilt-in parallel trajectory generationNo equivalent
RL trainingAtropos RL environmentsNo RL infrastructure
Trajectory exportCompression + export for fine-tuningNo training data export
Configurable workersBatch sizes, toolset distributionsNo batch processing
Built byNous Research (model training lab)Community (founder joined OpenAI)

Hermes was built by a model training lab. It's designed from the ground up as research infrastructure — not just a user-facing product. OpenClaw has no equivalent research capabilities.

15 Migration Path

OpenClaw → Hermes: Built-in Migration Tool

hermes claw migrate imports your persona (SOUL.md), memory, skills (auto-converted to agentskills.io), messaging configs, API keys, and TTS assets. Interactive preview with dry-run support. Skill conflicts are handled with skip/overwrite/rename options. You can run both agents side by side during transition.

🦞 Hermes → OpenClaw: Manual Process

No built-in migration tool. Requires manual mapping of memory files, skill format conversion, and channel reconfiguration. The self-improving skills system has no OpenClaw equivalent — you lose compounding automation value. Estimated effort: 12–21 hours.

16 Final Verdict

Category Winners SummaryLearning/Self-ImprovementMemory SystemSecurityModel SupportDeployment OptionsCost EfficiencyResearch / RLPlatform CoverageSkills Ecosystem SizeCommunity SizeEnterprise / TeamCompanion AppsDeveloper ExperienceMigration ToolingHERMESHERMESHERMESHERMESHERMESHERMESHERMESOPENCLAWOPENCLAWOPENCLAWOPENCLAWOPENCLAWTIEHERMESHermes: 8 winsOpenClaw: 5 wins
Fig 5: Category-by-category winner summary across all 14 comparison dimensions.

Choose Hermes When You...

  • Want an agent that genuinely improves over time
  • Prioritize security (zero CVEs, Docker hardened defaults)
  • Need deep memory across months of interactions
  • Want serverless deployment with near-zero idle costs
  • Work with Chinese LLM providers (MiniMax, GLM, Kimi)
  • Are a researcher needing RL training / trajectory generation
  • Want to migrate from OpenClaw (built-in migration tool)

🦞 Choose OpenClaw When You...

  • Need the broadest possible platform coverage (25+)
  • Want a massive skills ecosystem (13,700+ on ClawHub)
  • Need enterprise support (NVIDIA NemoClaw)
  • Require native Windows support
  • Want companion apps (macOS, iOS, Android)
  • Need multi-agent routing with per-assistant isolation
  • Already have an established OpenClaw deployment
🤝 Or Run Both

Since Hermes uses Python and OpenClaw uses Node.js, they don't conflict at runtime. You can route different channels to different agents, use OpenClaw for broad messaging coverage while Hermes handles deep-work tasks that benefit from its learning loop. The hermes claw migrate tool supports gradual migration when you're ready.

Deep Dive into Redis VI: Redis vs ZooKeeper vs etcd

· 4 min read

A Deep, Conceptual Comparison for Distributed Systems​


1. High-Level Positioning​

SystemCore IdentityPrimary Purpose
RedisIn-memory data platformFast data access, caching, counters, queues
ZooKeeperDistributed coordination serviceStrong consistency, coordination, metadata
etcdDistributed key-value storeCluster configuration & service discovery

One Sentence Summary​

  • Redis: “Give me data now.”
  • ZooKeeper: “Coordinate distributed systems safely.”
  • etcd: “Store and watch cluster state consistently.”

2. Fundamental Design Philosophy​

Redis Philosophy​

Speed > Everything
Memory-first
Eventual consistency (cluster)
Application-driven logic

Redis optimizes for latency and throughput.


ZooKeeper Philosophy​

Correctness > Performance
Strong consistency
Coordination first
Centralized control plane

ZooKeeper optimizes for correct distributed behavior.


etcd Philosophy​

Simplicity + Consistency
Raft-based consensus
Cloud-native control plane
Observability-friendly

etcd optimizes for operational clarity and reliability.


3. Core Architecture Comparison​

Redis Architecture (Cluster Mode)​

Client
|
┌───────────┴────────┐
│ Redis Cluster │
└───────────┬────────┘
┌───────────┴───────────┐
┌────┴────┐ ┌────┴────┐ ┌────┴────┐
│ Master1 │ │ Master2 │ │ Master3 │
└────┬────┘ └────┬────┘ └────┬────┘
│ │ │
Replicas Replicas Replicas

Key points:

  • Hash slots (16,384)
  • Sharding for scale
  • Replication for availability
  • Not consensus-based

ZooKeeper Architecture​

Client
|
┌────────┴──────────┐
│ ZooKeeper Quorum │
└────────┬──────────┘
┌──────────┼──────────┐
┌────┴────┐┌────┴────┐┌────┴────┐
│ Leader ││Follower ││Follower │
└─────────┘└─────────┘└─────────┘

Key points:

  • Single leader
  • Zab protocol
  • Linearizable writes
  • Hierarchical namespace

etcd Architecture​

Client
|
┌────────┴────────┐
│ etcd Cluster │
└────────┬────────┘
┌──────────┼──────────┐
┌────┴────┐┌────┴────┐┌────┴────┐
│ Leader ││ Follower││ Follower│
└─────────┘└─────────┘└─────────┘

Key points:

  • Raft consensus
  • Strong consistency
  • Flat key-value space
  • Watch-based design

4. Consistency Model (Critical Difference)​

FeatureRedisZooKeeperetcd
ConsistencyEventualStrongStrong
Consensus❌ZabRaft
Split-brainPossiblePreventedPrevented
Linearizable writes❌✅✅

Visual Consistency Comparison​

Redis:
Write → Master → Async replicas
(possible stale reads)

ZooKeeper / etcd:
Write → Leader → Majority ACK → Commit
(always consistent)

5. Data Model Comparison​

Redis Data Model​

Key → Value
├── String
├── Hash
├── List
├── Set
├── ZSet
└── Stream

Optimized for application data structures.


ZooKeeper Data Model​

/
├── app
│ ├── leader
│ ├── config
│ └── locks
  • Tree-like (filesystem style)
  • Znodes (small metadata)
  • Ephemeral nodes

etcd Data Model​

/services/app1/instances/1
/services/app1/instances/2
/config/db/timeout
  • Flat KV
  • Prefix-based grouping
  • First-class WATCH support

6. Failure Handling​

Redis Failure Model​

Master crashes
↓
Replica promoted
↓
Possible data loss window
  • Fast
  • Not transactional
  • Best-effort durability

ZooKeeper Failure Model​

Leader crashes
↓
Election
↓
New leader with consistent state
  • No data loss (committed)
  • Slower failover
  • Strong correctness

etcd Failure Model​

Leader crashes
↓
Raft election
↓
New leader
  • Guaranteed state safety
  • Designed for automation

7. Persistence & Storage​

AspectRedisZooKeeperetcd
Primary storageRAMDiskDisk
PersistenceRDB / AOFWAL + snapshotWAL + snapshot
Data volumeLargeSmallMedium
Restart recoveryFastModerateModerate

Redis Persistence​

RAM
│
├─ RDB (snapshot)
└─ AOF (append)

ZooKeeper / etcd Persistence​

Client writes
↓
WAL (log)
↓
Snapshot

8. Typical Use Cases (MOST IMPORTANT)​


Redis – When You SHOULD Use It​

✔ Caching
✔ Rate limiting
✔ Distributed counters
✔ Session storage
✔ Queues / Streams
✔ Real-time analytics
✔ Leaderboards

Diagram:

User → App → Redis → Response (sub-ms)

ZooKeeper – When You SHOULD Use It​

✔ Leader election
✔ Distributed locks
✔ Metadata management
✔ Kafka / HBase coordination
✔ Strong ordering guarantees

Diagram:

Services → ZooKeeper → Coordination

etcd – When You SHOULD Use It​

✔ Kubernetes state
✔ Service discovery
✔ Feature flags
✔ Distributed config
✔ Cloud-native control planes

Diagram:

Nodes → etcd → Watch → React

9. Watch / Notification Capability​

FeatureRedisZooKeeperetcd
WatchPub/SubWatcherNative Watch
ReliabilityBest-effortStrongStrong
Replay❌❌✅

etcd Watch Model​

Client sets watch
↓
Key changes
↓
Guaranteed event delivery

10. Scalability Characteristics​

AspectRedisZooKeeperetcd
Horizontal scaleExcellentPoorLimited
Data sizeVery largeVery smallSmall-medium
ClientsMillionsThousandsThousands

Why ZooKeeper Does NOT Scale Like Redis​

All writes → Leader
All coordination → Leader

This is intentional.


11. Operational Complexity​

AspectRedisZooKeeperetcd
SetupEasyHardMedium
DebuggingMediumHardMedium
TuningMany knobsMany knobsFew knobs
Cloud-nativePartialNoYes

12. Security & Access Control​

FeatureRedisZooKeeperetcd
AuthACL / AUTHACLTLS + RBAC
EncryptionTLSTLSTLS
Multi-tenancyLimitedLimitedStrong

13. What NOT to Do (Very Important)​

❌ Do NOT use Redis for:​

  • Distributed locks requiring strict correctness
  • Leader election with safety guarantees
  • Source of truth metadata

❌ Do NOT use ZooKeeper for:​

  • Large datasets
  • High-QPS application reads
  • Caching

❌ Do NOT use etcd for:​

  • Hot data paths
  • Large object storage
  • High-frequency counters

14. Decision Matrix (Quick Guide)​

Need speed? ───────────▶ Redis
Need correctness? ─────▶ ZooKeeper
Need cloud control? ───▶ etcd

15. Mental Model Summary​

Redis = Data Plane
ZooKeeper = Coordination Plane
etcd = Control Plane

They complement, not replace, each other.


16. Final Takeaway​

  • Redis is fast but not authoritative
  • ZooKeeper is authoritative but heavy
  • etcd is authoritative and cloud-native

The best systems often use all three, each for what it does best.


Deep Dive into Redis VII: Commands

· 11 min read

📊 Document Overview​

Organized by usage frequency and modern patterns

🏆 Top 10 Most Used Redis Commands​

1. SET - Store Key-Value Pair​

Purpose: Store string value with optional expiration

Syntax:

SET key value [EX seconds|PX milliseconds] [NX|XX]

Real-life Scenarios:

  • User session storage
  • Cache with TTL (Time-To-Live)
  • Feature flag storage
  • Temporary data storage

Best Practices:

# Cache with 60-second expiration
SET user:123:profile "{name: 'John', email: 'john@email.com'}" EX 60

# Set only if key doesn't exist (for locks)
SET lock:order:456 "processing" EX 30 NX

# Set only if key exists (for updates)
SET user:123:status "active" XX

Diagram:

┌─────────────────────────────────────────────┐
│ SET Command Flow │
├─────────────────────────────────────────────┤
│ Client Redis Server │
│ │ │ │
│ │ SET key value EX 30 │ │
│ │──────────────────────────>│ │
│ │ │ │
│ │ "OK" │ │
│ │<──────────────────────────│ │
│ │ │ │
│ │ (30 seconds later) │ │
│ │ GET key │ │
│ │──────────────────────────>│ │
│ │ (nil) │ │
│ │<──────────────────────────│ │
└─────────────────────────────────────────────┘

2. GET - Retrieve Key Value​

Purpose: Get value of a key

Syntax: GET key

Real-life Scenarios:

  • Retrieve cached data
  • Read configuration
  • Check feature flags

Examples:

# Basic get
GET user:456:profile

# Use with EXISTS check first
EXISTS user:456:profile
GET user:456:profile

# Pattern for cache miss handling
GET product:789
# If nil, fetch from database and SET with EX

3. DEL - Delete Keys​

Purpose: Remove one or more keys

Syntax: DEL key [key ...]

Best Practices:

# Delete single key
DEL user:123:session

# Delete multiple keys
DEL user:123:profile user:123:preferences user:123:cart

# Safer deletion with EXISTS check
EXISTS important:config
DEL important:config

# Use UNLINK for large keys in production (non-blocking)
UNLINK large:dataset:456

4. INCR/DECR - Atomic Counters​

Purpose: Increment/decrement integer values atomically

Real-life Scenarios:

  • Page view counters
  • Rate limiting
  • Inventory management
  • Unique ID generation

Examples:

# Basic counter
SET page:views 0
INCR page:views # Returns 1
INCR page:views # Returns 2

# Rate limiting pattern
INCR rate:limit:192.168.1.1
EXPIRE rate:limit:192.168.1.1 60

# Decrement inventory
SET product:789:stock 100
DECR product:789:stock # Returns 99

# Increment by specific amount
INCRBY user:123:score 50
DECRBY user:123:score 20

Rate Limiting Diagram:

┌─────────────────────────────────────────────────┐
│ Rate Limiting Pattern │
├─────────────────────────────────────────────────┤
│ Minute 1 │ Minute 2 │ Minute 3 │
│ ┌─────────┐ │ ┌─────────┐ │ ┌─────────┐ │
│ │Request 1│ │ │Request 1│ │ │Request 1│ │
│ │INCR key │ │ │INCR key │ │ │INCR key │ │
│ │EXPIRE │ │ │EXPIRE │ │ │EXPIRE │ │
│ └─────────┘ │ └─────────┘ │ └─────────┘ │
│ │Request 2│ │ │Request 2│ │ │
│ │INCR→2 │ │ │INCR→2 │ │ │
│ └─────────┘ │ └─────────┘ │ │
│ │Request 3│ │ │Request 3│ │ │
│ │INCR→3 │ │ │INCR→3 │ │ │
│ └─────────┘ │ └─────────┘ │ │
│ │Request 4│ │ │Request 4│ │ │
│ │INCR→4 │ │ │GET→4 │ │ │
│ │IF >3 │ │ │IF >3 │ │ │
│ │Reject │ │ │Reject │ │ │
│ └─────────┘ │ └─────────┘ │ │
└─────────────────────────────────────────────────┘

5. EXPIRE - Set Key Expiration​

Purpose: Set timeout on key

Syntax: EXPIRE key seconds

Modern Alternatives:

# Set with expiration in one command (preferred)
SET session:abc123 "user_data" EX 3600

# Update existing key's TTL
EXPIRE cache:key 300
EXPIREAT cache:key 1672531200 # Unix timestamp

# Check remaining TTL
TTL session:abc123
# Returns: -2 (key doesn't exist)
# Returns: -1 (key exists, no expire)
# Returns: >0 (seconds remaining)

# Remove expiration
PERSIST important:config

6. HSET/HGET - Hash Operations​

Purpose: Store field-value pairs within a key

Real-life Scenarios:

  • User profiles
  • Product information
  • Object storage
  • Partial updates

Examples:

# Store user profile
HSET user:123 name "John Doe" email "john@email.com" age 30

# Get specific fields
HGET user:123 name # "John Doe"
HGET user:123 age # "30"

# Get all fields
HGETALL user:123

# Update single field
HSET user:123 age 31

# Check if field exists
HEXISTS user:123 email

# Increment numeric field
HINCRBY user:123 visits 1

# Get multiple fields
HMGET user:123 name email phone

Hash vs String Storage:

┌─────────────────┐ ┌─────────────────┐
│ String Storage │ │ Hash Storage │
├─────────────────┤ ├─────────────────┤
│ │ │ │
│ user:123 │ │ user:123 │
│ ──────────── │ │ ──────────── │
│ { │ │ name: "John" │
│ "name": │ │ email: "john@" │
│ "John", │ │ age: "30" │
│ "email": │ │ │
│ "john@...", │ │ │
│ "age": 30 │ │ │
│ } │ │ │
│ │ │ │
│ Memory: 100B │ │ Memory: 80B │
│ │ │ (more efficient│
│ │ │ for many flds)│
└─────────────────┘ └─────────────────┘

7. LPUSH/RPUSH - List Operations​

Purpose: Add elements to lists

Real-life Scenarios:

  • Message queues
  • Activity feeds
  • Recent items
  • Job queues

Examples:

# Message queue pattern
LPUSH messages:queue "{job: 'process', data: {...}}"
LPUSH messages:queue "{job: 'email', data: {...}}"

# Worker processing
RPOP messages:queue # Gets oldest item

# Recent activities
LPUSH user:123:activity "Logged in"
LPUSH user:123:activity "Viewed product:789"
LPUSH user:123:activity "Purchased item"

# Get range (last 10 activities)
LRANGE user:123:activity 0 9

# Fixed-length list (news feed)
LPUSH feed:global "News item 1"
LTRIM feed:global 0 99 # Keep only 100 items

Message Queue Pattern:

┌─────────────┐ LPUSH ┌─────────────┐ RPOP/BLPOP ┌─────────────┐
│ Producer │───────────>│ Redis │<───────────────│ Consumer │
│ │ │ List │ │ │
│ │ │ ┌───────┐ │ │ │
│ │ │ │ Msg 3 │ │ │ │
│ │ │ │ Msg 2 │ │ │ │
│ │ │ │ Msg 1 │ │ │ │
│ │ │ └───────┘ │ │ │
└─────────────┘ └─────────────┘ └─────────────┘
│ │ │
│ │ │
│ (FIFO Queue) │
└──────────────────────────────────────────────────────────┘

8. SADD/SMEMBERS - Set Operations​

Purpose: Store unique unordered elements

Real-life Scenarios:

  • Tags/categories
  • Unique visitors
  • Friends/followers
  • Vote tracking

Examples:

# Store tags
SADD article:456:tags "redis" "database" "nosql" "cache"

# Check membership
SISMEMBER article:456:tags "redis" # Returns 1 (true)

# Get all members
SMEMBERS article:456:tags

# Set operations (powerful!)
SADD users:online "user1" "user2" "user3"
SADD users:active "user2" "user3" "user4"

# Intersection (users both online AND active)
SINTER users:online users:active # ["user2", "user3"]

# Union (all unique users)
SUNION users:online users:active # ["user1","user2","user3","user4"]

# Difference (online but not active)
SDIFF users:online users:active # ["user1"]

# Random element (lottery, sampling)
SRANDMEMBER users:online
SPOP users:online # Removes and returns random

9. ZADD/ZRANGE - Sorted Set Operations​

Purpose: Store sorted unique elements with scores

Real-life Scenarios:

  • Leaderboards
  • Time-series data
  • Priority queues
  • Range queries

Examples:

# Leaderboard
ZADD leaderboard 1000 "player1"
ZADD leaderboard 850 "player2"
ZADD leaderboard 920 "player3"

# Get top 3 players
ZREVRANGE leaderboard 0 2 WITHSCORES

# Get player rank (0-indexed from top)
ZREVRANK leaderboard "player2"

# Increment score
ZINCRBY leaderboard 50 "player1"

# Range queries (scores 800-900)
ZRANGEBYSCORE leaderboard 800 900

# Time-series pattern
ZADD temperature:nyc 1672444800 "38.5" # timestamp, value
ZADD temperature:nyc 1672448400 "39.2"
ZRANGEBYSCORE temperature:nyc 1672444800 1672448400

Leaderboard Architecture:

┌─────────────────────────────────────────────────┐
│ Redis Sorted Set Leaderboard │
├─────────────────────────────────────────────────┤
│ Score │ Member │ Operations │
├──────────┼─────────────────┼───────────────────┤
│ 1050 │ player1 │ ZADD score │
│ 920 │ player3 │ ZINCRBY +50 │
│ 850 │ player2 │ ZREVRANGE 0 2 │
│ 720 │ player4 │ ZREVRANK player2 │
│ 650 │ player5 │ ZRANGEBYSCORE │
│ │ │ 800 1000 │
└──────────┴─────────────────┴───────────────────┘
▲
│
└─── Scores automatically sorted,
O(log N) operations

10. SCAN - Safe Key Iteration​

Purpose: Incrementally iterate keys without blocking

Why not KEYS?

  • KEYS * blocks Redis (use in production with extreme caution)
  • SCAN is non-blocking, production-safe

Examples:

# Iterate through all keys matching pattern
SCAN 0 MATCH user:* COUNT 100

# Iterate specific type
SCAN 0 MATCH product:* COUNT 50 TYPE string

# Hash scan
HSCAN user:123 0 MATCH *name* COUNT 10

# Set scan
SSCAN tags:article:456 0 COUNT 20

# Sorted set scan
ZSCAN leaderboard 0 COUNT 15

SCAN Iteration Pattern:

┌─────────────────────────────────────────────┐
│ SCAN Iteration Process │
├─────────────────────────────────────────────┤
│ Initial: SCAN 0 MATCH user:* COUNT 5 │
│ Returns: │
│ 1) "5" (next cursor) │
│ 2) ["user:1", "user:2", "user:3"] │
│ │
│ Next: SCAN 5 MATCH user:* COUNT 5 │
│ Returns: │
│ 1) "12" (next cursor) │
│ 2) ["user:4", "user:5"] │
│ │
│ Next: SCAN 12 MATCH user:* COUNT 5 │
│ Returns: │
│ 1) "0" (iteration complete) │
│ 2) ["user:6"] │
│ │
│ Cursor 0 = Done │
└─────────────────────────────────────────────┘

🏗️ Data Structure Selection Guide​

┌─────────────────────────────────────────────────────────────┐
│ Choosing the Right Data Structure │
├──────────────┬────────────────┬─────────────┬───────────────┤
│ Use Case │ Structure │ Best For │ Example │
├──────────────┼────────────────┼─────────────┼───────────────┤
│ Simple Cache │ String │ Single vals │ SET/GET │
│ │ │ With TTL │ user:session │
├──────────────┼────────────────┼─────────────┼───────────────┤
│ Object Store │ Hash │ Multiple │ HSET/HGETALL │
│ │ │ fields │ user:profile │
├──────────────┼────────────────┼─────────────┼───────────────┤
│ Queue │ List │ FIFO/LIFO │ LPUSH/RPOP │
│ │ │ │ jobs:queue │
├──────────────┼────────────────┼─────────────┼───────────────┤
│ Unique Items │ Set │ Membership │ SADD/SISMEMBER│
│ │ │ Operations │ article:tags │
├──────────────┼────────────────┼─────────────┼───────────────┤
│ Rankings │ Sorted Set │ Scores │ ZADD/ZRANGE │
│ │ │ Ranges │ leaderboard │
├──────────────┼────────────────┼─────────────┼───────────────┤
│ Time Series │ Sorted Set │ Timestamps │ ZADD ts:data │
│ │ or Stream │ Sequences │ XADD log:stream│
└──────────────┴────────────────┴─────────────┴───────────────┘

⚡ Advanced & Modern Patterns​

11. Pipeline - Batch Operations​

Purpose: Reduce round-trip time

# Without pipeline (3 round trips)
SET user:1:name "John"
SET user:1:email "john@email.com"
SET user:1:age 30

# With pipeline (1 round trip)
MULTI
SET user:1:name "John"
SET user:1:email "john@email.com"
SET user:1:age 30
EXEC

12. Transactions - Atomic Operations​

MULTI
GET account:123:balance
DECRBY account:123:balance 100
INCRBY account:456:balance 100
EXEC

13. Lua Scripting - Complex Atomic Operations​

EVAL "local current = redis.call('GET', KEYS[1])
if tonumber(current) < tonumber(ARGV[1]) then
return redis.call('INCR', KEYS[1])
else
return nil
end" 1 rate:limit:ip 10

14. Streams - Modern Message Queue​

# Producer
XADD mystream * sensor-id 1234 temperature 19.8

# Consumer group
XGROUP CREATE mystream mygroup 0

# Consumer
XREADGROUP GROUP mygroup consumer1 COUNT 1 BLOCK 5000 STREAMS mystream >

🔐 Security & Production Commands​

15. AUTH - Authentication​

AUTH your_strong_password_here

# Better: Use Redis 6+ ACL
ACL SETUSER alice on >password ~cache:* +get +set

16. CONFIG - Configuration Management​

# Get configuration
CONFIG GET maxmemory
CONFIG GET requirepass

# Set configuration (prefer redis.conf for persistence)
CONFIG SET maxmemory 2gb
CONFIG SET save "900 1 300 10 60 10000"

# Never expose these in production:
CONFIG SET requirepass "" # DON'T!
CONFIG SET protected-mode no # DON'T!

17. INFO - Monitoring​

# Get all info (verbose)
INFO

# Get specific sections
INFO memory
INFO stats
INFO clients
INFO replication

# Critical metrics to monitor:
INFO memory # used_memory, maxmemory, evictions
INFO stats # instantaneous_ops_per_sec, keyspace_hits/misses
INFO persistence # rdb_last_save_time, aof_rewrite_in_progress

🎯 Memory Optimization Commands​

18. MEMORY USAGE​

MEMORY USAGE large:key
MEMORY STATS
MEMORY PURGE # Attempts to defragment (Redis 4+)

19. Efficient Data Encoding​

# Use appropriate data types
# Strings under 44 bytes use EMBSTR encoding
SET small:key "short" # More efficient

# Hashes with <= 512 fields use ziplist
HSET config:app field1 "val1" ... field500 "val500"

# Lists with small elements use ziplist
LPUSH recent:items "item1" "item2" # Check list-max-ziplist* configs

📈 Performance Optimization Patterns​

Connection Pooling Diagram:​

┌─────────────┐ ┌─────────────────┐ ┌─────────────┐
│ App │ │ Connection │ │ Redis │
│ Server │─────▶│ Pool │─────▶│ Server │
│ │ │ │ │ │
│ Request 1 │ │ ┌───────────┐ │ │ │
│ ───────── │ │ │ Connection│──┼─────▶│ Process │
│ GET key │ │ │ 1 │ │ │ Request │
│ │ │ └───────────┘ │ │ │
│ Request 2 │ │ ┌───────────┐ │ │ │
│ ───────── │ │ │ Connection│──┼─────▶│ Process │
│ SET key │───┬─▶│ │ 2 │ │ │ Request │
│ │ │ │ └───────────┘ │ │ │
│ Request 3 │ │ │ ┌───────────┐ │ │ │
│ ───────── │ └─▶│ │ Connection│──┼─────▶│ Process │
│ INCR key │ │ │ 3 │ │ │ Request │
│ │ │ └───────────┘ │ │ │
└─────────────┘ └─────────────────┘ └─────────────┘

20. Monitor Slow Logs​

# Configure slow log
CONFIG SET slowlog-log-slower-than 10000 # 10ms
CONFIG SET slowlog-max-len 128

# View slow logs
SLOWLOG GET 10
SLOWLOG LEN
SLOWLOG RESET

🚫 Anti-Patterns to Avoid​

Bad Patterns:​

# 1. KEYS * in production (use SCAN instead)
KEYS * # BLOCKS SERVER!

# 2. Large values in single key
SET huge:key "[...10MB of data...]" # Blocks during serialization

# 3. Not using TTL for cache
SET cache:key "data" # Might fill memory

# 4. Using Redis as primary database
# Redis is in-memory - data can be lost

# 5. Not monitoring memory usage
# Leads to OOM (Out Of Memory) errors

Good Patterns:​

# 1. Use pipelining for bulk operations
MULTI
SET key1 "val1"
SET key2 "val2"
EXEC

# 2. Use appropriate data structures
# For object with many fields: Hash, not multiple String keys

# 3. Always set TTL for cache
SET session:abc "data" EX 3600

# 4. Use Lua scripts for complex atomic operations

# 5. Monitor with INFO and SLOWLOG

🔄 Replication & High Availability Commands​

21. Replication Management​

# On replica
REPLICAOF master-ip 6379
REPLICAOF NO ONE # Promote to master

# Check replication status
INFO replication

# Monitor replication lag
redis-cli --latency

22. Sentinel & Cluster Commands​

# Sentinel
SENTINEL MASTERS
SENTINEL GET-MASTER-ADDR-BY-NAME mymaster

# Cluster
CLUSTER NODES
CLUSTER INFO
CLUSTER SLOTS

🧹 Maintenance Commands​

23. Backup & Persistence​

# Manual save (blocks)
SAVE # Synchronous, blocks
BGSAVE # Asynchronous, forks

# Check persistence status
INFO persistence
LASTSAVE # Unix timestamp of last save

# AOF operations
BGREWRITEAOF

24. Cleanup & Debugging​

# Flush data (CAUTION!)
FLUSHDB # Current database
FLUSHALL # All databases

# Debug object
DEBUG OBJECT keyname

# Monitor commands in real-time
MONITOR # Use sparingly, verbose

# Client management
CLIENT LIST
CLIENT KILL addr ip:port
CLIENT PAUSE timeout_ms # Pause clients

📊 Monitoring Dashboard Metrics​

┌─────────────────────────────────────────────────────┐
│ Redis Health Dashboard │
├─────────────────┬─────────────────┬─────────────────┤
│ Memory Usage │ Command Stats │ Connections │
├─────────────────┼─────────────────┼─────────────────┤
│ Used: 1.2GB/2GB│ Ops/sec: 5,000 │ Connected: 45 │
│ 60% │ Hits: 98% │ Max: 10,000 │
│ Peak: 1.8GB │ Misses: 2% │ Rejected: 0 │
│ Evictions: 0 │ Cmd/sec: 8,000 │ Timeout: 0 │
├─────────────────┼─────────────────┼─────────────────┤
│ Replication │ Persistence │ Keyspace │
├─────────────────┼─────────────────┼─────────────────┤
│ Role: Master │ RDB: Enabled │ DB0 Keys: 45K │
│ Slaves: 2 │ Last Save: 2m │ Expires: 12K │
│ Lag: 0.5s │ AOF: Enabled │ Avg TTL: 300s │
│ Status: OK │ Size: 500MB │ Hit Rate: 99% │
└─────────────────┴─────────────────┴─────────────────┘

🚀 Quick Reference Cheat Sheet​

Caching Patterns:​

# Cache-aside pattern
1. GET cache:key
2. If nil, fetch from DB
3. SET cache:key value EX 300

# Write-through pattern
1. Update DB
2. SET cache:key new_value EX 300

# Write-behind pattern
1. SET cache:key new_value EX 300
2. Async update to DB

Key Design Best Practices:​

Good: Bad:
user:123:profile user_profile_123
session:abc123 abc123_session
product:789:views product_views_789
geo:nyc:users newyork_users_list

Pattern: object_type:id:field

TTL Strategies:​

# Cache: 5 minutes - 1 hour
SET cache:key value EX 300

# Session: 30 minutes - 24 hours
SET session:key data EX 1800

# Rate limit: 1 second - 1 minute
SET rate:limit:ip 1 EX 60 NX

# Leaderboard: 1 day - 7 days
EXPIRE leaderboard:daily 86400

🔍 Troubleshooting Commands​

25. Diagnose Issues​

# Check if Redis is alive
PING # Should return PONG

# Check latency between commands
redis-cli --latency

# Monitor commands in real-time (debug)
MONITOR

# Check memory fragmentation
INFO memory | grep fragmentation

# List connected clients
CLIENT LIST

# Check slow queries
SLOWLOG GET 5

26. Performance Testing​

# Benchmark tool (outside redis-cli)
redis-benchmark -t set,get -n 100000 -q

# Pipeline benchmark
redis-benchmark -t set,get -n 100000 -q -P 16

🎨 Visualization: Data Structure Memory Layout​

String:
┌────────┬────────┬────────┐
│ Header │ Data │ Null │
│ Type │ "value"│ term │
└────────┴────────┴────────┘

Hash (ziplist encoding):
┌────┬────┬────┬────┬────┬────┐
│ ZL │ F1 │ V1 │ F2 │ V2 │ ZL │
│bytes│"name"│"John"│"age"│"30"│end │
└────┴────┴────┴────┴────┴────┘

List (quicklist):
┌─────────┬─────────┬─────────┐
│ Node │ Node │ Node │
│ [ziplist│ [ziplist│ [ziplist│
│ "item1" │ "item4" │ "item7" │
│ "item2" │ "item5" │ │
│ "item3" │ "item6" │ │
└─────────┴─────────┴─────────┘

📚 Further Learning Path​

Next Steps:​

  1. Redis Streams - Modern message queues
  2. Redis Modules - RediSearch, RedisJSON, RedisGraph
  3. Redis Cluster - Horizontal scaling
  4. Redis Sentinel - High availability
  5. Redis Enterprise - Commercial features

Production Checklist:​

  • Set maxmemory and eviction policy
  • Enable persistence (RDB/AOF)
  • Configure backups
  • Set up monitoring
  • Implement connection pooling
  • Use key expiration
  • Regular maintenance
  • Disaster recovery plan

Remember: Redis is single-threaded for commands. One slow command blocks all others. Always test commands in staging before production, and monitor performance metrics regularly.

Why Kubernetes Chose etcd

· 5 min read

A Deep Dive into Kubernetes’ Control Plane Backbone​


1. One-Sentence Answer (Executive Summary)​

Kubernetes chose etcd because it provides strong consistency, fault tolerance, watchability, and simplicity—exactly what a distributed control plane needs.


2. What Kubernetes Needs at Its Core​

Before understanding why etcd, we must understand what Kubernetes fundamentally is.

Kubernetes Is NOT:​

  • A container runtime
  • A scheduler only
  • A deployment tool

Kubernetes IS:​

A distributed state machine that continuously reconciles desired state with actual state

That sentence alone explains why etcd matters.


3. Kubernetes Mental Model​

User declares desired state
↓
Kubernetes stores desired state
↓
Controllers compare desired vs actual
↓
Actions taken to reconcile
↓
State updated

Key Insight​

👉 Everything depends on a reliable, consistent source of truth


4. The Role of the Kubernetes Data Store​

The data store must hold:

  • Desired state (YAML manifests)
  • Cluster metadata
  • Node status
  • Pod status
  • ConfigMaps
  • Secrets
  • Leases
  • Locks
  • Leader elections

This data store is the single source of truth.


5. Why This Data Store Is So Critical​

If the data store is wrong, Kubernetes is wrong.

Wrong state → Wrong scheduling
Wrong locks → Split brain
Wrong config → Cluster outage

So Kubernetes needs a store that is:

✔ Always correct
✔ Always consistent
✔ Always available (with quorum)
✔ Observable (watchable)
✔ Deterministic

6. Why NOT Redis?​

Let’s eliminate obvious candidates first.

Redis Characteristics​

Fast
In-memory
Eventual consistency
No consensus protocol
Best-effort durability

Why Redis Fails Kubernetes’ Needs​

Redis write → Master
Replica lag possible
Failover may lose writes

Diagram​

APIServer → Redis
↓
Possible stale read

🚫 Unacceptable for Kubernetes Kubernetes requires linearizable writes, not eventual correctness.


7. Why NOT ZooKeeper?​

ZooKeeper was a strong candidate.

ZooKeeper Strengths​

Strong consistency
Leader election
Distributed locks
Proven correctness

Why Kubernetes Did NOT Choose ZooKeeper​

IssueExplanation
Operational complexityHard to operate at scale
Watch semanticsOne-time triggers
API ergonomicsNot cloud-native
Data modelHierarchical, not flexible
ToolingHeavy JVM ecosystem

ZooKeeper was correct but heavy.


8. Enter etcd​

etcd Core Identity​

Distributed
Strongly consistent
Key-value store
Raft consensus
Watch-first design

9. etcd at a Glance​

┌───────────────┐
│ Kubernetes │
│ Control Plane │
└───────┬───────┘
│
┌───────▼───────┐
│ etcd │
│ (Source of │
│ Truth) │
└───────────────┘

10. Strong Consistency: The #1 Reason​

What etcd Guarantees​

Write acknowledged
↓
Majority of nodes committed
↓
All future reads see it

Diagram (Raft)​

Client
↓
Leader
↓
Follower A
↓
Follower B
(majority ACK)

✔ No stale reads ✔ No lost writes ✔ No split brain


11. Kubernetes Control Plane Is a Distributed State Machine​

State S₀
↓
Event E₁
↓
State S₁
↓
Event E₂
↓
State S₂

etcd guarantees:

  • Ordered events
  • Durable transitions
  • Deterministic replay

12. Watch Is a First-Class Primitive in etcd​

This is hugely important.

etcd Watch Model​

Client sets watch
↓
Key changes
↓
Guaranteed event stream

Kubernetes Depends on This​

etcd change
↓
APIServer
↓
Controller
↓
Action

Controllers are event-driven, not polling-based.


13. Compare Watch Semantics​

SystemWatch Reliability
Redis Pub/SubBest effort
ZooKeeperOne-shot
etcdContinuous, ordered, resumable

Only etcd matches Kubernetes needs.


14. Simple Data Model = Huge Advantage​

etcd Uses Flat Key-Value Space​

/registry/pods/ns1/podA
/registry/services/ns1/svcA
/registry/nodes/node1

Benefits​

✔ Easy versioning
✔ Easy compaction
✔ Easy snapshot
✔ Easy migration

ZooKeeper’s tree model complicates this.


15. Transactions & Compare-And-Swap​

etcd supports:

IF version == X
THEN update
ELSE fail

Kubernetes uses this for:

  • Leader election
  • Resource locking
  • Optimistic concurrency control

16. Leader Election (Kubernetes Everywhere)​

Controller A
Controller B
Controller C

Only ONE should be active.

etcd Makes This Safe​

Lease → TTL → automatic expiry

Diagram:

Controller A holds lease
↓
Crash
↓
Lease expires
↓
Controller B elected

17. etcd Is Disk-Backed (Not Memory-Only)​

Why This Matters​

Crash
↓
Restart
↓
State recovered

etcd durability is mandatory for cluster recovery.


18. Snapshot + WAL Model​

Writes → WAL
↓
Periodic snapshot

Benefits​

  • Fast recovery
  • Small snapshots
  • Replayable history

Perfect for Kubernetes.


19. Kubernetes Can Rebuild the Cluster from etcd Alone​

This is HUGE.

Nodes die
Pods die
APIServer restarts

As long as etcd survives, Kubernetes reconstructs everything.


20. etcd Is Cloud-Native Friendly​

FeatureWhy It Matters
gRPC APIPerformance & tooling
TLS everywhereSecurity
Small binaryEasy deployment
RaftSimple mental model

21. Scaling Expectations Match Perfectly​

Kubernetes Data Profile​

Low write volume
Small objects
High read consistency

etcd Designed Exactly for This​

etcd is not designed for:

  • High QPS counters
  • Large blobs
  • Hot data paths

Which aligns perfectly.


22. Operational Model Alignment​

3–5 nodes
Quorum based
Simple backups
Predictable behavior

Kubernetes admins can reason about failures.


23. What etcd Is NOT Used For in Kubernetes​

❌ Pod logs
❌ Metrics
❌ Application data
❌ High-frequency events

etcd is control plane only.


24. Why etcd Won Over ZooKeeper (Final Comparison)​

CriteriaZooKeeperetcd
ConsistencyStrongStrong
Watch modelWeakExcellent
APIHeavyClean
Cloud-nativeNoYes
Operational burdenHighMedium
Fit for Kubernetes❌✅

25. Final Mental Model​

etcd = Brain
Kubernetes = Nervous System
Nodes = Muscles

If the brain is wrong, the body fails.


26. Final Takeaway​

Kubernetes chose etcd because:

✔ Strong consistency
✔ Event-driven watches
✔ Simple, correct semantics
✔ Cloud-native design
✔ Perfect data profile match

etcd is not fast—but Kubernetes does not need fast. Kubernetes needs correct.


ZooKeeper vs etcd

· 4 min read

Short answer up front:

ZooKeeper is chosen over etcd when absolute correctness, strict ordering, and coordination semantics matter more than cloud-native ergonomics and operational simplicity.

This goes straight to the philosophy and historical trade-offs of distributed systems.


1. “Correctness” Means Different Things in Different Systems​

When people say “ZooKeeper is more correct”, they usually mean coordination correctness, not storage correctness.

Two kinds of correctness​

TypeMeaning
Data correctnessReads return the latest committed value
Coordination correctnessOrdering, fencing, session semantics are strictly enforced

Both etcd and ZooKeeper are data-correct. ZooKeeper is exceptionally strong at coordination correctness.


2. ZooKeeper’s Original Purpose (This Matters a LOT)​

ZooKeeper was not designed as a KV store.

It was designed to solve distributed coordination problems:

  • Leader election
  • Distributed locks
  • Membership tracking
  • Configuration management
  • Barrier synchronization

ZooKeeper’s design assumption​

“If coordination is wrong, the entire distributed system collapses.”

This assumption drove extremely conservative design choices.


3. ZooKeeper’s Strongest Weapon: Session Semantics​

ZooKeeper has sessions, etcd does not (in the same way).

ZooKeeper session model​

Client connects
↓
Session established
↓
Ephemeral nodes tied to session
↓
Client dies → session expires → nodes deleted

Why this is powerful​

Lock correctness
Leader correctness
Membership correctness

Diagram​

Client A holds lock
↓
Client A crashes
↓
ZooKeeper deletes ephemeral node
↓
Lock immediately released

This is automatic fencing, not optional logic.


4. ZooKeeper’s Ordering Guarantees Are Extremely Strict​

ZooKeeper guarantees:

  • Total order of writes
  • Monotonic reads per client
  • No reordering under failure
  • Linearizable operations

But more importantly:

ZooKeeper exposes ordering as a first-class primitive


5. Sequential znodes (A Big Deal)​

ZooKeeper can create nodes like:

/lock/node-00000001
/lock/node-00000002
/lock/node-00000003

This enables:

  • Fair locks
  • FIFO queues
  • Barrier algorithms

etcd cannot do this natively.


6. ZooKeeper Watches vs etcd Watches (Subtle Difference)​

You might hear:

“etcd has better watches”

This is true for data propagation, but false for coordination logic.

ZooKeeper watches are intentionally weaker​

  • One-shot
  • Explicit re-registration
  • Tied to session lifecycle

Why?

Because ZooKeeper forces you to reason about race conditions explicitly.

This reduces false assumptions in coordination logic.


7. ZooKeeper Is Extremely Conservative by Design​

ZooKeeper assumes:

Network partitions WILL happen
Clients WILL crash
GC pauses WILL happen

So it prefers:

  • Blocking over guessing
  • Failure over inconsistency
  • Simplicity over convenience

etcd is conservative too — but ZooKeeper is paranoid.


8. Why Large-Scale Distributed Systems Still Choose ZooKeeper​

Typical ZooKeeper users​

  • Apache Kafka
  • Apache HBase
  • Hadoop YARN
  • Apache Storm
  • Apache Solr
  • Large financial systems
  • Telco control planes

These systems care about:

✔ Strict leader fencing
✔ Lock correctness
✔ No split-brain EVER
✔ Predictable failure modes

9. ZooKeeper vs etcd: Philosophy Comparison​

DimensionZooKeeperetcd
Primary roleCoordination serviceConsistent KV store
Session semanticsFirst-classLease-based
Lock safetyExtremely strongStrong but simpler
Ordering primitivesBuilt-inLimited
API philosophyExplicit, low-levelSimple, declarative
Cloud-native❌✅
Operational simplicity❌✅

10. Why Kubernetes Did NOT Choose ZooKeeper​

Now the flip side.

Kubernetes does not need:

  • Sequential znodes
  • Complex lock trees
  • Session-heavy logic

Kubernetes needs:

✔ State storage
✔ Event watching
✔ Simple transactions
✔ Declarative reconciliation

ZooKeeper would have been overkill and harder to operate.


11. Why ZooKeeper Is Still the “Primary Option” in Some Domains​

ZooKeeper wins when:

You need coordination correctness > everything else​

Examples:

  • Financial trading leader election
  • Distributed job schedulers
  • Metadata services with strict fencing
  • Multi-writer consensus with fairness guarantees

In these systems:

A single incorrect lock is worse than total outage

ZooKeeper optimizes for never being wrong, even if that means being unavailable.


12. Failure Mode Philosophy (Very Important)​

ZooKeeper failure philosophy​

If unsure → stop

etcd failure philosophy​

If quorum exists → continue

Both are correct — but ZooKeeper is stricter.


13. Mental Model Summary​

ZooKeeper = Distributed coordination kernel
etcd = Distributed state store

ZooKeeper acts like a kernel enforcing rules. etcd acts like a database holding truth.


14. Final Answer (Concise)​

ZooKeeper is chosen over etcd when:

  • Coordination correctness is paramount
  • Leader fencing must be absolute
  • Fair ordering matters
  • Failures must be explicit, not hidden
  • Operational complexity is acceptable

etcd is chosen when:

  • State correctness matters
  • Watchability matters
  • Cloud-native ops matter
  • Simplicity matters

15. One-Line Rule of Thumb​

If you fear split-brain more than downtime → ZooKeeper If you fear operational complexity more than coordination edge cases → etcd

Deep Dive into Redis I

· 37 min read

Table of Contents​

  1. Redis Architecture: The Complete Picture
  2. Fundamentals
  3. Data Structures and Operations
  4. Persistence Mechanisms
  5. RDB and AOF File Formats Deep Dive
  6. Replication and High Availability
  7. Redis Cluster and Sharding
  8. Hash Slots Deep Dive
  9. Operational Considerations
  10. Mental Model Summary
  11. Quick Reference Guide

Redis Architecture: The Complete Picture​

Before diving into details, here's the complete Redis ecosystem at a glance:

graph TB
subgraph "Application Layer"
App[Application]
end

subgraph "Redis Layer"
subgraph "Single Instance"
Master1[Master]
Replica1[Replica]
Master1 --> Replica1
end

subgraph "Redis Cluster (Sharded)"
M1[Master A<br/>Slots 0-5460]
M2[Master B<br/>Slots 5461-10922]
M3[Master C<br/>Slots 10923-16383]
R1[Replica A]
R2[Replica B]
R3[Replica C]
M1 --> R1
M2 --> R2
M3 --> R3
end

subgraph "High Availability"
S1[Sentinel 1]
S2[Sentinel 2]
S3[Sentinel 3]
end
end

subgraph "Persistence Layer"
RDB[(RDB Snapshot)]
AOF[(AOF Log)]
end

App --> Master1
App --> M1
App --> M2
App --> M3

S1 -.Monitor.-> Master1
S2 -.Monitor.-> Master1
S3 -.Monitor.-> Master1

Master1 --> RDB
Master1 --> AOF
M1 --> RDB
M2 --> RDB
M3 --> RDB

Key Components:

ComponentPurposeWhen to Use
Single MasterBasic Redis instanceSmall datasets, simple caching
Master + ReplicaRead scaling + failoverRead-heavy workloads
SentinelAutomatic failoverHigh availability without sharding
Redis ClusterHorizontal scaling (sharding)Large datasets exceeding single node
RDBPoint-in-time snapshotsFast restarts, backups
AOFWrite operation logMaximum data durability

Decision Tree:

flowchart TD
Start{Data fits in<br/>single instance?}
Start -->|Yes| HA{Need high<br/>availability?}
Start -->|No| UseCluster[Use Redis Cluster]

HA -->|No| SingleMaster[Single Master<br/>+ optional replicas]
HA -->|Yes| UseSentinel[Master + Replicas<br/>+ Sentinel]

UseCluster --> Persist{Need persistence?}
UseSentinel --> Persist
SingleMaster --> Persist

Persist -->|Fast restart| UseRDB[Enable RDB]
Persist -->|Max durability| UseAOF[Enable AOF]
Persist -->|Best of both| UseHybrid[Enable Both<br/>Recommended]

Fundamentals​

What Is Redis?​

Redis stands for REmote DIctionary Server.

At its heart, Redis is:

  • An in-memory data store
  • Extremely fast (microsecond latency)
  • Used as:
    • Cache
    • Database
    • Message broker
    • Counter system
    • Coordination system

Redis in One Sentence​

Redis is a single-threaded, in-memory data structure server optimized for speed, simplicity, and predictable behavior.

Where Redis Fits in a System​

flowchart LR
Client --> Redis
Client --> DB[(Database)]
Redis --> DB

Redis often sits between your application and your database to:

  • Reduce load
  • Improve latency
  • Handle real-time data

Core Redis Philosophy​

Redis makes several strong design choices:

PrincipleMeaning
In-memoryData lives in RAM for speed
Single-threadedNo locks, no race conditions
Simple data structuresPredictable performance
Explicit durabilityYou choose persistence level

Why Single-Threaded Is a Feature (Not a Bug)​

sequenceDiagram
participant Client1
participant Client2
participant Redis

Client1->>Redis: INCR counter
Client2->>Redis: INCR counter
Redis-->>Client1: OK
Redis-->>Client2: OK

Because Redis processes one command at a time:

  • No locks
  • No inconsistent states
  • Operations like INCR are naturally atomic

Redis Consistency Model​

Redis favors:

Consistency per key, not global consistency

Key Guarantees:

  • Single command = atomic
  • No partial writes
  • Replica lag possible
  • Cluster operations limited across shards

Redis Performance Model​

Why Redis is fast:

graph TD
RAM --> NoDiskIO
SingleThread --> NoLocks
SimpleCommands --> PredictableLatency

Typical Latency:

OperationLatency
GET~1µs
INCR~1–2µs
Network RTTDominant cost

Redis Is Not a Silver Bullet​

Redis trades features for speed.

What Redis Is Great At:

  • Counters
  • Caching
  • Rate limiting
  • Queues
  • Leaderboards
  • Distributed locks

What Redis Is Not Good At:

  • Complex queries
  • Large cold datasets
  • Long-term storage without planning

Data Structures and Operations​

Redis Data Model (Mental Model)​

Redis is not a table store.

Think of Redis as a huge hash map in memory:

graph TD
Redis --> Key1["user:1"]
Redis --> Key2["order:991"]
Redis --> Key3["counter:login"]

Each key:

  • Is a string
  • Maps to a data structure, not just a value

Redis Data Structures (Conceptual View)​

Redis supports rich native data types:

TypeThink of it as
StringBasic value / counter
HashObject
ListQueue / Stack
SetUnique collection
Sorted SetRanking system
StreamAppend-only log

Example Mental Mapping:

graph LR
AppObject --> RedisHash["user:42 (hash)"]
Queue --> RedisList["jobs:list"]
Ranking --> ZSet["leaderboard"]

Redis as a Counter System (INCR)​

Counters are one of Redis's killer features.

Why INCR Is Special:

  • Atomic
  • Lock-free
  • Extremely fast
  • Works under massive concurrency

How INCR Works Internally:

sequenceDiagram
participant App1
participant App2
participant Redis

App1->>Redis: INCR page_views
App2->>Redis: INCR page_views
Redis->>Redis: page_views = page_views + 1
Redis-->>App1: 101
Redis-->>App2: 102

Because Redis is single-threaded:

  • No race conditions
  • No lost updates

How Redis Stores Data​

Important Clarification:

Redis does NOT understand { "user": "whereq" } as a data structure by default. Redis only sees bytes.

The application must decide how to store data:

Option 1: Simple key-value

SET user whereq

Stored as:

"user" -> "whereq"

Option 2: Namespaced key

SET user:whereq ""

Stored as:

"user:whereq" -> ""

Option 3: JSON serialized

SET user:whereq '{"user":"whereq"}'

Stored as:

"user:whereq" -> '{"user":"whereq"}'

Option 4: Redis Hash (logical structure)

HSET user:whereq user whereq

Stored internally as:

"user:whereq" -> hash(user=whereq)

Persistence Mechanisms​

Overview​

Redis is fundamentally an in-memory database, but it provides optional durability through two complementary persistence mechanisms:

graph LR
Memory["In-Memory Data<br/>(Source of Truth)"] --> RDB["RDB<br/>(Point-in-Time Snapshot)"]
Memory --> AOF["AOF<br/>(Write Command Log)"]

Key Principle:

Memory is always the source of truth. RDB and AOF are derived artifacts for recovery.

AspectRDBAOF
What it storesBinary snapshot of all dataSequential log of write commands
When it writesPeriodic (configurable intervals)After every write (configurable)
Recovery speedFast (binary load)Slower (replay commands)
Data loss riskHigher (between snapshots)Lower (per-write durability)
File sizeCompactLarger (needs rewriting)
Best forBackups, replica syncMaximum durability

Data Flow Through Persistence:

sequenceDiagram
participant Client
participant Redis Memory
participant AOF
participant RDB

Client->>Redis Memory: SET key value
Redis Memory->>Client: OK
Redis Memory->>AOF: Append command (immediate)

Note over Redis Memory,RDB: Later (based on save intervals)
Redis Memory->>RDB: Fork & snapshot all data

Note over Redis Memory: On restart:
alt AOF exists
AOF->>Redis Memory: Replay commands
else RDB only
RDB->>Redis Memory: Load snapshot
end

Relationship Between Components:

┌───────────────────────────────────────────────┐
│ │
│ WRITES FLOW (Write Command) │
│ │
│ Client → Memory → AOF (append) │
│ ↓ │
│ (periodic) │
│ ↓ │
│ RDB (snapshot) │
│ │
└───────────────────────────────────────────────┘

┌───────────────────────────────────────────────┐
│ │
│ RECOVERY FLOW (Redis Restart) │
│ │
│ 1. Check if AOF exists? │
│ ├─ Yes → Load AOF (replay all commands) │
│ └─ No → Load RDB (restore snapshot) │
│ │
│ 2. Memory rebuilt │
│ 3. Ready to serve │
│ │
└───────────────────────────────────────────────┘

RDB (Snapshotting)​

What RDB Really Is:

RDB = Redis DataBase file

It is:

  • A binary file on disk
  • Representing a point-in-time snapshot of Redis memory
  • Created by Redis itself, not by the OS

Think of RDB as:

📸 "A photograph of all Redis data at a specific moment in time"

What is inside an RDB file?

  • Every key
  • Every value
  • The data type of each value
  • Expiration metadata (TTL)
graph TD
RAM["Redis In-Memory Data"] --> Snapshot["RDB File on Disk"]

Is RDB a runtime state?

No. RDB exists outside Redis memory.

Redis runtime state is:

  • Live data in RAM
  • Active connections
  • Client buffers

RDB is only used for:

  • Restart recovery
  • Backup
  • Replication bootstrap

How RDB is created (conceptually):

Redis:

  1. Forks a child process
  2. Child process reads memory
  3. Writes a compact binary file
  4. Parent keeps serving requests
sequenceDiagram
Redis->>OS: fork()
Child->>Disk: write snapshot
Parent->>Clients: continue serving

✅ No blocking of the main Redis thread ❗ Extra memory during fork (copy-on-write)

When Redis uses RDB:

SituationUse RDB?
Redis restart✅
Full data reload✅
Replica sync (initial)✅
Incremental updates❌

Characteristics:

  • Periodic full snapshot
  • Fast restart
  • Risk of data loss between snapshots
timeline
t1 : Snapshot
t2 : Snapshot
t3 : Crash (data lost between t2 and t3)

AOF (Append Only File)​

What AOF Is:

  • Logs every write
  • More durable
  • Slightly slower
flowchart LR
Write --> AppendLog --> Disk

How Redis Manages Both RDB and AOF Together​

Key clarification:

There is NO extra daemon, NO sync process, NO background service.

Everything is handled by the Redis server process itself.

RDB vs AOF – different purposes:

RDBAOF
SnapshotCommand log
PeriodicContinuous
Fast restoreDurable
CompactLarger

They do not sync with each other.

How Redis actually works with both enabled:

graph TD
Write --> Memory
Write --> AOF
Timer --> RDB

Important insight:

  • Memory is the source of truth
  • RDB and AOF are derived artifacts

Restart logic (very important):

When Redis restarts:

  1. If AOF exists → AOF is used
  2. Else → RDB is used
flowchart TD
Start --> AOFExists{AOF exists?}
AOFExists -->|Yes| LoadAOF
AOFExists -->|No| LoadRDB

Why no syncing is needed:

Because:

  • Redis never writes from RDB to AOF
  • Redis never writes from AOF to RDB
  • Both are written from memory

Memory → Persistence Not persistence → memory → persistence

Production Reality:

Most systems use:

graph TD
Redis --> RDB
Redis --> AOF

Snapshots for speed + AOF for safety.

Persistence and Migration​

When slot migration occurs:

  • Migration moves keys in RAM
  • Does NOT force persistence
  • Persistence happens later

If a master crashes after migration but before snapshot:

Redis handles this safely:

  1. Master restarts and loads entire RDB into RAM
  2. Cluster metadata (slot ownership) overrides RDB contents
  3. Keys that no longer belong to this master are marked invalid
  4. They will NOT be served and will eventually be deleted

Key Principle:

Cluster metadata (slot ownership) overrides RDB contents

Will snapshot happen automatically after migration?

No. Slot migration does NOT automatically trigger an RDB snapshot.

Redis deliberately decouples:

  • data movement
  • persistence timing

Otherwise, resharding would cause massive disk IO spikes and latency stalls.

Important:

  • RDB = local historical photo
  • RAM = source of truth
  • Slots = routing metadata
  • Migration = RAM → RAM transfer
  • Persistence = future snapshots reflect reality

RDB and AOF File Formats Deep Dive​

This section explores the internal structure of Redis persistence files, how they are organized on disk, and the engineering trade-offs made in their design.

RDB File Format and Structure​

What's Inside an RDB File?

An RDB file is a binary serialization of Redis's entire in-memory dataset at a specific point in time.

graph TD
RDB[RDB File] --> Header[Header: Magic + Version]
RDB --> DB0[Database 0]
RDB --> DB1[Database 1]
RDB --> DBN[Database N]
RDB --> EOF[EOF Marker + Checksum]

DB0 --> Meta0[Metadata: DB selector]
DB0 --> Keys0[Key-Value Pairs]
DB0 --> Expire0[Expiry Information]

RDB File Structure (Sequential Layout):

┌─────────────────────────────────────────────────────────┐
│ MAGIC NUMBER: "REDIS" (5 bytes) │
├─────────────────────────────────────────────────────────┤
│ VERSION: "0009" (4 bytes, current version) │
├─────────────────────────────────────────────────────────┤
│ AUXILIARY FIELDS (optional metadata) │
│ - Redis version │
│ - Creation timestamp │
│ - Used memory │
│ - Replication info │
├─────────────────────────────────────────────────────────┤
│ DATABASE SELECTOR (e.g., DB 0) │
├─────────────────────────────────────────────────────────┤
│ RESIZE DB (hash table size hints) │
├─────────────────────────────────────────────────────────┤
│ KEY-VALUE PAIRS │
│ ┌──────────────────────────────────────┐ │
│ │ Type byte (string/list/hash/etc.) │ │
│ │ Key (encoded string) │ │
│ │ Value (type-specific encoding) │ │
│ │ Expiry (optional, millisecond TTL) │ │
│ └──────────────────────────────────────┘ │
│ ... (repeat for all keys in DB) │
├─────────────────────────────────────────────────────────┤
│ DATABASE SELECTOR (e.g., DB 1) │
│ ... (more key-value pairs) │
├─────────────────────────────────────────────────────────┤
│ EOF MARKER (0xFF) │
├─────────────────────────────────────────────────────────┤
│ CRC64 CHECKSUM (8 bytes) │
└─────────────────────────────────────────────────────────┘

Key-Value Encoding:

Each data type has optimized encoding:

Data TypeEncoding StrategyExample
StringRaw bytes, integer encoding for numbers"123" → 3-byte integer vs 3-byte string
ListZiplist (compact) or LinkedListSmall lists: compressed format
HashZiplist or Hash TableDepends on size thresholds
SetIntSet or Hash TableInteger-only sets: compact array
Sorted SetZiplist or Skip ListHybrid encoding based on size
StreamRadix tree nodesSpecialized compact format

Example: String Encoding

Small integers (0-9999):
Type: 1 byte (REDIS_ENCODING_INT)
Value: variable length integer encoding

Short strings (<= 20 bytes):
Type: 1 byte
Length: 1 byte
Data: N bytes

Long strings:
Type: 1 byte
Length: 4 bytes (encoded)
Data: N bytes

LZF Compressed strings:
Type: 1 byte + compression flag
Compressed length: variable
Uncompressed length: variable
Compressed data: N bytes

Does RDB Have Indexes?

❌ No — RDB has NO internal indexes.

RDB is designed for:

  • Sequential write (fast snapshot creation)
  • Sequential read (fast full restore)
  • Minimal file size (no index overhead)

How RDB Restore Works:

sequenceDiagram
participant Redis
participant RDB File
participant Memory

Redis->>RDB File: Open and validate checksum
RDB File->>Redis: Read header
Redis->>RDB File: Read DB selector
loop For each key-value pair
RDB File->>Redis: Read type + key + value
Redis->>Memory: Insert into hash table
Redis->>Memory: Set expiry if present
end
RDB File->>Redis: EOF marker
Redis->>Redis: Validate CRC64
Redis->>Memory: Rebuild indexes (if needed)

Key Point:

RDB is a dump, not a database. It's optimized for bulk I/O, not random access.

RDB Trade-offs:

Advantage ✅Disadvantage ❌
Compact binary formatNo indexes = can't query RDB directly
Fast to write (sequential I/O)Must load entire file to access any data
Fast to read (sequential load)Memory spike during fork (copy-on-write)
Built-in compression (LZF)Periodic snapshots = potential data loss
CRC64 integrity checkLarge datasets = long snapshot time
Portable across architecturesNot human-readable

RDB Creation Process (Technical Details):

flowchart TD
Start[Write command triggers save] --> Fork[fork() creates child process]
Fork --> COW[Parent uses Copy-on-Write]
Fork --> Child[Child has memory snapshot]

COW --> Parent[Parent continues serving requests]
Child --> Iterate[Iterate all databases]
Iterate --> Encode[Encode each key-value]
Encode --> Write[Sequential write to temp file]
Write --> Rename[Atomic rename to dump.rdb]
Rename --> Exit[Child exits]

Parent --> Continue[Continues normally]
Exit --> Continue

Why RDB Uses fork():

  • Parent process never blocks on disk I/O
  • Child gets consistent point-in-time snapshot
  • Copy-on-write = minimal memory overhead (only modified pages are copied)

Storage Organization:

Redis Data Directory:
/var/lib/redis/
├── dump.rdb ← Current RDB snapshot
├── temp-<pid>.rdb ← Temporary file during save (deleted after rename)
└── appendonly.aof ← AOF file (if enabled)

RDB Configuration:

# Automatic snapshots
save 900 1 # After 900 sec (15 min) if at least 1 key changed
save 300 10 # After 300 sec (5 min) if at least 10 keys changed
save 60 10000 # After 60 sec if at least 10000 keys changed

# Compression
rdbcompression yes # Use LZF compression (CPU vs size trade-off)

# Checksum
rdbchecksum yes # CRC64 integrity check

AOF File Format and Structure​

What's Inside an AOF File?

AOF (Append-Only File) is a text-based log of every write operation, stored in RESP protocol format (Redis Serialization Protocol).

graph TD
AOF[AOF File] --> Cmd1[Command 1: SET key1 value1]
AOF --> Cmd2[Command 2: INCR counter]
AOF --> Cmd3[Command 3: LPUSH list item]
AOF --> CmdN[Command N: ...]

Cmd1 --> RESP1[RESP Format]
Cmd2 --> RESP2[RESP Format]

AOF File Structure (Sequential Log):

┌─────────────────────────────────────────────────────────┐
│ *2 ← Array with 2 elements │
│ $6 ← Bulk string, 6 bytes │
│ SELECT ← Command name │
│ $1 ← Bulk string, 1 byte │
│ 0 ← Database number │
├─────────────────────────────────────────────────────────┤
│ *3 ← Array with 3 elements │
│ $3 ← Bulk string, 3 bytes │
│ SET ← Command name │
│ $4 ← Bulk string, 4 bytes │
│ key1 ← Key │
│ $6 ← Bulk string, 6 bytes │
│ value1 ← Value │
├─────────────────────────────────────────────────────────┤
│ *2 │
│ $4 │
│ INCR │
│ $7 │
│ counter │
├─────────────────────────────────────────────────────────┤
│ ... (commands continue chronologically) │
└─────────────────────────────────────────────────────────┘

RESP Protocol Format:

RESP Types:
*N = Array with N elements
$N = Bulk string with N bytes
+ = Simple string
- = Error
: = Integer

Example: Storing a Hash

HSET user:1 name "Alice" age 30

Becomes in AOF:

*5
$4
HSET
$6
user:1
$4
name
$5
Alice
$3
age
$2
30

Does AOF Have Indexes?

❌ No — AOF also has NO indexes.

AOF is:

  • Append-only (writes only at the end)
  • Sequential read (replay from start)
  • No random access needed

How AOF Restore Works:

sequenceDiagram
participant Redis
participant AOF File
participant Memory

Redis->>AOF File: Open file
loop For each command
AOF File->>Redis: Read RESP command
Redis->>Redis: Parse command
Redis->>Memory: Execute command
Redis->>Memory: Update data structures
end
AOF File->>Redis: EOF reached
Redis->>Memory: Ready to serve

AOF Rewrite Process:

Over time, AOF grows as it logs every write. Redis periodically rewrites AOF to compact it.

flowchart TD
Start[AOF grows too large] --> Trigger[Auto-rewrite triggered]
Trigger --> Fork[fork() child process]

Fork --> Parent[Parent: Continue logging to old AOF]
Fork --> Child[Child: Iterate current memory]

Child --> Snapshot[Read current state from memory]
Snapshot --> Generate[Generate minimal commands]
Generate --> NewAOF[Write to temp AOF file]

Parent --> Buffer[Buffer new writes in memory]
NewAOF --> Append[Append buffered writes]
Append --> Rename[Atomic rename to appendonly.aof]
Rename --> Replace[Old AOF replaced]

Before Rewrite:

SET counter 1
INCR counter
INCR counter
INCR counter
DEL temp_key
SET temp_key value
DEL temp_key
SET user:1 Alice
SET user:1 Bob
... (thousands of operations)

After Rewrite:

SET counter 4
SET user:1 Bob
... (only final state)

AOF Rewrite = Converting history into current state

AOF Fsync Strategies:

AOF offers three durability levels via appendfsync:

StrategyBehaviorDurabilityPerformanceData Loss Risk
alwaysfsync() after every writeMaximumSlowest~0 (only crash during write)
everysecfsync() once per secondGoodFast~1 second of data
noLet OS decide when to flushMinimalFastestPotentially 30+ seconds

Technical Detail:

// Simplified AOF write flow
write_command_to_aof_buffer(command);

if (appendfsync == ALWAYS) {
fsync(aof_fd); // Force write to disk NOW
} else if (appendfsync == EVERYSEC) {
// Background thread calls fsync() every second
schedule_fsync_background();
} else {
// OS will flush when it wants
}

Storage Organization:

Redis Data Directory:
/var/lib/redis/
├── appendonly.aof ← Main AOF file
├── appendonly.aof.manifest ← Manifest for multi-part AOF (Redis 7+)
├── appendonly.aof.1.base.rdb ← Base RDB (Redis 7+)
├── appendonly.aof.1.incr.aof ← Incremental AOF (Redis 7+)
└── temp-rewriteaof-<pid>.aof ← Temporary during rewrite

Redis 7+ Multi-Part AOF:

Redis 7 introduced AOF 2.0 with multiple files:

graph LR
Base[Base RDB Snapshot] --> Incr1[Incremental AOF 1]
Incr1 --> Incr2[Incremental AOF 2]
Incr2 --> IncrN[Incremental AOF N]

Benefits:

  • Faster rewrite (doesn't block as long)
  • Better crash recovery
  • Lower memory overhead

AOF Trade-offs:

Advantage ✅Disadvantage ❌
Much better durability (per-write)Larger file size than RDB
Human-readable (RESP text format)Slower recovery (must replay all commands)
Automatic rewrite compactionRewrite can spike CPU/memory
Granular fsync controlNot portable across Redis versions (sometimes)
Append-only = crash-safe writesCan grow very large without rewrite
Preserves command historyNo random access to specific keys

AOF Configuration:

# Enable AOF
appendonly yes

# Fsync strategy
appendfsync everysec # Recommended balance

# Auto-rewrite trigger
auto-aof-rewrite-percentage 100 # Rewrite when 100% larger
auto-aof-rewrite-min-size 64mb # Minimum size to trigger

# AOF file name
appendfilename "appendonly.aof"

# Use RDB preamble (hybrid mode)
aof-use-rdb-preamble yes # Store base snapshot as RDB + incremental AOF

Hybrid Persistence: RDB + AOF Together​

How They Work Together:

graph TD
Write[Write Command] --> Memory[Update Memory]
Memory --> AOF[Append to AOF]

Timer[Periodic Timer] --> RDB[Create RDB Snapshot]
RDB --> Disk1[dump.rdb]
AOF --> Disk2[appendonly.aof]

Memory -.Source of Truth.-> Both[Both derive from memory]

Restart Priority:

flowchart TD
Start[Redis Starts] --> Check{AOF enabled?}
Check -->|Yes| AOFExists{AOF file exists?}
Check -->|No| UseRDB[Load dump.rdb]

AOFExists -->|Yes| LoadAOF[Load AOF<br/>Higher priority]
AOFExists -->|No| LoadRDB[Load dump.rdb]

LoadAOF --> Validate[Validate data]
LoadRDB --> Validate
UseRDB --> Validate

Why AOF Has Priority?

AOF is more up-to-date (potentially has writes after last RDB snapshot)

Hybrid Persistence Strategy (Best Practice):

# Enable both
save 900 1
appendonly yes
appendfsync everysec

# Use RDB-format base in AOF rewrites
aof-use-rdb-preamble yes

Result:

  • RDB provides fast restart baseline
  • AOF provides durability since last snapshot
  • Hybrid mode = best of both worlds

File Format Evolution and Compatibility​

RDB Versions:

VersionRedis VersionKey Changes
11.0Initial format
52.6Millisecond expiry
62.8LZF compression improvements
73.2Quick list encoding
95.0Stream data type, 64-bit lengths

Compatibility:

  • Redis can read older RDB formats
  • Redis cannot read newer RDB formats
  • Always test before downgrading

AOF Format:

  • RESP2 (traditional)
  • RESP3 (Redis 6+, backward compatible)
  • Multi-part AOF (Redis 7+)

Performance Characteristics​

RDB Performance:

graph LR
Fork[fork] -->|Instant| Child[Child Process]
Child -->|Sequential Write| Disk[Disk I/O]
Disk -->|~1-10 seconds| Complete[Complete]

Fork -.Copy-on-Write.-> Memory[Memory Overhead]

Typical RDB Save Time:

Dataset SizeApproximate TimeMemory Overhead
1 GB1-2 seconds< 50 MB (COW)
10 GB10-20 seconds< 500 MB (COW)
100 GB100-200 seconds< 5 GB (COW)

AOF Performance:

  • Write latency: +0.1-1 ms (depends on fsync strategy)
  • Rewrite time: Similar to RDB (background fork)
  • Recovery time: Slower than RDB (replay all commands)

Comparison:

Recovery Time:
RDB: 10 GB → ~30 seconds (sequential read + hash table rebuild)
AOF: 10 GB → ~5 minutes (parse + execute millions of commands)

Durability:
RDB: Last snapshot (potentially minutes/hours old)
AOF: Last fsync (potentially 0-1 seconds old)

Internal Storage and I/O Optimization​

Linux Kernel Optimizations Redis Uses:

  1. fork() + Copy-on-Write

    • Shared memory pages until modification
    • Minimal memory duplication
  2. mmap() for file I/O

    • Memory-mapped files for fast access
  3. fallocate() for AOF

    • Pre-allocate disk space for better performance
  4. fdatasync() vs fsync()

    • fdatasync() = faster, skips metadata updates

Write Amplification:

AOF Write Amplification = How many disk writes per logical write?

appendfsync always: 1 fsync per write = High durability, high cost
appendfsync everysec: 1 fsync per second = Good balance
appendfsync no: OS buffering = Low cost, low durability

Trade-offs Summary​

RDB:

✅ Fast restart
✅ Compact file
✅ No runtime overhead
✅ Perfect for backups

❌ Data loss risk (between snapshots)
❌ Memory spike during fork
❌ Long save time for huge datasets
❌ No incremental updates

AOF:

✅ High durability (per-write)
✅ Human-readable
✅ Continuous updates
✅ Automatic compaction (rewrite)

❌ Larger files
❌ Slower recovery
❌ Slight write latency overhead
❌ Rewrite can be expensive

Hybrid (Recommended):

✅ Best of both worlds
✅ Fast recovery (RDB base)
✅ Minimal data loss (AOF incremental)

⚠️ Slightly more complex configuration
⚠️ Uses more disk space

Key Takeaways​

  1. RDB = Binary snapshot, NO indexes, sequential I/O optimized
  2. AOF = Command log, NO indexes, append-only optimized
  3. Both use fork() to avoid blocking main thread
  4. Neither has random access capability - they're recovery mechanisms, not databases
  5. Hybrid mode (RDB + AOF) is the recommended production setup
  6. File formats evolve with Redis versions - test compatibility when upgrading

Mental Model:

┌─────────────────────────────────────────────┐
│ In-Memory Data Structures │
│ (Source of Truth) │
└─────────┬─────────────────────┬─────────────┘
│ │
▼ ▼
┌──────────┐ ┌──────────┐
│ RDB │ │ AOF │
│ Snapshot │ │ Command │
│ File │ │ Log │
└──────────┘ └──────────┘

No Indexes No Indexes
Sequential I/O Append-Only
Fast Recovery High Durability

Replication and High Availability​

Master–Replica Model​

Replication means copying data from one Redis to others.

graph TD
Master --> Replica1
Master --> Replica2
  • Writes go to Master
  • Reads can go to Replicas
  • Replicas are eventually consistent

Why Replicas Matter:

  • Scale reads
  • Protect against data loss
  • Enable failover

How Master–Replica Works Internally​

Key correction:

❗ Master is NOT always the only interface to the application

That's a design choice, not a Redis rule.

Logical model:

graph TD
App --> Master
Master --> Replica1
Master --> Replica2
  • Writes → Master
  • Reads → Master or Replicas (optional)

How data flows:

There is no separate sync daemon.

Replication is done by:

  • Redis master
  • Redis replica
  • Using a replication protocol

Replication phases:

Phase 1: Full sync (initial)

sequenceDiagram
Replica->>Master: SYNC
Master->>Replica: RDB snapshot
Master->>Replica: buffered writes

Phase 2: Continuous replication

sequenceDiagram
Client->>Master: Write
Master->>Replica: Propagate command
  • Replicas replay commands
  • Replication is asynchronous

Can applications talk to replicas?

Yes — but carefully.

OperationSafe on Replica?
Reads✅
Writes❌
Strong consistency❌

Master and Replica Relationship:

  • Master and replica are the same Redis server binary
  • Same persistence mechanisms (RDB/AOF)
  • Same data model
  • Same networking behavior
  • Roles are logical, not physical

Replacement happens via failover, not magically.

graph LR
Master --> Replica
Replica -->|Failover| NewMaster

Sentinel (High Availability Brain)​

Redis Sentinel watches Redis instances.

What Sentinel Does:

  • Monitors Redis health
  • Detects failures
  • Promotes replicas
  • Reconfigures clients

Sentinel Architecture:

graph TD
Sentinel1 --> Master
Sentinel2 --> Master
Sentinel3 --> Master
Master --> Replica

Sentinels vote to avoid false failover.

Why 3 Sentinels?

Quorum and fault tolerance

Sentinel is NOT Redis:

  • Sentinel is a separate process
  • It monitors Redis nodes
  • It talks to other Sentinels

Why multiple Sentinels?

If only 1 Sentinel:

  • Network glitch = false failover

If 2 Sentinels:

  • Split brain possible

If 3 Sentinels:

  • Majority vote works

Odd number rule:

graph TD
Sentinel1 --> Vote
Sentinel2 --> Vote
Sentinel3 --> Vote

Majority = 2 out of 3 Failover only happens if quorum is reached

Sentinel failure tolerance:

SentinelsCan lose
31
52

Automatic Failover Flow:

sequenceDiagram
Sentinel->>Master: Ping
Master--x Sentinel: No response
Sentinel->>Replica: Promote
Replica->>Sentinel: I am master

Redis Cluster and Sharding​

Understanding Nodes, Instances, and Masters​

Terminology:

TermMeaning
NodeA machine / VM / container
Redis instanceOne Redis process
MasterA Redis instance that owns slots
ReplicaA Redis instance replicating a master

Important:

🔴 One Redis instance = one role (master OR replica) 🔴 One node can host multiple Redis instances (rare in prod)

Correct architecture:

graph TD
Node1 --> MasterA
Node2 --> ReplicaA1
Node3 --> ReplicaA2

Best practice:

One Redis instance per node for:

  • Simpler failure domain
  • Clear CPU / memory isolation
  • Predictable persistence behavior

What Is Redis Cluster?​

When a single Redis node is not enough, you use Redis Cluster.

What Is Sharding?

Splitting data across multiple Redis nodes.

graph TD
App --> Slot1[Redis Node A]
App --> Slot2[Redis Node B]
App --> Slot3[Redis Node C]

Correct Redis Cluster Layout:

A Redis Cluster has multiple masters, each responsible for a subset of slots

graph TD
Master1 --> Replica1
Master2 --> Replica2
Master3 --> Replica3

Each master:

  • Owns a different shard (slot range)
  • Has 0+ replicas

Why multiple masters are required:

ReasonExplanation
ScalabilityOne master cannot hold entire keyspace
ThroughputWrites distributed across masters
AvailabilityFailure isolates only part of data

Understanding Sharding​

Correct definition:

Sharding partitions the KEYSPACE, not snapshots

Sharding:

  • Happens at runtime
  • Happens at key routing time
  • Is independent of persistence

Correct sharding model:

graph TD
Keyspace --> Slot0-5000 --> MasterA
Keyspace --> Slot5001-10000 --> MasterB
Keyspace --> Slot10001-16383 --> MasterC

Replica relationship:

graph TD
MasterA --> ReplicaA1
MasterB --> ReplicaB1

❗ Replicas do NOT participate in sharding ❗ Replicas mirror exactly one master's shard

Snapshot relationship:

graph TD
MasterA --> RDB_A
MasterB --> RDB_B
ReplicaA1 --> RDB_A_replica

📌 Each instance snapshots only its own shard

Important Clarifications:

  • Sharding = logical keyspace partitioning concept
  • Realized by assigning slot ranges to Redis master instances
  • NOT a file, NOT a database, NOT a persistence mechanism

Shard Definition:

Shard = Master + its assigned slots (+ replicas)

One master owns one or more slot ranges, which together form a shard.

A master can own multiple non-contiguous slot ranges, but conceptually they are treated as one shard.

graph TD
MasterA --> Slot1_100
MasterA --> Slot300_500

Redis Cluster Architecture​

Critical Understanding:

Redis has NO "cluster main controller" Redis Cluster is fully decentralized

Every Redis node:

  • Knows the slot map
  • Can redirect clients
  • Acts independently

What Redis Cluster Solves:

ProblemSolved?
Horizontal scaling✅
Node failure✅
Cross-slot transactions❌ (limited)

Big Picture:

graph TD
App --> RedisCluster
RedisCluster --> Master1
RedisCluster --> Master2
Master1 --> Replica1
Master2 --> Replica2
Sentinel --> RedisCluster
RedisCluster --> Disk

Hash Slots Deep Dive​

Why Redis Has Hash Slots (Not Just Hash % N)​

Before Redis Cluster (pre-3.0), people did:

node = hash(key) % number_of_nodes

Problems with that approach:

  • Adding/removing a node reshuffles almost all keys
  • Massive cache invalidation
  • Impossible to do online resharding

Redis Cluster Solution: Hash Slots

Redis introduces an indirection layer:

Key → Hash → Slot → Master Node
flowchart LR
Key --> Hash
Hash --> Slot[0..16383]
Slot --> Master

Fixed number of slots:

  • 16384 slots (0–16383)
  • Slots never change
  • Only slot → node mapping changes

This is the foundation of Redis Cluster scalability.

Exact Hash / Slot Algorithm​

Step 1: Extract hash tag (if any)

key = user:{42}:profile
hash_input = 42

If {} exists → only content inside is hashed If not → entire key is hashed

Step 2: CRC16

slot = CRC16(hash_input) % 16384

Why CRC16?

  • Fast
  • Good distribution
  • Deterministic
  • Same across all clients

Step 3: Slot → Master lookup

flowchart LR
Slot -->|Slot table| MasterA

Important:

🔹 Only the key string matters for slot calculation 🔹 The value is irrelevant for routing

Slot Ranges and Assignment​

Is the Redis slot just an integer calculated by CRC16?

✅ Yes — in modern Redis Cluster:

slot = CRC16(key) % 16384
  • Slot range is fixed: 0 – 16383
  • This has never changed since Redis Cluster was introduced
  • Slots are integers only, not ranges, not objects

Important:

A key always hashes to the same slot forever. Slot migration only changes which node owns that slot, not how the slot is computed.

Example: 3-Master Cluster

Total slots = 16384

MasterSlots
Master A0 – 5460
Master B5461 – 10922
Master C10923 – 16383

This is:

  • Balanced
  • Explicit
  • Deterministic

📌 These ranges are not required to be contiguous.

Can slots in the same shard be non-contiguous?

✅ Yes — absolutely allowed

Redis Cluster rule:

A master can own any set of slots, contiguous or not

Example:

graph TD
Slot1_100 --> MasterA
Slot201_300 --> MasterA
Slot101_200 --> MasterB

✔ Redis does not require continuity ✔ Continuity is usually chosen for operational simplicity

Why Redis allows non-contiguous slots:

  • Online resharding
  • Gradual migration
  • Failure recovery
  • Load balancing

Why operators prefer contiguous slots:

  • Easier to reason about
  • Faster debugging
  • Cleaner monitoring

Slot Assignment​

Who assigns and manages slot ranges?

❌ Not applications ❌ Not hashing logic ❌ Not modulo arithmetic

✅ Redis Cluster itself (with admin tools)

Slot assignment happens via:

  • redis-cli --cluster create
  • redis-cli --cluster reshard
  • Redis operators / orchestration systems
  • Cloud Redis services

How slot assignment works internally:

Each master maintains:

slots[0..16383] → owned / not owned

Cluster-wide metadata looks like:

flowchart TB
SlotMap["Cluster Slot Map"]

SlotMap -->|0-5460| MasterA
SlotMap -->|5461-10922| MasterB
SlotMap -->|10923-16383| MasterC

Clients cache this map.

Where Is Slot Ownership Stored?

Each Redis node keeps:

  • A full copy of the cluster topology
  • Slot → node mapping
  • Node health state

Stored in:

nodes.conf

This file is:

  • Managed automatically
  • Updated by Redis itself
  • Never edited manually

Slot Migration​

Are Slots Physical Things?

❌ No — slots are NOT physical objects

A slot is:

  • A logical label (0–16383)
  • Used only for routing and ownership
  • Stored as cluster metadata in each Redis node

There is:

  • ❌ No slot file
  • ❌ No slot directory
  • ❌ No slot process
  • ❌ No slot memory region

What is physical?

PhysicalDescription
Redis instanceOS process
MemoryActual key-value storage
KeysStored in RAM
RDB / AOFPersistence files
TCP connectionsClient & cluster bus

Slots are just numbers used to decide which keys belong where.

What Does "Slot Migration" Actually Mean?

Slot migration is a two-part operation:

1. Move ownership of a slot
2. Move keys belonging to that slot

Step-by-Step Slot Migration:

Let's migrate slot 8000 from Master A → Master B

Step 1: Mark slot as "migrating" and "importing"

sequenceDiagram
participant A as Master A
participant B as Master B
participant C as Clients

A->>A: slot 8000 = MIGRATING to B
B->>B: slot 8000 = IMPORTING from A
  • Slot still logically exists
  • Both nodes know migration is in progress

Step 2: Move keys belonging to the slot

flowchart LR
Key1 -->|DUMP| A
A -->|RESTORE| B

Redis internally:

  • Scans keys in slot 8000 on A
  • Serializes each key
  • Sends to B
  • Deletes from A

This is key-level movement, not memory blocks.

What actually moves:

KEY + VALUE + TTL + TYPE

Internally via:

MIGRATE host port key db timeout

Step 3: Finalize slot ownership

sequenceDiagram
A->>A: remove slot 8000
B->>B: own slot 8000
A->>Cluster: update slot map
B->>Cluster: update slot map

Now:

slot 8000 → Master B

Important Corrections:

  • Slots are never transmitted
  • Only key-value pairs move
  • Slot metadata is updated separately

There is no structure like:

{
"slot": 5793,
"data": {...}
}

Who Coordinates Slot Migration?

❌ Not Sentinel ❌ Not a central controller ❌ Not ZooKeeper ❌ Not etcd

✅ Redis nodes coordinate themselves

Redis Cluster is fully decentralized.

Coordination happens via:

  • Cluster bus (TCP port: client_port + 10000)
  • Gossip protocol
  • Cluster state machine inside each Redis process

Who initiates migration?

Usually:

  • redis-cli --cluster reshard
  • Or orchestration tools (K8s operator, Ansible, etc.)

But once initiated: ➡ Redis nodes do everything themselves

How Do Clients Survive During Migration?

This is where ASK / MOVED comes in.

During migration:

sequenceDiagram
Client->>MasterA: GET key_in_8000
MasterA-->>Client: ASK MasterB
Client->>MasterB: ASKING + GET key
  • Temporary redirect
  • Client retries once
  • No downtime

After migration:

sequenceDiagram
Client->>AnyNode: GET key
Node-->>Client: MOVED slot 8000 MasterB
  • Permanent redirect
  • Client updates slot cache

Hash Tags: Forcing Co-Location​

Problem:

Multi-key operations must be on same slot

MGET user:1 user:2 ❌ (likely different slots)

Solution: Hash tags

user:{42}:name
user:{42}:email

Both hash on 42

flowchart LR
user42name --> SlotX
user42email --> SlotX
SlotX --> MasterA

✔ Multi-key ops work ✔ Transactions work ✔ Lua scripts work

Hash Tag Rule:

Only substring inside { } is hashed

Best practice:

  • Use hash tags sparingly
  • Avoid hotspotting
  • Never hash on low-cardinality values (e.g. {US})

Can You Customize Hash / Slot Logic?​

What you CANNOT customize:

ItemCustomizable
Number of slots❌ Fixed at 16384
Hash algorithm❌ CRC16 is fixed
Slot computation❌ Fixed
Slot redirection behavior❌ Fixed

Redis must behave identically across all clients.

What you CAN influence:

1. Key design

This is your only real control

user:{id}:*
order:{id}:*
session:{id}

2. Slot distribution

Via:

  • CLUSTER ADDSLOTS
  • CLUSTER DELSLOTS
  • redis-cli --cluster reshard

3. Read routing

  • Read from master only
  • Or allow replica reads (client-level)

Complete Data Flow Example​

Example: Writing and Reading Data

Data: {user: "whereq"}

Write Flow:

  1. Application stores:

    SET user:whereq '{"user":"whereq"}'
  2. Redis sees:

    KEY = "user:whereq"
    VALUE = '{"user":"whereq"}'
  3. Slot calculation:

    slot = CRC16("user:whereq") % 16384 = 5793
  4. Cluster metadata contains:

    MasterA owns slots: 4096–8191 (example)
    → slot 5793 ∈ MasterA
  5. Data stored in MasterA's RAM (not directly in RDB)

Read Flow:

  1. Application sends:

    GET user:whereq
  2. Client hashes:

    CRC16("user:whereq") → slot 5793
  3. Client (or Redis redirect) routes to MasterA

  4. MasterA returns:

    {"user":"whereq"}

After Migration:

If slot 5793 migrates to MasterB:

  1. MasterA sends entire key-value pair to MasterB
  2. Cluster metadata updated: slot 5793 now belongs to MasterB
  3. Application reads: same hash → slot 5793 → now routes to MasterB
  4. Application receives data transparently

Key Principle:

Slots route data; nodes store data


Operational Considerations​

Adding a New Master to the Cluster​

What happens when you add a new master node?

Step 1: New master joins the cluster

redis-cli --cluster add-node NewMaster ExistingMaster

Result:

  • New master joins with ZERO slots
  • It holds no data
  • It serves no traffic
flowchart LR
A[Master A<br/>Slots 0–5460]
B[Master B<br/>Slots 5461–10922]
C[Master C<br/>Slots 10923–16383]
D[New Master D<br/>Slots: none]

A --- B --- C --- D

📌 Redis will never auto-assign slots. This is intentional.

Why Redis does NOT auto-rebalance slots:

Auto-rebalance would be dangerous:

RiskExplanation
Massive data movementMillions/billions of keys
Latency spikesMIGRATE blocks CPU
Unpredictable IODisk + network storms
App impactCascading failures

Redis follows the principle:

Data movement must be explicit, observable, and reversible

Step 2: Resharding (manual or scripted)

You explicitly run:

redis-cli --cluster reshard

Or specify:

redis-cli --cluster reshard \
--cluster-from <old-master-id> \
--cluster-to <new-master-id> \
--cluster-slots 4096

This:

  • Moves slots, not files
  • Migrates key/value pairs
  • Updates cluster metadata
  • Happens online

Example: 3 Masters → 4 Masters

Before:

MasterSlots
A0–5460
B5461–10922
C10923–16383

Target distribution (example):

MasterSlots
A0–4095
B4096–8191
C8192–12287
D12288–16383

During migration:

sequenceDiagram
participant Client
participant A as MasterA
participant D as MasterD

Client->>A: GET key(slot=13000)
A-->>Client: ASK 13000 D
Client->>D: GET key
D-->>Client: value

Redis uses ASK redirects during migration.

What happens to RDB/AOF after migration?

Persistence follows memory ownership:

  1. Key moves from A → D (RAM)
  2. Slot ownership updated
  3. Next RDB snapshot:
    • A's RDB ❌ no longer has the key
    • D's RDB ✅ contains the key

No syncing between RDBs ever happens.

Adding replicas:

You normally add replicas AFTER slot resharding:

redis-cli --cluster add-node ReplicaOfD NewMasterD --cluster-slave

Replicas automatically sync data.

Does application need to change anything?

❌ No changes required

Clients:

  • Cache slot → master map
  • Receive MOVED / ASK
  • Refresh metadata automatically

This is why slots exist.

Memory Management​

Redis masters must keep data in RAM:

✅ Yes — Redis is fundamentally an in-memory database

Redis guarantees:

  • All active data lives in RAM
  • Persistence is for recovery only

What if data exceeds RAM?

Redis gives you explicit strategies, not silent paging.

Option 1: Eviction (most common)

maxmemory 32gb
maxmemory-policy allkeys-lru

Policies include:

  • allkeys-lru
  • volatile-lru
  • allkeys-random
  • noeviction (write fails)

📌 Redis will never swap to disk automatically.

Option 2: Redis Cluster (scale horizontally)

Instead of bigger RAM:

Add masters
Reshard slots
Distribute memory

Option 3: Redis on Flash (specialized)

Enterprise / Redis Stack only:

  • SSD-backed
  • Still RAM-first
  • Different architecture

Best Practices​

Slot layout:

  • Prefer contiguous slot ranges
  • Evenly distribute across masters

Key naming:

  • Predictable
  • Stable
  • Avoid random prefixes

Bad:

abc123:user:1
xyz999:user:2

Good:

user:1
user:2

Hash tags:

Use only when required:

✔ Sessions ✔ User-scoped objects ❌ Global counters ❌ Time series

Avoid hotspots:

Bad:

counter:{global}

Better:

counter:{region}:{id}

When Adding Masters:

✔ Add master first (empty) ✔ Reshard gradually ✔ Move slots from multiple masters ✔ Monitor latency during migration ❌ Don't auto-rebalance ❌ Don't migrate too many slots at once


Mental Model Summary​

Final Mental Model (The One to Keep)​

KEY --(CRC16 % 16384)--> SLOT --(cluster map)--> MASTER --> REPLICAS
  • Slot ≠ shard
  • Slot ≠ data
  • Slot = routing label
  • Shard = ownership of many slots

Complete Architecture​

KEY
↓
CRC16 hash
↓
SLOT (0–16383)
↓
Cluster Metadata
↓
Owning Master
↓
RAM (source of truth)
↓
RDB / AOF (recovery only)

Key Truths​

TopicCore Truth
RDBDisk snapshot of memory
AOFWrite command log
Both togetherMemory is source of truth
ReplicationMaster streams commands
SentinelExternal HA coordinator
ShardingData split by slots
SlotsLogical routing layer

One-Sentence Summary​

Redis Cluster shards the keyspace using hash slots, assigns slots to masters, replicates data to replicas, and persists each instance independently using RDB/AOF — all without a central controller.

Redis Mental Checklist (Hero Level)​

When designing with Redis, always ask:

  1. Is this hot data?
  2. Do I need atomic operations?
  3. What happens if Redis restarts?
  4. Can I tolerate eventual consistency?
  5. Do I need sharding or just replicas?

Final Thought​

Redis is powerful because it is opinionated.

If you:

  • Respect its strengths
  • Understand its limits
  • Design intentionally

Redis becomes one of the most reliable and elegant tools in distributed systems.


Quick Reference Guide​

Common Redis Patterns​

1. Cache Pattern:

Application → Check Redis → Hit? Return : Fetch from DB → Store in Redis → Return

2. Counter Pattern:

INCR page_views
INCRBY user:123:credits 50

3. Distributed Lock:

SET lock:resource "token" NX PX 30000 # Atomic lock with 30s TTL

4. Rate Limiting:

INCR rate:user:123:20260108 # Count requests per day
EXPIRE rate:user:123:20260108 86400

5. Session Store:

SETEX session:abc123 3600 '{"user_id":123,"role":"admin"}'

Common Commands Cheat Sheet​

OperationCommandExample
Set valueSET key value [EX seconds]SET user:1 "Alice" EX 3600
Get valueGET keyGET user:1
IncrementINCR keyINCR counter
Hash setHSET key field valueHSET user:1 name "Alice"
Hash getHGET key fieldHGET user:1 name
List pushLPUSH key valueLPUSH queue:jobs task1
List popRPOP keyRPOP queue:jobs
Set addSADD key memberSADD tags:post:1 "redis"
Sorted set addZADD key score memberZADD leaderboard 100 "player1"
Check existenceEXISTS keyEXISTS user:1
DeleteDEL keyDEL user:1
Set expiryEXPIRE key secondsEXPIRE session:abc 3600
Get TTLTTL keyTTL session:abc

Configuration Quick Reference​

Persistence:

# RDB Snapshots
save 900 1 # Save if 1+ keys changed in 900s
save 300 10 # Save if 10+ keys changed in 300s
save 60 10000 # Save if 10000+ keys changed in 60s
rdbcompression yes
rdbchecksum yes

# AOF
appendonly yes
appendfsync everysec # Options: always | everysec | no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-use-rdb-preamble yes # Hybrid mode

# Hybrid (Recommended)
save 900 1
appendonly yes
appendfsync everysec
aof-use-rdb-preamble yes

Memory Management:

maxmemory 2gb
maxmemory-policy allkeys-lru # Options:
# allkeys-lru - Evict any key, LRU
# volatile-lru - Evict keys with TTL, LRU
# allkeys-random - Evict any key, random
# volatile-random - Evict keys with TTL, random
# volatile-ttl - Evict keys with shortest TTL
# noeviction - Return errors when memory limit reached

Replication:

# On replica
replicaof <master-ip> <master-port>
replica-read-only yes

Cluster:

cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 15000

Troubleshooting Guide​

ProblemPossible CausesSolutions
Slow queriesLarge keys, blocking commandsUse SCAN instead of KEYS, avoid SMEMBERS on large sets
Memory spikesRDB fork during saveIncrease memory, schedule saves during low traffic
Data lossNo persistence enabledEnable AOF with appendfsync everysec
Connection timeoutsNetwork issues, maxclients limitCheck maxclients, network latency
Replication lagMaster overloaded, slow networkOptimize write load, check network bandwidth
Cluster MOVED errorsSlot migration in progressNormal during resharding, wait or retry
OOM errorsMemory limit reachedCheck maxmemory-policy, add more nodes

Performance Tips​

DO:

  • ✅ Use pipelining for bulk operations
  • ✅ Use Redis transactions (MULTI/EXEC) for atomicity
  • ✅ Set appropriate TTLs to expire unused data
  • ✅ Use hash tags for multi-key operations in cluster
  • ✅ Monitor with INFO command and Redis monitoring tools
  • ✅ Use connection pooling in applications
  • ✅ Batch operations when possible

DON'T:

  • ❌ Use KEYS * in production (use SCAN instead)
  • ❌ Store huge values (> 100KB) in single keys
  • ❌ Use Redis as primary database without persistence
  • ❌ Run blocking commands (BLPOP with long timeout) without care
  • ❌ Ignore memory limits and eviction policies
  • ❌ Use transactions across multiple hash slots in cluster
  • ❌ Store sensitive data without encryption layer

Redis Versions Features​

VersionKey Features
3.0Redis Cluster introduced
3.2GEO commands, Quick List
4.0Modules system, lazy freeing
5.0Streams data type, new sorted set commands
6.0SSL/TLS, ACLs, RESP3, threaded I/O
6.2Tracking, client-side caching
7.0Functions, sharded pub/sub, ACL improvements, Multi-part AOF

Monitoring Metrics​

Key Metrics to Watch:

# Memory usage
INFO memory
- used_memory
- used_memory_rss
- mem_fragmentation_ratio

# Performance
INFO stats
- instantaneous_ops_per_sec
- total_commands_processed
- keyspace_hits / keyspace_misses (hit ratio)

# Persistence
INFO persistence
- rdb_last_save_time
- rdb_changes_since_last_save
- aof_current_size

# Replication
INFO replication
- role (master/replica)
- connected_slaves
- master_repl_offset

# Cluster
CLUSTER INFO
- cluster_state
- cluster_slots_assigned
- cluster_slots_ok

Resource Planning​

Sizing Guidelines:

Use CaseRecommended SetupExample
Small cacheSingle master1-4 GB RAM
Production cacheMaster + 1-2 replicas + Sentinel8-32 GB RAM per node
Large dataset3+ master cluster, 1 replica each32-256 GB RAM per node
High availabilitySentinel (3+ instances) or ClusterOdd number of sentinels

Network Requirements:

  • Client ↔ Redis: Low latency (< 1ms ideal)
  • Cluster nodes: Low latency (< 1ms), high bandwidth
  • Replication: Sustained bandwidth for writes

Key Corrections and Clarifications​

Slots and Data Storage​

Important:

  • Slots are just labels
  • Nodes own labels
  • Keys follow labels
  • Redis nodes gossip label ownership
  • Shard = naming convenience

Correct Understanding:

  • Slot is a pure logic term
  • Slot represents the keyspace
  • Slot is basically in Redis metadata
  • Slot is a logic label
  • Data is NOT stored "in slots"
  • Data is stored in Master's memory
  • Slot routes request to the correct master

Migration and Persistence​

Important:

  • Slots are NOT stored in RDB
  • Slots are NOT persisted with data
  • Slots live in cluster metadata
RDB = raw key/value memory dump
Cluster metadata = routing truth

Summary Answers:

QuestionCorrect Answer
Snapshot after migration?❌ No
Temporary RDB inconsistency?✅ Yes, expected
Crash before snapshot safe?✅ Yes
Slot logic during RDB load?Metadata-driven
Hash during load?Only for validation
Data always in RAM?✅ Yes
Exceed RAM?Evict or scale

Application Interaction​

Does the application care which master or replica it talks to?

✅ Conceptually correct

Writes:

  • Application does not choose the master
  • Client library computes hash slot
  • Client routes to correct master
  • If wrong node → MOVED redirect

✔ Application logic remains unaware

Reads:

  • Reads go to masters by default
  • Guarantees strong consistency
  • Clients may read from replicas
  • Requires explicit client configuration

⚠️ Replica reads may be eventually consistent

Adding a New Master​

When a new master is added later, Redis does NOT automatically rebalance slots. Slot redistribution (resharding) is an explicit, controlled operation.

Key Principle:

Adding a master does NOT change hashing. It only changes slot ownership — explicitly.

KEY → SLOT (fixed) → MASTER (mutable)