# 4.3 The Town Crier (Logical Replication) ![The Town Crier](assets/arch_logical_replication_crier.png) So far, we’ve talked about a single elephant in a single depot. But what if the depot is too small? What if you need an elephant in New York to know exactly what the elephant at the main branch in London is doing? ## The Crier's Scroll Postgres has a way of sharing its Pocket Diary with the world. This is called **Logical Replication**. Instead of just keeping his notes to himself, the London elephant becomes a **Publication**. He hires a **Town Crier** to stand on the roof of the depot. ```sql -- Making the Cafe's menu public CREATE PUBLICATION cafe_news FOR TABLE dishes, drinks; ``` Every time a new scribble is committed to the diary—a new jam order or a tea-party invite—the Crier shouts it out across the ocean. "Hear ye! Hear ye! Mrs. Higgins bought a teapot!" ## The Listener's Notebook (Subscription) In New York, another elephant is acting as a **Subscription**. He has a dedicated worker sitting on his roof with a telescope, listening for the London Crier. ```sql -- New York branch subscribing to the London menu CREATE SUBSCRIPTION ny_branch_sync CONNECTION 'host=london-cafe port=5432 user=replicator password=peanuts dbname=elephant_cafe' PUBLICATION cafe_news; ``` Every time the worker hears a change—_"London just added a new customer!"_—he writes it down and hands it to the New York elephant, who immediately performs the same action in his own depot. It’s like a worldwide game of Telephone, but one where everyone actually listens! ## Why Shouting is Better than Shipping Before the Town Crier, if you wanted the New York depot to match London, you had to ship the **entire filing cabinet** (Physical Replication) over the ocean every time. It was heavy, slow, and you couldn't change anything in New York without breaking the connection. It was like trying to mail a mountain in a very small envelope. With **Publications and Subscriptions**, you are only shipping the _meaning_ of the changes. New York can have a completely different filing cabinet layout, or even run a different version of Postgres, and it can still keep up with the news from London. ## The Bookmark (Replication Slots) But what if the New York worker falls asleep? Or what if a storm blocks the telescope? The London elephant is very considerate. He doesn't want the New York elephant to miss any news! So, he uses a **Replication Slot**—basically a **Heavy Metal Bookmark** that he clips onto the page in his Pocket Diary where New York last stopped reading. Even if New York is offline for hours, the London elephant **refuses to throw away his old diary pages** (WAL files) until he knows New York has seen them. He just keeps hold of that bookmark until the telescope worker wakes up and catches up. ## The Cost of Shouting (Physical Resources) Even the Town Crier needs to be paid in peanuts! Logical replication isn't "free" labor for the elephant; it consumes real resources in both depots. ### On the Source (Publication) * **CPU (Decoding Sweat)**: The London elephant has to hire a **Logical Decoder** worker. This worker spends all day reading the Pocket Diary and translating "binary scribbles" into "human news." This takes extra CPU cycles every time you write data. * **Memory**: The Decoder needs a small "Holding Pen" (Buffer) in memory to organize changes before shouting them. If your transactions are huge, this pen might overflow to Frozen Pantry! * **Disk (The Storage Hazard)**: As we saw with the **Heavy Metal Bookmark** (Replication Slot), if the news isn't delivered, the London elephant's pockets fill with WAL files. If you aren't careful, the Town Crier's backup can crash the entire depot! ### On the Target (Subscription) * **CPU & I/O (The Busy Scribe)**: The New York elephant has to actually *perform* the work London told him about. He’s re-running the inserts and updates, which means he’s sweating just as much as the London elephant did. * **Network (The Messenger)**: Shouting across the ocean takes bandwidth. If you're replicating a billion-row table, your "telescope worker" is going to be very, very busy. > [!TIP] > Always monitor your **[[Chapter 6/6.0 - The Waiting Game|Wait Events]]** on the subscriber! If you see `WalReceiverWait`, your network might be too slow for the news. > [!WARNING] > If the New York branch stays offline too long, the London elephant's pocket will get stuffed with millions of old diary pages! A "Stale Slot" can eventually fill up the entire depot's storage (the Frozen Pantry). Remember to check your bookmarks: ```sql -- Checking who is currently reading the news SELECT slot_name, active, restart_lsn FROM pg_replication_slots; ``` This "Logical" connection is the secret sauce for moving data between different systems, upgrading your database without downtime, and eventually building the massive, interconnected networks we'll explore in **[[Chapter 7/7.0 - The Elephant in the Clouds (Distributed Storage)|Chapter 7: The Cloud Scales]]**. --- [[Chapter 4/4.2 - The Recovery Parade (Crash Recovery)|← 4.2 - The Recovery Parade]] | [[Chapter 4/4.0 - Safety Without Sweating (The Write-Ahead Log)|↑ 4.0 - Safety Without Sweating]] | [[Chapter 4/4.4 - The Pinky Swear (Transactions)|4.4 - Transactions →]]