How to add a field to a highly loaded database table

Started by maestro_bah, Sep 18, 2022, 03:31 AM

Previous topic - Next topic

maestro_bahTopic starter

Greetings, everyone!

Currently, we have a database in production that contains over 15 million records.

As part of a task, I am required to append a new field to the existing table with a default value of "5". The project is built on Laravel framework.

I need to execute this task in such a way that it doesn't affect the stability of the project i.e., without causing the project to fail. This needs to be achieved without disabling the project.

So, the question now is - what should the migration process look like? Given that there are about 2 million users, it becomes imperative to come up with a well-structured plan to avoid any issues.
  •  

Kik84

To handle the task of adding a new field to the table, you can opt for "migration".

In case of a short term blocking of the project, which may not be noticed by or may require patience from the users, this approach can be adopted. However, if the number of users runs into millions, such an approach may not be feasible.

You can follow the guidelines mentioned in the official Laravel documentation for migrations: https://laravel.com/docs/9.x/migrations#generating-migrations

Here's an example code for adding the new field to the table:

ALTER TABLE TABLE_NAME ADD new_cell_name INTEGER(1) NOT NULL DEFAULT '5';
  •  

keith.bowman

When adding a new field to a large database without disrupting the stability of the project, it's important to follow a careful migration process. Here's a suggested plan to achieve this:

1. Create a new migration file: Use Laravel's migration command to generate a new migration file. This file will contain the necessary code to add the new field to the existing table.

2. Implement the migration: In the generated migration file, write the code to add the new field to the table. Set the default value of the field to "5". Make sure to handle any potential issues such as column conflicts or naming collisions.

3. Test the migration locally: Before running the migration on the production database, it's crucial to test it locally in a development or staging environment. Create a backup of the production database and import it into the local environment. Apply the migration and thoroughly test the entire system to ensure that it functions correctly.

4. Optimize the migration: For large databases, it's essential to optimize the migration process to minimize the impact on the system. Consider breaking the migration into smaller batches, each processing a subset of the records. This can be achieved using Laravel's chunking feature when querying the database. Chunking allows you to retrieve and modify a limited number of records at a time, preventing memory-related issues.

5. Prepare for deployment: Prior to deploying the migration to the production environment, inform your team about the upcoming change and its potential impact. Coordinate with other developers to ensure all necessary code changes, if any, are in place.

6. Perform a rolling deployment: To avoid any downtime, perform a rolling deployment where you update the application one server at a time while keeping the others online. This helps distribute the load and minimizes disruptions to users. Monitor the system during the deployment to detect any unexpected issues.

7. Execute the migration: Once the deployment is complete, run the migration on the production database. Since you've tested it thoroughly in the local environment, the chances of errors are minimized.

8. Monitor and verify: After executing the migration, monitor the system closely to ensure stability. Check that the new field has been successfully added to all records, including any existing ones, with the default value intact.

9. Optimize server and database settings: Before running the migration, ensure that your server and database settings are optimized for handling large datasets. This includes adjusting memory limits, query caching, and optimizing indexing for efficient data retrieval.

10. Plan for downtime: While the aim is to minimize disruptions, it's important to acknowledge that there may be some downtime during the deployment. Communicate this to your users and plan the deployment during periods of low traffic or implement a maintenance page to inform users about the temporary unavailability.

11. Monitor resource usage: Throughout the migration process, closely monitor the resource usage on your servers. Keep an eye on CPU, memory, and disk utilization to ensure that the system remains stable and performant during the migration.

12. Implement fallback options: In case any issues arise during the migration, have fallback plans in place. This might include having a full backup of the production database and a rollback strategy to revert to the previous state if necessary.

13. Inform stakeholders and users: Prioritize clear and effective communication with your team, stakeholders, and users. Notify them about the migration plan, potential downtime, and any relevant details they need to be aware of. Transparency and timely updates can help manage expectations and minimize concerns.

14. Consider using queues: If possible, consider using Laravel's queue system to handle the migration asynchronously. This allows the migration to be processed in the background without directly impacting the main application's performance.

15. Test and verify: After the migration is completed, thoroughly test and verify the functionality of the newly added field along with other critical features of the system. Conduct thorough regression testing to ensure that all aspects of the project are functioning as expected.
  •