Implementing Zero Downtime Migrations in Empress for Seamless User Experience

Introduction

In the realm of software development, one of the key challenges is to perform system upgrades without disrupting the user experience - an issue addressed by Zero Downtime Migrations in Empress. This feature allows the site to run in read-only mode during the upgrade process, ensuring that static sites and dynamic pages, which only need to read data, can continue to operate seamlessly. This guide will delve into the technical depth of this feature, mirroring the clarity and sophistication of Apple’s style.

Core Functionality and Advantages

Zero Downtime Migration is implemented by either using read-replicas or “read-only transactions” provided by databases. In both cases, writes on your database are disabled until maintenance mode is lifted. This ensures that the user experience remains unaffected, as all read operations continue to function. Meanwhile, any actions that require writing to the database are temporarily disabled and resume once the upgrade is complete.

# Example of disabling write operations during read-only mode
@frappe.whitelist()
def log_visit():
    if frappe.flags.read_only:
        return

    doc = frappe.new_doc("Doc Visit")
    doc.insert()

From a developer’s perspective, the advantages are clear:

  • Seamless User Experience: The website and Desk show persistent read-only warnings during updates, ensuring clear communication with the user. All forms are also made read-only, preventing any data modifications.
  • Reduced Downtime Impact: The primary intention behind this feature is to minimize the impact of downtime, ensuring that your site remains functional for read operations.

Considerations and Limitations

Notably, there are certain caveats to keep in mind:

  • App Compatibility: Not all apps automatically support this feature. Apps not designed to work in read-only mode may behave erratically.
  • Update Glitches: During database schema changes and client-side app updates, occasional glitches may occur. These cannot be fully avoided, but the intent is to minimize their impact.

Implementing Zero Downtime Migrations

Here are the steps to enable Read Only Mode during upgrades:

  1. [Optional/Recommended] - Set up a read replica by following the replication guide.
  2. Edit your site config to enable this feature: bench --site develop set-config allow_reads_during_maintenance 1

If you’re an app developer, you can modify your app to support this feature. For instance, you can use frappe.flags.read_only to disable or modify behaviour in read-only mode.

On the client-side, you can update the UI to indicate that the app is in read-only mode and that write-actions can’t be performed. The website context automatically includes the read_only_mode key that you can use to add warnings or toggle features on your pages conditionally.

<!-- some html page -->
{% if read_only_mode %}
    <div class="alert">Site is running in read only mode. Full functionality will be restored soon.</div>
{% endif %}
<!-- other content -->

Conclusion

Zero Downtime Migrations is a crucial feature in Empress that ensures seamless user experience during site upgrades by enabling read-only mode. It’s worth noting that developers should carefully consider the caveats and make necessary adjustments to their apps to fully embrace this feature. With careful implementation and debugging, this feature significantly contributes to the development and customization of business solutions, minimizing the impact of downtime and ensuring a smooth user experience.