本课程带你用Docker与Python亲手压垮单机Postgres,再实现分片修复:编写分片路由、用一致性哈希解决扩容迁移,并配置PgBouncer与Citus,掌握用吞吐量和p95延迟支撑架构决策的硬核技能。

原始标题:Postgres Sharding: Complete Hands-On with Docker and Python

Postgres Sharding: Complete Hands-On with Docker and Python

这门中级 PostgreSQL 实战课程聚焦于数据库分片(Sharding)与性能优化,拒绝纯理论架构,主打“在本地手动搞垮并修复数据库”的硬核实战。学员将在本地通过 Docker 搭建虚拟业务环境,模拟 64 台服务器高并发下单,亲历百万用户和 850 万订单压垮单机数据库的完整过程,以此掌握从索引、分区到分片的性能决策边界。

课程的核心价值在于手把手带学员攻克分布式数据库的经典痛点,为系统设计面试提供真实的数据支撑。你将不仅能用 Python 亲手编写分片路由、利用一致性哈希解决扩容迁移难题,还能深度配置 PgBouncer 连接池与 Citus 分布式扩展,最终学会在面试中用具体吞吐量和 p95 延迟等“大厂级”硬核指标,完美回答“如何对订单数据库进行分片”的架构难题。

Published 10/2026
Created by Roni Das
MP4 | Video: h264, 1920×1080 | Audio: AAC, 44.1 KHz, 2 Ch
Level: Intermediate | Genre: eLearning | Language: English | Duration: 16 Lectures ( 2h 34m ) | Size: 1.2 GB

Build an orders database, load it until it breaks, shard it yourself, add PgBouncer and Citus, and measure every step

What you’ll learn
⚡ Decide when a slow PostgreSQL database needs sharding, and when an index, a cache, a replica or partitioning is enough
⚡ Choose a shard key from real queries, and measure how evenly it spreads the data
⚡ Write a shard router in Python and move a million customers and 8.5 million orders into four shards
⚡ Load test a database with 64 concurrent checkout servers and read throughput and p95 latency correctly
⚡ Fix the classic sharding problems: fan-out queries, hot rows, connection limits and adding a shard
⚡ Put PgBouncer in front of each shard in transaction mode, and know what it costs
⚡ Use consistent hashing and logical buckets so adding a shard moves about a fifth of the data, not four fifths
⚡ Shard PostgreSQL with Citus: distributed tables, co-location, Task Count and live rebalancing
⚡ Answer “how would you shard an orders database?” in a system design interview, with real numbers

Requirements
❗ A laptop with Docker Desktop installed and running (Mac, or Windows with WSL 2), about 8 GB of memory for Docker and 10 GB of free disk
❗ VS Code, or any code editor you like
❗ Basic comfort with a terminal. You do not need to install Python: every file runs inside a container
❗ Some SQL helps, but every statement is explained as we write it

Description
Most sharding explanations stop at a diagram. In this course you build the whole thing on your own laptop and watch the numbers change.

We start with one PostgreSQL database for a made-up grocery delivery company called KiranaLoop. Sixty-four checkout servers place orders at the same time, and we measure it every second. With a million customers and 8.4 million orders, one second drops to 308 orders. That slow second is where the course really begins.

Before we shard anything, we try the cheaper steps first: a read replica, then partitioning by month. You will see what each one fixes, and what it cannot fix. Then we choose a shard key from the queries the company actually runs, split the data across four PostgreSQL shards with a router we write ourselves, and measure again: 32,472 orders per second, about five times the single database.

Then we break it on purpose, four different ways, and fix each one

✨ a dashboard that has to ask every shard (from 660 ms down to 6.8 ms)

✨ a 9 pm flash sale where everyone wants the same row (50,000 sold, zero oversold)

✨ too many connections (28 of 128 servers turned away, then all 128 served through PgBouncer)

✨ adding a fifth shard (80% of customers move with the simple rule, about 20% with a ring or buckets)

Finally, we do it the way many companies do, with Citus. One line replaces our router, and we watch Citus move shards onto a new machine while the database keeps running.

Every line of code is typed live and explained line by line, with a diagram next to the code for the tricky parts. Every number you see was printed by a real terminal. On your machine the numbers will be different, but the shape will be the same.

The full code is in the course resources, so you can run exactly what I run. Each lecture also comes with a PDF slide deck: the diagrams, the commands, the measured numbers and a recap.

Who this course is for
⭐ Backend developers whose PostgreSQL database is starting to struggle, and who want to know their options before reaching for sharding
⭐ Engineers preparing for system design interviews who want to talk about sharding from real experience, not slides
⭐ Developers who have read about sharding and consistent hashing, and want to see it working on their own machine
⭐ Team leads and architects who need to judge whether sharding is worth its cost for their system

隐藏内容

此处内容需要权限查看

  • 普通3金币
  • 会员免费
  • 永久会员免费推荐
会员免费查看

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注