Database perspective and best practice

The main challenge of migrating from monolithic to microservice is dividing the database with a clear-cut separation. As mentioned before, you need a domain expert here. Only a domain expert will have the knowledge about which data normally changes together. With experience, you can also have this knowledge. With this knowledge, divide the database in a bounded context.

To start with, rather than actual separation, one can start with database views. By doing this, one can ensure that any separation is working fine. For a new microservice that is separated out, you can read/write from these views. You might feel some performance issues, but this is acceptable until you are in a trial period. Another way of doing it would be to migrate data from the core database to a new database used by the new microservice. This data transfer can be done in two different ways:

  • Trigger
  • Event sourcing

You can implement the trigger in the core database, and as data is modified in your database and its respective tables, you can send data to a new database, or you can establish a database link to do this. Another non-traditional method is using events. In event sourcing, there are tools that record the event in your current database and replay this event in your new database. Debezium is one of the many open source tools that records the event and can play it on a newly created microservice.

During database migration from a monolithic to a microservice architecture, keep in mind backward compatibility as well as forward compatibility. You should not break something that's already running. You should make sure that everything is set up so that nothing can be shut down in one shot. If you want to delete a column from a database, rename it first and release it. If everything is correctly set up, then you can remove it from the next release. Any migration happening on a microservices database should not be manual. The proper tools should be used for database migration; FlyBase and Liquibase are two such tools that will help you to upgrade your database automatically. Both are supported by many languages, and they also have Maven plugins.

This changeover cannot happen in one day. It is a continuous process until you have completely moved to a microservice architecture, so until this process has finished, try to avoid writing a destructive statement in the database. You can lose data by doing this.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset