PeekDBDatabase Internals
  • Home
  • Blog
  • Docs
  • Papers
  • About
English/中文

Blog

Deep dive into database kernel internals

      • PostgreSQL Buffer Manager
      • PostgreSQL Heap Storage
      • PostgreSQL Tuple Layout
      • PostgreSQL Free Space Map
      • PostgreSQL Tablespaces
      • PostgreSQL Virtual File Descriptors
        • SQL Parser
        • Why LALR(1)
        • SQL and Relational Theory
        • Why SQL Is Based on Relational Theory
        • Why SQL Still Exists
        • Type System
      • Regular Expressions
      • Cache Lines
      • NUMA Architecture and CPU Affinity
      • Introduction to SIMD
        • Understanding Pointers
        • C vs. Rust: Variable Declaration
        • #include Preprocessor Directive
        • Multithreaded Programming
        • Pipe
        • Signal
        • Go Basics
        • C++ Primer Plus Notes
        • STL Sequence Containers
        • STL Heap Algorithms
        • STL Associative Containers
        • When Copy Constructors Are Called
        • Virtual Destructors
        • Empty Class/Struct Size
        • Prefix vs. Postfix ++ Overloading
        • C++ Study Notes
      • Better Study Resources
        • Redis User Management
        • Redis Replication (masteruser)
        • Redis Max Clients
        • Redis Protected Mode
        • Redis TLS
        • Redis & vm.overcommit_memory
        • Redis Upgrade
        • Graceful Shutdown
        • RedisShake Migration Tool
        • Stalls Caused by bgsave
        • Sentinel Client Reconfig Script
        • Redis High Availability & Replication
        • Sentinel Source Code Analysis
        • Redis Sentinel Mode
        • Redis Cluster
        • Redis Cluster Internals
        • Redis Transactions
        • Tencent Cloud Redis
        • Compile, Install & Use Redis
        • Redis Features & Internals Overview
        • Redis String Type (SDS)
        • Redis Persistence (RDB)
        • Redis Persistence (AOF)
        • How Redis Executes a Command
        • Redis Hash Type Implementation

Understanding Rust Ownership, Borrowing, and Lifetimes

Understanding Rust’s three core memory-management concepts — ownership, borrowing, and lifetimes — from the perspective of memory safety. Ownership guarantees each value has exactly one owner to avoid data races; borrowing lets you access a value without taking ownership, split into mutable and immutable borrows; lifetimes help avoid dangling references and guarantee reference validity. Through side-by-side C++ and Rust code, the article shows how the compiler enforces these rules to keep memory safe.

Published on Aug 4, 2026

Understanding Cache Lines

The cornerstone of high-performance programming: cache line principles (64 bytes, spatial locality), array vs. linked list traversal benchmark (10x+ difference), cachegrind miss rate analysis, MESI protocol, false sharing and padding solutions, and PostgreSQL PG_CACHE_LINE_SIZE=128 design rationale.

Published on Jul 31, 2026

When C++ Copy Constructors Are Called

Three scenarios that trigger C++ copy constructors: object-to-object initialization, pass-by-value parameters, and returning objects from functions — with a complete code example and comparison of optimized vs. non-optimized (RVO/copy elision) behavior.

Published on Jul 31, 2026

C++ Empty Class and Empty Struct Size

Why is sizeof an empty class/struct 1 instead of 0 in C++? The C++ standard mandates that no two objects shall have the same address, so the compiler adds a dummy byte to ensure distinct addresses.

Published on Jul 31, 2026

C++ Prefix vs. Postfix ++ Operator Overloading

The implementation difference between C++ prefix ++ (returns reference, no temporary) and postfix ++ (returns const value, uses a dummy int parameter), with efficiency comparison and complete code/output example.

Published on Jul 31, 2026

C++ Primer Plus Notes

Reading notes for C++ Primer Plus: function templates and decltype, friend mechanism, try-catch exception handling, smart pointers (unique_ptr/shared_ptr/weak_ptr), C++11 features, and the Boost library.

Published on Jul 31, 2026

C++ Study Notes

C++ key takeaways: five programming paradigms (procedural/OOP/generic/template meta/functional), move semantics and reference collapsing, std::forward for perfect forwarding, with code examples.

Published on Jul 31, 2026

C++ Virtual Destructors

Two core questions about C++ virtual destructors: when to use them (deleting derived objects through base class pointers) and how virtual functions work (the vtable mechanism), with complete code examples and comparison output.

Published on Jul 31, 2026

Go Basics

Getting started with Go: installation, project setup (go mod), packages and visibility, functions/defer/init, concurrency (goroutines, channels, WaitGroup, Context, Mutex).

Published on Jul 31, 2026

Pipe

Linux IPC via pipes: anonymous pipes with pipe() for parent-child communication, named pipes (FIFO) with mkfifo() for arbitrary processes, with complete code examples and analysis of 4 blocking I/O edge cases.

Published on Jul 31, 2026

Signal

Linux IPC via signals: signal sources and types, reliable vs. unreliable signals, kernel handling timing (kernel-to-user transition), reentrant function concerns, with complete code examples using signal() and sigaction().

Published on Jul 31, 2026

Multithreaded Programming

A summary of C multithreaded programming: thread creation (pthread_create), three synchronization primitives (mutex, condition variable, semaphore), with complete code examples and deadlock analysis.

Published on Jul 31, 2026

NUMA Architecture and CPU Affinity

From SMP symmetric multiprocessing to NUMA non-uniform memory access: how NUMA nodes and local/remote memory latency differences work, plus hands-on affinity tuning with numactl and sched_setaffinity for CPU/NUMA pinning.

Published on Jul 31, 2026

Introduction to SIMD

A comprehensive guide to SIMD (Single Instruction Multiple Data): x86/ARM instruction set evolution, compiler auto-vectorization (-O3 -march=native), AVX2 Intrinsics programming (_mm256_loadu_ps/add_ps), and PostgreSQL optimizations (CRC32C, JSON parsing, simd.h compatibility layer).

Published on Jul 31, 2026

STL Associative Containers Notes

STL associative containers analysis: set/map backed by red-black tree (O(log n)), unordered_map backed by hash table (O(1)), with key source code showing _Rb_tree / _Hashtable internals and operator[] implementation.

Published on Jul 31, 2026

STL Heap Implementation Notes

STL heap algorithm source analysis: is_heap (validating heap property), make_heap (building a heap), push_heap (sift up), pop_heap (sift down), sort_heap (heap sort), with fully annotated source code.

Published on Jul 31, 2026

STL Sequence Containers Notes

STL sequence container source code analysis: vector (2x growth), list (doubly linked), deque (segmented space with map pointer array), stack/queue (deque-based), priority_queue (vector-based heap), with annotated source code and container comparisons.

Published on Jul 31, 2026

Redis Master-Replica Replication: masteruser

By default, replication uses the default user to connect, but production environments usually disable it. Following the principle of least privilege, set up a dedicated user for replication and configure the replica to connect to the master with that user via masteruser / masterauth.

Published on Jul 29, 2026

Redis User Management

Redis 6.0 introduced ACLs (Access Control Lists), enabling multiple users and fine-grained permission control. This article covers creating users, configuring passwords, and dividing command and key permissions — and how it differs from the single-password model before Redis 6.0.

Published on Jul 29, 2026

PostgreSQL Supports Specifying Optimizer Cost Parameters When Creating Tablespaces

PostgreSQL allows specifying optimizer cost parameters when creating tablespaces, directly influencing query cost estimation. This targets the optimizer accuracy problem in hybrid storage environments (e.g., hot data on NVMe SSDs, cold data on HDDs). This article analyzes the mechanism and includes practical usage examples.

Published on Jul 24, 2026

PostgreSQL Buffer Manager

Here we analyze the code of PostgreSQL's buffer manager. The buffer is extremely important and directly affects the database's performance and stability. It is the key component through which the database's SQL computation layer interacts with external storage (disk). Both flushing data pages to disk and reading them back go through the buffer.

Published on Jul 23, 2026

PostgreSQL Free Space Map

An in-depth look at PostgreSQL's Free Space Map (FSM): why it exists, how it is designed as a three-layer tree structure, and how it integrates with tuple insertion and vacuum.

Published on Jul 20, 2026

Recommended Database Learning Resources

Learning resources related to database technology

Published on Jul 17, 2026

Anatomy of PostgreSQL's Heap Table Storage Engine

In a database, data is actually stored in tables; in PostgreSQL specifically, it is stored in heap tables, which form the foundation of the storage engine. Here we take apart the heap table.

Published on Jul 17, 2026

PostgreSQL Page Layout and Tuple Layout

A detailed look at PostgreSQL's page layout, heap tuple structure, TID identifiers, tuple header fields, and alignment/padding optimizations

Published on Jul 17, 2026

PostgreSQL Virtual File Descriptor — VFD Mechanism

The number of files a process can open in an OS is limited, and the file descriptors a process can obtain are finite. For database processes, which frequently open many files, they may easily exceed the OS limit.

Published on Jul 17, 2026

SQL Parser

How a database turns SQL text into a query tree — lexical, syntactic, and semantic analysis, with PostgreSQL's flex/bison implementation

Published on Jul 17, 2026

The Core Design Principles of a Relational Database Type System

From a database kernel designer's perspective and first principles: why the type system exists, how it converges loose SQL into a uniquely-typed execution plan, and the design trade-offs behind implicit casts, deferred typing, and in-kernel representation

Published on Jul 17, 2026

Regular Expressions

A practical guide to regular expressions — what they are, the core syntax for matching text, and how regex engines work under the hood

Published on Jul 9, 2026

What Is LALR(1) and Why Did PostgreSQL Choose It?

A deep dive into LALR(1) parsing — how it works, why PostgreSQL adopted it in the 1980s, and why it still uses a bison grammar today

Published on Jul 6, 2026

Reading Notes on SQL and Relational Theory

Reading notes on SQL and Relational Theory by C. J. Date

Published on Jul 4, 2026

Why SQL Exists

Data independence, declarative queries, and the rise of the relational model

Published on Jul 4, 2026

Why SQL and Relational Theory Exist: A Revolution in Data Independence

From the chaos of the pre-relational era to Codd's relational model and the birth of SQL, this article examines the historical motivations and core problems that relational database theory set out to solve — answering a fundamental question: why did we need all of this?

Published on Jul 4, 2026

#include Preprocessor Directive

A study note on the C #include preprocessor directive: its processing simply inserts the header file contents at the directive position, joining the header and the current source file into one (equivalent to copy-paste). Besides the common #include "*.h", #include can also include a .c file — e.g. #include "print.c" is equivalent to copying print.c code into that position; verified via the gcc -E main.c preprocessing output, which shows the included file content is indeed copied to the directive location.

Published on Mar 15, 2026

C vs. Rust: Variable Declaration

A comparison of variable declaration between C and Rust: C lets you declare a variable without initialization and the compiler does not check it, easily causing runtime bugs; Rust requires initialization or it fails to compile (error[E0381]). It also compares mutability — Rust is immutable by default (let / let mut), while C is mutable by default (unless const), and discusses how default immutability shifts errors to compile time and improves readability and engineering practice.

Published on Mar 15, 2026

Redis Cluster

A comprehensive guide to Redis Cluster, the official sharding solution: decentralized architecture, the 16384 slots and the CRC16 key-hash algorithm (with the keyHashSlot source), hash tags, MOVED/ASK redirection and client correction, Gossip failure detection (PFail/Fail), slot migration and fault tolerance; source analysis of node startup and clusterCron, cluster initialization (redis-cli --cluster create); cluster setup (3-master example, cluster info/nodes), HA replica placement and failover, scaling (add-node/reshard/rebalance/del-node), backup/restore, and the common cluster commands.

Published on Mar 15, 2026

Redis Cluster Internals

A walkthrough of Redis Cluster internals: the decentralized distributed solution for sharding and high availability; 16384 hash slots and CRC16 key hashing with the clusterNode slots bitmap; the cluster bus and the Gossip protocol (PING/PONG/MEET, automatic discovery via CLUSTER MEET); failure detection (PFAIL subjective down, FAIL objective down with quorum, the flags bitfield); failover election (clusterHandleSlaveFailover, failover_auth_sent/failover_auth_count); the HA workflow (majority confirmation, configEpoch to prevent split-brain, MOVED redirection); and the design rationale — decentralization, high performance, scalability, and eventual consistency.

Published on Mar 15, 2026

Understanding Pointers

A study note on understanding pointers in C, based on "Computer Systems: A Programmer's Perspective". Covers the key principles of how pointers map to machine code: every pointer has a type (the type determines how many bytes are read from the starting address; void * as a generic pointer); every pointer has a value (the address of an object, with NULL(0) meaning it points nowhere); type casting only changes the type, not the value, and changes the scaling of pointer arithmetic; and pointers can also point to functions (a function pointer holds the address of the function's first instruction). Includes a C example (Node/IntNode/CharNode) that demonstrates type-based reinterpretation and its output.

Published on Mar 15, 2026

Redis Sentinel Mode

A practical operations guide to Redis Sentinel: how it relates to master-replica replication, its four features (monitoring / notification / automatic failover / configuration provider), a full sentinel.conf reference, startup examples, deployment recommendations (at least 3 nodes), the Sentinel command set, runtime reconfiguration, adding/removing sentinels, the SDOWN/ODOWN mechanism, and VIP switching options.

Published on Mar 12, 2026

Redis Sentinel Source Code Analysis

A deep dive into the Redis Sentinel core implementation: initialization, node discovery (Pub/Sub and INFO), subjective/objective down detection, failover leader election (sentinelVoteLeader / sentinelGetLeader) and the failover state machine (slave selection, promotion, replica reconfiguration), plus the TILT mode and the epoch-based split-brain protection.

Published on Mar 10, 2026

Redis Persistence — The AOF Story

From AOF fundamentals, how to enable it, and file rewriting, to a source-level walkthrough of AOF writing (call→propagate→feedAppendOnlyFile→flushAppendOnlyFile) and startup replay (loadAppendOnlyFile with a fake client).

Published on Mar 5, 2026

Redis Stalls Caused by bgsave

Why a bgsave on a large Redis instance can stall the main process for seconds (fork() copying page tables), plus how to monitor and mitigate it (latest_fork_usec, THP, memory cap, Redis Cluster).

Published on Mar 5, 2026

Sentinel Client Reconfig Script (client-reconfig-script)

How Redis Sentinel invokes client-reconfig-script during a failover: the script arguments, the script execution queue, sentinelRunPendingScripts scheduling, and the two trigger points (hello messages and the INFO command).

Published on Mar 5, 2026

Redis Notes: Features and How They Work

A survey of Redis features and internals—the protocol, the five basic data types, keys and expiration/eviction, multiple databases, persistence, transactions, pipelining, client-side caching, and replication.

Published on Mar 5, 2026

How Redis Implements the Hash Type

Starting from the macroscopic data structures, this article explains the underlying implementation of the Redis Hash type—the dictionary, incremental rehashing, and the conversion between the ziplist and hashtable encodings.

Published on Mar 5, 2026

How Redis Executes a Command

A source-level walkthrough of how Redis executes a command, compared with PostgreSQL, covering the event loop, connection setup, command read/parse, and the RESP protocol.

Published on Mar 5, 2026

Compiling, Installing, and Using Redis

Build Redis from source, walk through common redis.conf settings and basic client usage, and compare Redis with PostgreSQL via the server startup call graph.

Published on Mar 5, 2026

Redis Persistence — The RDB Chapter

A deep dive into Redis RDB persistence — the fork copy-on-write snapshot principle, BGSAVE triggering, the rdbSave/rdbSaveRio write implementation, the RDB file format encoding, and the rdbLoad startup data-recovery source analysis.

Published on Mar 5, 2026

Redis High Availability and Replication

Redis master-replica replication (full vs partial), replicaof setup, the WAIT command, replication config details (repl-*, min-replicas, replica-announce, etc.), plus the run-ID/offset partial-resync mechanism and expired-key handling.

Published on Mar 5, 2026

How Redis Implements the String Type (SDS)

A deep dive into the underlying implementation of the Redis String type — SDS (Simple Dynamic String), embstr encoding, integer encoding, plus memory optimizations like space pre-allocation and lazy freeing, with core C source analysis.

Published on Mar 5, 2026

Introducing RedisShake — the Redis Data Migration Tool

An introduction to RedisShake, Alibaba's open-source Redis data migration/backup tool, covering sync, scan, rdb, and aof readers plus redis and file writers with configuration examples.

Published on Mar 5, 2026

Redis Max Clients

A deep dive into the Redis maxclients setting, OS file-descriptor limits, and how they relate to the maximum connection count

Published on Jan 9, 2026

Redis Protected Mode

How Redis protected-mode works, its relationship with the default user's password, and troubleshooting tips for master-replica replication

Published on Jan 9, 2026

How to Gracefully Shut Down Redis

Two ways to gracefully shut down Redis—the SHUTDOWN command and systemd management—plus source analysis and the Redis 6/7 differences

Published on Jan 9, 2026

Tencent Cloud Redis Overview

An overview of Tencent Cloud Database Redis—product architecture, the Proxy layer, multi-AZ deployment, failover, upgrades, and backup/restore

Published on Jan 9, 2026

How to Configure TLS in Redis

A complete walkthrough of Redis TLS configuration—from enabling the TLS port and certificates to CA signing, replication, and cluster TLS

Published on Jan 9, 2026

Redis Transactions

How Redis transactions work—the MULTI/EXEC/DISCARD/WATCH commands, execution flow, and source-level analysis of the WATCH-based optimistic locking

Published on Jan 9, 2026

Redis Upgrade

How to upgrade or restart a Redis instance without downtime—full steps for replica switchover and rolling Sentinel/Cluster upgrades

Published on Jan 9, 2026

Why Redis Requires vm.overcommit_memory=1

Why Redis sets vm.overcommit_memory=1—to guarantee fork success during background persistence—plus related tuning such as THP

Published on Jan 9, 2026
© 2026 PeekDB— Content licensed under CC BY 4.0CC BY 4.0
PrivacyTermsGitHub