|
| 1 | +--- |
| 2 | +title: "CNPG Recipe 16 - Balancing Data Durability and Self-Healing with Synchronous Replication" |
| 3 | +date: 2024-12-26T10:57:00+01:00 |
| 4 | +description: "Explore how CloudNativePG 1.25 introduces data durability control to strike the right balance between consistency and availability in PostgreSQL clusters." |
| 5 | +tags: ["postgresql", "postgres", "kubernetes", "k8s", "cloudnativepg", "cnpg", "postgresql", "postgres", "dok", "data on kubernetes"] |
| 6 | +cover: cover.jpg |
| 7 | +thumb: thumb.jpg |
| 8 | +draft: false |
| 9 | +--- |
| 10 | + |
| 11 | +_CloudNativePG 1.25 enhances control of PostgreSQL synchronous replication with |
| 12 | +a new dataDurability option, allowing you to choose between prioritising data |
| 13 | +consistency or self-healing capabilities. This article explains the feature, |
| 14 | +contrasts it with previous approaches, and provides guidance on migrating to |
| 15 | +the new API format._ |
| 16 | + |
| 17 | +<!--more--> |
| 18 | + |
| 19 | +--- |
| 20 | + |
| 21 | +In ["CNPG Recipe 13 - Configuring PostgreSQL Synchronous Replication"]({{< relref "../20240910-syncrep/index.md" >}}), |
| 22 | +I provided a comprehensive overview of how PostgreSQL synchronous replication |
| 23 | +is implemented in CloudNativePG. This article builds on that foundation by |
| 24 | +introducing a new feature available in [CloudNativePG 1.25](https://cloudnative-pg.io/releases/cloudnative-pg-1-25.0-released/): |
| 25 | +**data durability control** for internal synchronous replication within a |
| 26 | +PostgreSQL cluster. |
| 27 | + |
| 28 | +## Addressing the Missing Piece in 1.24 |
| 29 | + |
| 30 | +As detailed in ["CNPG Recipe 13"]({{< relref "../20240910-syncrep/index.md" >}}), |
| 31 | +CloudNativePG 1.24 introduced a new API for managing synchronous replication, |
| 32 | +marking the beginning of the |
| 33 | +[deprecation process for the previous method based on `minSyncReplicas` and `maxSyncReplicas`](https://cloudnative-pg.io/documentation/current/replication/#synchronous-replication-deprecated). |
| 34 | + |
| 35 | +However, a significant difference existed between the old and new methods in |
| 36 | +terms of data durability: |
| 37 | + |
| 38 | +- The previous approach, based on `minSyncReplicas` and `maxSyncReplicas`, |
| 39 | + prioritised self-healing over data durability. If no replicas were available, |
| 40 | + CloudNativePG would automatically disable synchronous replication to maintain |
| 41 | + availability. |
| 42 | +- In contrast, the new API prioritised data durability. Write operations would |
| 43 | + hang if no synchronous replica was available, ensuring consistency but |
| 44 | + potentially impacting availability. |
| 45 | + |
| 46 | +From my discussions with CloudNativePG users, the first option typically |
| 47 | +appeals to platform engineers focused on achieving large-scale self-healing and |
| 48 | +high availability, while the second aligns with the expectations of synchronous |
| 49 | +replication from a DBA's perspective. |
| 50 | + |
| 51 | +CloudNativePG 1.24 lacked an option to balance these two approaches, leaving |
| 52 | +users unable to choose between prioritising data durability or self-healing. |
| 53 | + |
| 54 | +This limitation also hindered migration to the new synchronous replication API |
| 55 | +for users who relied on self-healing capabilities. |
| 56 | + |
| 57 | +## The `dataDurability` Option in CloudNativePG 1.25 |
| 58 | + |
| 59 | +CloudNativePG 1.25 introduces the `dataDurability` option in the |
| 60 | +`.spec.postgresql.synchronous` stanza, enabling users to configure the |
| 61 | +durability mode for synchronous replication. |
| 62 | + |
| 63 | +This option allows a choice between two modes: |
| 64 | + |
| 65 | +- `required` (*default*): Ensures strict data consistency and durability, |
| 66 | + aligning with PostgreSQL's behaviour. This mode maintains compatibility with |
| 67 | + the behaviour in CloudNativePG 1.24. |
| 68 | +- `preferred`: Focuses on self-healing and high availability by automatically |
| 69 | + disabling synchronous replication when no replicas are available in the |
| 70 | + cluster. This mode bridges the gap between the new API for synchronous |
| 71 | + replication control and the previous approach based on `minSyncReplicas` and |
| 72 | + `maxSyncReplicas`. |
| 73 | + |
| 74 | +## Migrating to the New Format |
| 75 | + |
| 76 | +After upgrading to CloudNativePG 1.25, it is recommended to update your |
| 77 | +cluster's configuration for synchronous replication based on `minSyncReplicas` |
| 78 | +and `maxSyncReplicas` to the new API format based on the |
| 79 | +`postgresql.synchronous` stanza. |
| 80 | + |
| 81 | +For example, you can safely replace the following configuration: |
| 82 | + |
| 83 | +```yaml |
| 84 | + # <snip> |
| 85 | + minSyncReplicas: 1 |
| 86 | + maxSyncReplicas: 1 |
| 87 | + # <snip> |
| 88 | +``` |
| 89 | + |
| 90 | +With the updated format: |
| 91 | + |
| 92 | +```yaml |
| 93 | + # <snip> |
| 94 | + postgresql: |
| 95 | + synchronous: |
| 96 | + method: any |
| 97 | + number: 1 |
| 98 | + dataDurability: preferred |
| 99 | + # <snip> |
| 100 | +``` |
| 101 | + |
| 102 | +This configuration maintains the same behaviour as the previous approach using |
| 103 | +`minSyncReplicas` and `maxSyncReplicas`, while also prioritising self-healing. |
| 104 | + |
| 105 | +To shift the focus to data consistency, simply update the configuration to use |
| 106 | +`dataDurability: required` in the snippet above. This allows you to align with |
| 107 | +stricter durability requirements as needed from a DBA standpoint. |
| 108 | + |
| 109 | +Upgrade today to CloudNativePG and enjoy this new feature! |
| 110 | + |
| 111 | +--- |
| 112 | + |
| 113 | +Stay tuned for the upcoming recipes! For the latest updates, consider |
| 114 | +subscribing to my [LinkedIn](https://www.linkedin.com/in/gbartolini/) and |
| 115 | +[Twitter](https://twitter.com/_GBartolini_) channels. |
| 116 | + |
| 117 | +If you found this article informative, feel free to share it within your |
| 118 | +network on social media using the provided links below. Your support is |
| 119 | +immensely appreciated! |
| 120 | + |
| 121 | +_Cover Picture: [“Elephants“ (public domain)](https://www.needpix.com/photo/66726/)._ |
| 122 | + |
0 commit comments