官术网_书友最值得收藏!

Migrating data in production

Monolith code bases usually use a primary relational database for persistence. Modern web frameworks are often packaged with object-relational mapping (ORM), which allows you to define your domain objects using classes that correspond to tables in the database. Instances of these model classes correspond to rows in the table. As monolith code bases grow, it's not uncommon to see additional data stores, such as document or key value stores, be added. 

Microservices should not share access with the same database your monolith connects to. Doing so will inevitably cause problems when trying to coordinate data migrations, such as schema changes. Even schema-less stores will cause problems when you change the way data is written in one code base but not how data is read in another code base. For this and other reasons, it's best to have microservices fully manage the data stores they use for persistence.

When transitioning from a monolith to microservices, it's important to have a strategy for how to migrate data. All too often, a team will extract the code for a microservice and leave the data, setting themselves up for future pain. In addition to difficulty managing migrations, a failure in the monolith relational database will now have cascading impacts on services, leading to difficult-to-debug production incidents. 

One popular technique for managing large-scale data migrations is to set up dual writing. When your new service is deployed, you'll have two write pathsone from the original monolith code base to its database and one from your new service to its own data store. Make sure that writes go to both of these code paths. You'll now be replicating data from the moment your new service goes into production, allowing you to backfill older data using a script or a similar offline task. Once data is being written to both data stores, you can now modify all of your various read paths. Wherever the code is used to query the monolith database directly, replace the query with a call to your new service. Once all read paths have been modified, remove any write paths that are still writing to the old location. Now you can delete the old data (you have backups, right?). 

主站蜘蛛池模板: 织金县| 陕西省| 博客| 营山县| 皋兰县| 洪雅县| 武汉市| 海晏县| 丽江市| 克东县| 江川县| 金寨县| 三河市| 麟游县| 山东省| 石嘴山市| 铅山县| 峨边| 衡南县| 麻城市| 香格里拉县| 永新县| 清流县| 长汀县| 罗江县| 丰原市| 阿拉尔市| 芮城县| 象山县| 阳朔县| 仪陇县| 连州市| 防城港市| 萨迦县| 丰宁| 四川省| 邯郸县| 林甸县| 太仆寺旗| 桦南县| 天津市|