Udemy
Turn what you know into an opportunity and reach millions around the world.
Learn More
Your cart is empty.
Keep shopping
Postgres Sharding Hands-On: Docker, Python, PgBouncer, Citus
New
2 students

Postgres Sharding Hands-On: Docker, Python, PgBouncer, Citus

Shard a PostgreSQL database yourself: shard key, Python router, PgBouncer and Citus, measured live. Interview-ready
Created byRoni Das
Last updated 10/2026
English

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

Course content

6 sections • 16 lectures • 2h 34m total length
  • What sharding is, and what we will build5:42
  • How enterprises run a database6:06

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