Database migrations can be a complex and risky process, especially when done in production. Any downtime or data loss can have a significant impact on your users and business.
Feature flags can help you mitigate these risks and achieve zero downtime database migrations. By decoupling your database changes from your code changes, feature flag management tools allow you to deploy and test your new database schema and code in a controlled and safe manner.
Just a few months ago, we leveraged our own feature flags for upgrading customers from our First Gen CD product to the Next Gen CD version in order to ensure a smooth transition. Recognizing that some customers wanted management approval before revealing the Next Gen user experience, we employed feature flags to conceal its entry point. Additionally, to achieve parity during the initial upgrades, we enabled certain features specifically for select accounts, without affecting the larger user base. These features addressed either behavioral changes or functionalities present in First Gen but absent in Next Gen. Furthermore, Harness' feature flags played a pivotal role in the migration of Harness users. Through them, we could adjust properties in the Harness MongoDB, porting users over from First Gen to Next Gen. In essence, these feature flags ensured a controlled and secure upgrade for our customers.
How to use feature flags for zero-downtime database migrations
Here is a step-by-step guide on how to use feature flags for zero-downtime database migrations:
Decouple your database changes from your code changes. This means that you should make your database changes independently of your code changes. You can use a tool like Flyway to manage your database migrations.
Deploy the new code behind a feature flag. This means that you should deploy your new code to production, but keep it disabled behind a feature flag.
Add the new database columns. Once you have deployed the new code, you can add the new database columns to your database.
Activate writes into the new database columns. Once the new database columns have been added, you can activate writes into them by enabling the feature flag that controls the new code.
Migrate data into the new database columns. Once writes are enabled to the new database columns, you can start migrating your data into them. You can do this using a variety of methods, such as batch jobs or change data capture (CDC).
Turn off the old database columns. Once you have migrated all of your data into the new database columns, you can turn off the old database columns by disabling the feature flag that controls the old code.
Benefits of using Harness Feature Flags for zero-downtime database migrations
There are several benefits to using Harness feature flags for zero-downtime database migrations:
Reduce downtime and risk. By decoupling your database changes from your code changes and deploying them in a controlled manner, you can reduce downtime and risk.
Increase agility. Feature flags allow you to deploy and test new database changes without having to deploy new code. This can help you increase your agility and responsiveness to market changes.
Improve reliability. By enabling you to test new database changes in production with real data, feature flags can help you improve the reliability of your database migrations.
Leveraging a kill switch. If you get a huge volume of data written to your database it has the potential to impact other customers. Feature flags allow you to opt to discard non-critical data per customer account. Although you don’t want to find yourself in that situation, it's always good to be able to prevent one bad account from breaking others.
Examples of how Harness feature flags can be used for zero-downtime database migrations
Here are some examples of how Harness feature flags can be used for zero-downtime database migrations:
Migrate to a new database schema. You can use feature flags to gradually migrate your data from an old database schema to a new one. This allows you to test the new schema in production with real data before fully cutting over.
Add new database columns. You can use feature flags to gradually enable new database columns. This allows you to test the new columns in production with real data before making them available to all users.
Migrate to a new database platform. You can use feature flags to gradually migrate your data from one database management system to another. This allows you to test the new platform in production with real data before fully cutting over.
Beyond database migrations
Here are some other the key benefits of using Harness Feature Flags:
Automated rollout pipelines: Harness Feature Flags allows you to create automated rollout pipelines that can be used to deploy new features to production in a controlled and predictable manner.
Governance: Harness Feature Flags has the most in-depth governance tool with Harness Policy as Code, powered by Open Policy Agent (OPA), providing a number of capabilities such as role-based access control, approval workflows, and audit logging.
Automated lifecycle management : Harness Feature Flags helps you manage your flag tech-debt by detecting potentially stale or deprecated flags and automatically removing them if needed
Integrated with CI/CD: Since Harness covers a range of software delivery products, you can easily integrate your feature flags into CI/CD as a unified pipeline that helps maintain the flags as part of the software development lifecycle.
Feature flags are a powerful tool that can help you achieve zero-downtime database migrations. By decoupling your database changes from your code changes and deploying them in a controlled manner, Harness feature flags can help you reduce downtime, increase agility, and improve the reliability of your database migrations.
Want to migrate your own database with zero-downtime? Visit our product page or sign up to start using feature flags.