Skip to content

Data Persistence Externalization Principle

Data Persistence Externalization Principle is a software design and architectural practice that decouples an application's state from its runtime processes by storing data in external systems rather than local files or in-memory structures^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md].

By maintaining state outside of the application, the system ensures that data survives application restarts or crashes^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md].

Core Concepts

The primary motivation for this principle is data durability and reliability^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md]. When an application maintains its own state internally or via local file systems, a crash or failure can lead to significant data loss. Externalizing persistence shifts the responsibility of storage to dedicated systems (like databases) designed for durability and consistency^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md].

Furthermore, this approach facilitates scalability and concurrency. Relying on local storage can become problematic if multiple instances of a service need to write to the same data source simultaneously^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md]. External stores often provide mechanisms to handle concurrent access and data synchronization more effectively than local file locks.

Implementation

Implementing this principle typically involves replacing local file I/O operations with calls to an external database or cache.

For example, a Go application might initially save data to a local videos.json file^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md]. To apply the externalization principle, the implementation is modified to connect to a Redis cluster:

  1. Client Connection: Establish a connection to the external store using a client library (e.g., redis.NewFailoverClient in Go)^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md].
  2. Data Write (Externalization): Instead of writing JSON to a file, the application serializes the data and sends it to the external store (e.g., redisClient.Set)^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md].
  3. Data Read: The application retrieves data by querying the external store (e.g., redisClient.Get) rather than reading a local file^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md].

This transformation moves the data lifecycle out of the application's direct control and into a managed environment.

  • [[23种经典设计模式]]: Patterns such as the Data Access Object (DAO) can be used to abstract the details of the external persistence mechanism.
  • [[流程化筆記]]: When implementing this principle, documenting the migration process or the setup of external databases can be treated as a workflow to ensure steps are not missed.

Sources

  • 400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md