Advanced PostgreSQL: Indexing, Partitioning, Replication · Aula

Replicação em cascata

Implemente a replicação em cascata para distribuir a carga de um servidor primário, fazendo com que servidores secundários repliquem dados de outros servidores secundários.

Aula 1 de 411 etapas

Replicação em cascata é uma aula grátis de Advanced PostgreSQL: Indexing, Partitioning, Replication no CoddyKit. Esta é a aula 1 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Advanced PostgreSQL: Indexing, Partitioning, Replication, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Advanced PostgreSQL: Indexing, Partitioning, Replication inclui 4 aulas no total.

Partes desta aula ainda não foram traduzidas e aparecem em inglês.

What is Cascading Replication?

Welcome to Cascading Replication! In typical PostgreSQL replication, all standby servers connect directly to the primary server.

However, for larger setups or when you need many read replicas, this can put a lot of load on the primary. Cascading replication offers a solution.

It means a standby server can itself act as a primary for other standbys, forming a chain.

How the Replication Chain Works

Imagine a chain: The primary server sends its Write-Ahead Log (WAL) to a first standby (let's call it Standby A).

Then, Standby A, instead of just consuming the WAL, also acts as a source, sending that same WAL data to a second standby (Standby B).

This creates a flow: Primary → Standby A → Standby B. Standby A is an 'intermediate' standby.

Key Benefits of Cascading

Why go through the trouble of chaining standbys? Cascading replication offers several advantages:

  • Reduced Load on Primary: The primary only needs to send WAL to Standby A, not to all standbys directly.
  • Scalability for Reads: You can create many read replicas without overwhelming the primary's network or I/O.
  • Geographical Distribution: Standbys can be spread across different data centers, with intermediate standbys bridging the distance.

Preparing the First Standby (Standby A)

The first step is to set up Standby A as a regular streaming replication standby. This involves taking a base backup from the primary and configuring Standby A to connect to the primary.

The key here is that Standby A must also be configured to send WAL files to other standbys, not just receive them.

Configuring Standby A to be a Source

For Standby A to act as a source for other standbys, you need to adjust its PostgreSQL configuration (`postgresql.conf`).

The important parameters are wal_level and max_wal_senders. hot_standby should also be on to allow queries.

wal_level = replica
max_wal_senders = 5 -- Or more, for primary and other standbys
hot_standby = on

Standby B: Connecting to Standby A

Now, for Standby B, instead of pointing its primary_conninfo to the actual primary server, you point it to Standby A.

This tells Standby B to receive its WAL stream from Standby A.

primary_conninfo = 'host=standby_a_ip port=5432 user=replication_user password=YOUR_PASSWORD'
primary_slot_name = 'standby_b_slot' -- Optional, for guaranteed WAL
hot_standby = on

Initializing Standby B's Base

Just like any new standby, Standby B needs an initial copy of the data. You can take this base backup directly from the primary, or, more commonly in a cascading setup, you can take it from Standby A.

Using Standby A for the base backup further reduces load on the primary.

# On Standby B, after stopping PostgreSQL
pg_basebackup -h standby_a_ip -p 5432 -U replication_user -D /var/lib/postgresql/data -F p -Xs -P -R

Monitoring the Cascade

It's crucial to monitor the health of your cascading setup. The pg_stat_replication view is your best friend.

On the primary, you'll see Standby A connected. On Standby A, you'll see Standby B connected.

-- On Standby A (or Primary):
SELECT application_name, client_addr, state, sync_state FROM pg_stat_replication;

Practical Use Cases

Cascading replication shines in scenarios where:

  • You have a high number of read queries and need many read replicas.
  • You want to offload backup operations to a standby, but also need other standbys for reads.
  • You are distributing your database across multiple regions and want to minimize cross-region traffic from the primary.
  • You're building complex disaster recovery tiers.

Cascading Replication Quiz

Let's test your understanding of cascading replication.

Recap: Cascading Replication

In this lesson, we explored cascading replication, a powerful architecture for scaling PostgreSQL read replicas and distributing replication load.

You learned how standbys can form a chain, with one standby replicating from another, and the key configurations and benefits, such as reduced primary load and enhanced read scalability.

This technique is essential for robust, distributed PostgreSQL environments.

Grátis para começar

Aprenda Advanced PostgreSQL: Indexing, Partitioning, Replication com um tutor de IA — grátis

Escreva e execute código real no seu navegador, obtenha ajuda instantânea de um tutor de IA 24/7 e continue de onde parou na web ou no app.

Cursos
11
Aulas
44

Perguntas Frequentes

A aula “Replicação em cascata” é grátis?

Sim — o texto completo de “Replicação em cascata” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Advanced PostgreSQL: Indexing, Partitioning, Replication, atualize para CoddyKit PRO. O curso de Advanced PostgreSQL: Indexing, Partitioning, Replication inclui 4 aulas no total.

O que vou aprender em “Replicação em cascata”?

Implemente a replicação em cascata para distribuir a carga de um servidor primário, fazendo com que servidores secundários repliquem dados de outros servidores secundários. Você pratica Advanced PostgreSQL: Indexing, Partitioning, Replication com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.

Preciso ter experiência prévia para começar Advanced PostgreSQL: Indexing, Partitioning, Replication?

Nenhuma experiência prévia é necessária. Advanced PostgreSQL: Indexing, Partitioning, Replication no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 1 de 4.

Quanto tempo leva a aula “Replicação em cascata”?

A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.

Posso escrever e executar código nesta aula de Advanced PostgreSQL: Indexing, Partitioning, Replication?

Sim. Cada aula de Advanced PostgreSQL: Indexing, Partitioning, Replication inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.

Todas as aulas deste curso

  1. Replicação em cascata
  2. Replicação multimestre (BDR)
  3. Slots de replicação e gerenciamento de WAL
  4. Decodificação lógica e captura de alterações de dados
← Voltar para Advanced PostgreSQL: Indexing, Partitioning, Replication