From 9063b01c15ca9d97e639748a7b1273a743b409c6 Mon Sep 17 00:00:00 2001 From: vaheminasyan2 Date: Wed, 21 Apr 2021 16:28:29 +0000 Subject: [PATCH] [Build and Deploy Staging Site] Publish from microsoft/azuretipsandtricks-private:main/src/blog --- blog/tip313.md | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 blog/tip313.md diff --git a/blog/tip313.md b/blog/tip313.md new file mode 100644 index 000000000..12c9cabf1 --- /dev/null +++ b/blog/tip313.md @@ -0,0 +1,92 @@ +--- +type: post +title: "Tip 313 - How to develop apps with Azure Database for PostgreSQL using best practices" +excerpt: "Learn how to develop apps with Azure Database for PostgreSQL using best practices" +tags: [Databases] +share: true +date: 2021-04-21 12:00:00 +--- + +::: tip + +:fire: Checkout the Azure Developer page at [azure.com/developer](https://azure.com/developer?WT.mc_id=azure-azuredevtips-azureappsdev). + +:bulb: Learn more : [Azure PostgreSQL Databases](https://azure.microsoft.com/services/postgresql/?WT.mc_id=azure-azuredevtips-azureappsdev). + +:tv: Watch the video : [How to develop apps with Azure Database for PostgreSQL using best practices](https://youtu.be/cjBlWWT2_Sk?WT.mc_id=youtube-azuredevtips-azureappsdev). + +::: + +### How to develop apps with Azure Database for PostgreSQL using best practices + +#### Managed PostgreSQL provides availability, performance, elasticity and security +PostgreSQL is a great relational database with lots of features. [Azure Database for PostgreSQL](https://docs.microsoft.com/azure/postgresql/overview?WT.mc_id=docs-azuredevtips-azureappsdev) is based on [PostgreSQL Community Edition](https://www.postgresql.org/?WT.mc_id=other-azuredevtips-azureappsdev) and offers a managed version of PostgreSQL, which enables elastic scaling, high availability, data protection and more. + +You can create incredible applications with Azure Database for PostgreSQL and when you do, it is important to follow [best practices](https://docs.microsoft.com/azure/postgresql/application-best-practices#configuration-of-application-and-database-resources?WT.mc_id=docs-azuredevtips-azureappsdev) to optimize security, availability and performance. This post discusses some of the best practices for building an application with Azure Database for PostgreSQL. + +#### Prerequisites +If you want to follow along, you'll need the following: +* An Azure subscription (If you don't have an Azure subscription, create a [free account](https://azure.microsoft.com/free/?WT.mc_id=azure-azuredevtips-azureappsdev) before you begin) + +#### Keep your PostgreSQL server secure +The first best practice is about security. It is important to follow the latest security guidelines to keep your application safe. In Azure Database for PostgreSQL, you can configure security by: + +* Enabling the PostgreSQL [server firewall](https://docs.microsoft.com/azure/postgresql/concepts-firewall-rules?WT.mc_id=docs-azuredevtips-azureappsdev) +* Run the PostgreSQL server and your application in a [virtual network](https://docs.microsoft.com/azure/postgresql/concepts-data-access-and-security-vnet?WT.mc_id=docs-azuredevtips-azureappsdev) +* Use [Azure Private Link](https://docs.microsoft.com/azure/postgresql/concepts-data-access-and-security-private-link?WT.mc_id=docs-azuredevtips-azureappsdev) to create a secure link between your application and the PostgreSQL server +* Set the [minimum TLS version](https://docs.microsoft.com/azure/postgresql/concepts-ssl-connection-security?WT.mc_id=docs-azuredevtips-azureappsdev) of the PostgreSQL server to 1.2 + +You can configure security in the Azure Database for PostgreSQL database **Connection security blade**. + + + +(Configure PostgreSQL Connection Security) + +#### Use environment variables for connection information +When you connect to Azure Database for PostgreSQL from an application, you shouldn't store the connection string (including username and password) in your application code. It is best practice to use environment variables for the connection string. If your application runs in an [Azure App Service Web App](https://azure.microsoft.com/services/app-service/web/?WT.mc_id=azure-azuredevtips-azureappsdev), you can have the application read from the environment variables, which you can configure in the **Configuration** menu of the App Service. In there, you can configure connection strings and application settings, without storing them in your application code. + +You can even take it a step further and [store connection string data in Azure Key Vault](https://microsoft.github.io/AzureTipsAndTricks/blog/tip271.html?WT.mc_id=github-azuredevtips-azureappsdev) and have the application retrieve it from there. + + + +(Configure connection strings in Azure App Service) + +#### Enable read replication to mitigate failovers and enhance performance +If your database server fails for whatever reason, your application becomes unusable. To prevent this from happening, you can increase your application availability, by [creating read replicas of your Azure Database for PostgreSQL](https://docs.microsoft.com/azure/postgresql/concepts-read-replicas?WT.mc_id=docs-azuredevtips-azureappsdev). Read replicas are Azure Database for PostgreSQL databases in another geographic region that will have all the data of the original database synchronized to them. When your original database is unavailable, you can change your application to use the replica database and read data from it. You can also upgrade the replica to become a read/write database, so that your application can also write to it, whilst your primary database is still offline. + +Additionally, you can use read replicas to enhance geographic performance. You can create read-only replicas in geographic regions where your users are active and where you also deploy another instance of your application. By doing this, the data is close to your users, which improves performance. + +You can add replicas in the Azure Database for PostgreSQL database **Replication blade**. + + + +(Azure Database for PostgreSQL replicas) + +#### Use autovacuum +By default, when PostgreSQL performs a delete operation, the records are marked for deletion and will be purged later. PostgreSQL purges these records when it performs a vacuum job. If you don't perform vacuum jobs on a regular basis, your database will grow and slow down because of data bloat, suboptimal indexes and increased I/O. + +Luckily, you can enable [autovacuum](https://docs.microsoft.com/azure/postgresql/howto-optimize-autovacuum?WT.mc_id=docs-azuredevtips-azureappsdev) in Azure Database for PostgreSQL. Autovacuum performs the vacuum job regularly, which cleans up your database and keeps it performant. + +To enable the autovacuum feature, navigate to the **Server parameters** blade of the Azure Database for PostgreSQL database and find and turn **autovacuum** to **ON**. + + + +(Azure Database for PostgreSQL autovacuum setting) + +#### Monitor performance with the Query Store +Most managed databases in Azure enable you to monitor and analyze query statistics. This enables you to troubleshoot which queries are fast and which ones are slow and which queries are executed the most. To enable this feature for Azure Database for PostgreSQL, you need to enable the [Query Store](https://docs.microsoft.com/en-us/azure/postgresql/concepts-query-store?WT.mc_id=docs-azuredevtips-azureappsdev) feature, which is disabled by default. + +You can enable the Query Store feature in the **Server parameters** blade of the Azure Database for PostgreSQL database. In there, you need to set the **pg_qs.query_capture_mode** parameter to **ALL** and save the changes. + + + +(Enable the Query Store feature) + +After 20 minutes or so, query statistics will show up in the **Query Performance Insight** blade of the database. + + + +(The Query Performance Insight blade) + +#### Conclusion +[Azure Database for PostgreSQL](https://docs.microsoft.com/azure/postgresql/overview?WT.mc_id=docs-azuredevtips-azureappsdev) provides a scalable, highly available, secure and performant version of PostgreSQL. And when you use [best practices](https://docs.microsoft.com/azure/postgresql/application-best-practices#configuration-of-application-and-database-resources?WT.mc_id=docs-azuredevtips-azureappsdev), like optimized [server security](https://docs.microsoft.com/azure/postgresql/concepts-security?WT.mc_id=docs-azuredevtips-azureappsdev), [not storing connection strings in code](https://docs.microsoft.com/azure/app-service/configure-common#configure-app-settings?WT.mc_id=docs-azuredevtips-azureappsdev), use [read replications](https://docs.microsoft.com/azure/postgresql/concepts-read-replicas?WT.mc_id=docs-azuredevtips-azureappsdev), enable [autovacuum](https://docs.microsoft.com/azure/postgresql/howto-optimize-autovacuum?WT.mc_id=docs-azuredevtips-azureappsdev), and use the [Query Store](https://docs.microsoft.com/azure/postgresql/concepts-query-store?WT.mc_id=docs-azuredevtips-azureappsdev) feature, you make sure that your application will perform at its best and will stay available and secure. Go and check it out! \ No newline at end of file