Replicación síncrona frente a asíncrona
Comprenda las ventajas y desventajas de durabilidad y latencia entre la replicación física síncrona y asíncrona en PostgreSQL.
Replicación síncrona frente a asíncrona es una lección gratuita de Advanced PostgreSQL: Indexing, Partitioning, Replication en CoddyKit. Esta es la lección 4 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Advanced PostgreSQL: Indexing, Partitioning, Replication, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Advanced PostgreSQL: Indexing, Partitioning, Replication incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en inglés.
Two Modes of Streaming
Physical streaming replication can run in two modes: asynchronous (the default) and synchronous. They differ in when the primary considers a commit complete.
Asynchronous Replication
In async mode the primary commits as soon as the WAL is flushed locally. It does not wait for the standby. This is fast but a crash can lose the most recent commits not yet shipped.
Synchronous Replication
In sync mode the primary waits until at least one standby confirms it received (or applied) the WAL before reporting commit success. This guarantees zero data loss for confirmed commits at the cost of extra latency.
Configuring Sync Standbys
Name your standbys with application_name and list them in synchronous_standby_names on the primary.
-- on the primary, in postgresql.conf
synchronous_standby_names = 'standby1, standby2';Quorum-based Sync
You can require any N of several standbys to confirm, balancing durability and availability.
synchronous_standby_names = 'ANY 1 (standby1, standby2)';synchronous_commit Levels
The synchronous_commit setting controls how far a commit must travel:
off/local— local onlyremote_write— standby received WALon— standby flushed WAL to diskremote_apply— standby applied WAL (visible on replica)
Per-transaction Control
This setting can be changed per transaction, letting unimportant writes go fast while critical writes wait for confirmation.
SET synchronous_commit = 'remote_apply';
INSERT INTO payments (amount) VALUES (100);Availability Risk of Sync
If the only synchronous standby goes down, commits on the primary will block until a standby returns. Always pair sync replication with multiple candidates or quorum settings.
Checking Replication State
Inspect the live state from the primary using pg_stat_replication.
SELECT application_name, state, sync_state
FROM pg_stat_replication;Choosing a Mode
Use async for throughput and geographically distant replicas. Use sync when losing even one committed transaction is unacceptable, such as financial ledgers.
Latency Reality
Synchronous commit latency is roughly the network round-trip to the standby plus its fsync time. Keeping sync standbys on a low-latency link is essential.
Quick Check
What is the key trade-off of synchronous replication?
Recap
You compared asynchronous and synchronous replication, configured synchronous_standby_names and synchronous_commit levels, and learned the durability-vs-latency-vs-availability trade-offs.
Preguntas frecuentes
¿La lección «Replicación síncrona frente a asíncrona» es gratis?
Sí — el texto completo de «Replicación síncrona frente a asíncrona» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Advanced PostgreSQL: Indexing, Partitioning, Replication, actualiza a CoddyKit PRO. El curso de Advanced PostgreSQL: Indexing, Partitioning, Replication incluye 4 lecciones en total.
¿Qué aprenderé en «Replicación síncrona frente a asíncrona»?
Comprenda las ventajas y desventajas de durabilidad y latencia entre la replicación física síncrona y asíncrona en PostgreSQL. Practicas Advanced PostgreSQL: Indexing, Partitioning, Replication con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar Advanced PostgreSQL: Indexing, Partitioning, Replication?
No se requiere experiencia previa. Advanced PostgreSQL: Indexing, Partitioning, Replication en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 4 de 4.
¿Cuánto tiempo toma la lección «Replicación síncrona frente a asíncrona»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de Advanced PostgreSQL: Indexing, Partitioning, Replication?
Sí. Cada lección de Advanced PostgreSQL: Indexing, Partitioning, Replication incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- Conceptos de replicación
- Replicación física (streaming)
- Configuración de un servidor en espera
- Replicación síncrona frente a asíncrona