Why Trunk-Based Development is the Future of Software Development
Kris Nicolaou, March 10, 2023
In today's fast-paced business environment, customer needs and preferences can change quickly. Companies that aim to remain relevant must adopt agile methodologies, which allow them to become more flexible and responsive.
The time to deploy any software plays a huge role in a company's success. Marketing it earlier allows them to get a good market share, increase revenue, and streamline sales growth. It's among the metrics that measure the success of product development.
According to a GitLab survey, software development and release delays are typical. These delays are often due to code development, review, analysis, data management, and testing bottlenecks. Developers can address all of these through trunk-based development (TBD).
But what is trunk-based development, exactly? Why does it matter in the development process?
In this article, we will explore why TBD is the future of software development and how it can help improve team collaboration and productivity.
Key Takeaways
- Trunk-based development allows developers to address the common pain points of software development and delivery.
- It uses one main "trunk," enabling developers to streamline their processes in a single branch.
- Major tech brands rely on trunk-based development to remain competitive.
What is trunk-based development?
In software development, the trunk is an integral part of the process. It is a base code into which all subsequent code is merged and integrated. This base code is a repository for all updates and changes made to the source code.
Copies made from this source code are known as branches, which extend outward from the trunk and allow developers to experiment with new features or bug fixes. This system enables software development teams to manage their projects and ensure quality control easily.
With trunk-based development (TBD), software creators make periodic slight code modifications in a shared branch of their version control system. This branch, the mainline or baseline branch, is also called the trunk.
This agile software development methodology enables teams to create, maintain, and deploy software faster and more efficiently using continuous integration (CI). It is based on having a single source code, allowing teams to identify and fix issues quickly.
Here's a diagram to show the process:
With TBD, developers can change the codebase without worrying about breaking existing features or introducing new bugs. This makes it easier for small teams to collaborate on projects and keep their codebase up-to-date with the latest changes. It also allows developers to merge conflicts and updates in one main branch.
The goal is to prevent long-lived feature branches by merging small changes to the entire feature. They can instead commit to the primary or short-lived branches through an effective code review process. This feature is essential because it lessens the duplicated work, preventing merge hell caused by long-lived branches.
When you compare it with non-trunk-based development, the process has more steps, requiring more time before deployment.
This is why it is becoming increasingly popular among software developers looking for better ways to manage their projects.
Trunk-Based Development vs. Gitflow
Trunk-based development and Gitflow are popular software development methodologies. While both have advantages and disadvantages, they have significant differences. Their development process features will help you arrive at an informed decision on which one is best for your project.
The methodologies emphasize code review but differ in merging code into a main branch. Trunk-based development focuses on keeping all changes in one branch, with all fixes and new changes happening in a single branch code. It doesn't require a new branch for each new process, making it simplified and faster to deploy.
Gitflow is a Git branching model using feature branches and several primary branches. The workflow encourages branching out from a main branch. It allows changes to undergo testing before merging into the main branch.
The merging will only occur when the feature branch is final. The number of long-lived feature branches requires more collaboration. This requirement is mainly due to its increased risk of producing a different source code from the trunk branch.
Benefits of Trunk-Based Development for Teams and Organizations
With TBD, developers can collaborate in real-time, share code quickly, and easily roll back to previous versions. The benefits of TBD are vast – it simplifies the process of developing complex systems while ensuring code quality and reducing time-to-market.
Here are its benefits in detail.
Faster integration of new features
Traditional branching and merging methods often take weeks to integrate changes from different branches. In trunk-based development, developers work on the same codebase simultaneously, allowing them to deliver new functionality faster.
With faster integration, developers can respond faster to changes in market demands and consumer needs. It's also critical in streaming the development process. They can fix bugs and test more efficiently, allowing developers to reduce the time and resources needed for development. Overall, it improved efficiency and productivity.
Efficient bug fixes
Since developers conduct integration in the main branch in real-time, they can readily test these changes with real-world simulation. This allows them to identify bugs and apply a hotfix, particularly for high-priority bugs that require immediate correction.
Developers should do bug fixes before product development and release. Software bugs significantly impact product quality, usability, and reliability. Software bugs can sometimes lead to security breaches or data loss.
Trunk-based development can save time and money. Most team members will spend around 30 to 50 percent of their time fixing bugs. Most of the time, some even have 16 engineers working to fix bugs. That's why solving these bugs during the design phase rather than the production stage is more cost-efficient.
One example is the blue screen of death (BSOD) errors following the Microsoft Windows 10 update (KB5000802) release last March 2021. The update should fix several security vulnerabilities in the operating system. However, it caused issues for some users, including performance problems, crashes, and BSOD errors.
According to reports, the issue was due to a compatibility problem with some third-party software, including antivirus programs. Microsoft quickly acknowledged the issue and released a new update (KB5001567) to address the problem. However, the bug affected many users and highlighted the importance of thorough testing and bug fixing before releasing software updates.
Improved collaboration
Trunk-based development is critical for improving collaboration and teamwork. With the development process happening in the main branch, the workflow is more organized and collaborative. This development approach encourages team members to have frequent code integration since they work on the same codebase. It fosters a sense of teamwork to ensure the code base remains stable and is of high quality.
This method is also critical for reducing knowledge silos. All members have visibility on what others are working on. Through this, senior team members can provide faster feedback. Developers catching issues and bugs earlier can help reduce overall development time.
Automated testing
Trunk-based development can improve software quality by making testing more efficient. Since all changes are integrated into the trunk immediately, developers can test changes. This leads to faster feedback and more opportunities to catch bugs before they enter the production environment.
Automated testing in trunk-based development involves creating a suite of automated tests running every time code changes appear in the trunk. These tests run different aspects of the code, including its functionality, performance, security, and reliability. By automating the process, developers can quickly identify any issues or bugs their code changes may have introduced.
It reduces the time and effort required to test code changes, allowing developers to focus more time and energy on coding and development tasks. It also helps catch bugs and errors early in development, reducing the likelihood of serious problems occurring later.
Pro-Tip
The best way for teams to fix a bug is to duplicate it on the trunk, remedy it with a test, and verify it by the CI server. Developers must also reproduce the CI pipeline guarding the trunk to protect and verify active release branches.
Best Practices for an Effective Trunk-Based Development Process
With the proper process, teams can confidently move from concept to production. Here are some of the best practices when implementing trunk-based development.
Keep it small
When doing TBD, it's best to make frequent, minor changes to the main code branch rather than waiting to integrate more enormous changes simultaneously. This approach offers several benefits, including faster feedback loops, reduced risk of conflicts and integration issues, and greater visibility into the codebase. However, to realize these benefits, making changes in small, incremental batches is crucial.
One reason is that small batches make it easier to isolate issues and identify the root causes of problems. Substantial sets can be challenging to pinpoint which caused issues or conflicts, making it harder to resolve problems efficiently. This dilemma applies when several developers are working on a project.
Another reason for doing trunk-based development in small batches is that it helps to reduce the risk of conflicts and integration issues. This practice also allows team members to avoid cognitive overload, allowing them to focus and decide on codes and changes.
Complement with feature flags
As mentioned, faster time to market is critical for software development. One way to achieve this is to minimize the complexity of merging code changes. However, it's always easier said than done. Pushing to integrate changes can lead to problems, especially when done haphazardly. This stage is where feature flags come into play.
Feature flags, also known as feature toggles, are a technique used to enable or disable features in an application. They allow developers to control the availability of features without deploying new code. Essentially, developers can dictate when and to whom to deliver these codes.
Feature flags encourage experimentation by enabling developers to try out new features without impacting users. Developers can iterate quickly and test different ideas, leading to innovative and improved features.
With feature flags, developers can selectively turn on or off certain application features during runtime. This allows them to test and experiment with different elements in a controlled environment.
For example, if a team wants to test a new user interface design, they can enable a feature flag. This feature flag will allow a subset of users to see the new design while the rest see the old design.
It enables them first to release new features to a small subset of users. Afterward, they can gather feedback and validate assumptions before rolling out the feature to all users. Developers can roll back a feature if issues arise, reducing the risk of potential bugs or downtime.
This allows teams to iterate and test new features faster, improving the overall development process and producing a better product.
Carry out synchronous code reviews
Doing a parallel code review is the best course of action when implementing trunk-based development. Ideally, developers must do a code review instantly. Asynchronous code reviews can only delay the process. If a second review is necessary, developers must do it concurrently.
This approach lessens the back and forth and reduces the delays in the process. Developers understand that delays in merging can result in merge conflicts, which can become time-consuming for all team members.
Synchronous code reviews also promote an efficient and collaborative work environment. Developers can view pull requests by team members, run them through automated tests, and see improvements in code coverage. This process assures engineers the new code meets requirements and specifications.
Automate testing
One of the primary advantages of trunk-based development is its ability to automate testing. Developers can leverage continuous integration and delivery. The automation should be done comprehensively, specifically in automated unit tests, and always before each commit.
For instance, GitHub developers can protect branches from pulling requests from merges after completing all tests.
Developers should also run these tests before committing to the code. They should also ensure these tests run at different intervals and stages of the pipeline. For example, short-running unit tests should occur during development and code merging. Long-running tests, on the other hand, are best executed in the production environment.
Automating the testing process is necessary, particularly for those transitioning from Gitflow. Software engineers must design and implement meaningful software testing.
Consider this scenario: You're using continuous integration with trunk-based development to ship code to the production environment. This instance can happen multiple times in a day. This can mean you may not have enough capacity to conduct a full regression test before deployment.
In such a case, code identification through automated testing can help break these codes before it reaches the trunk.
Limit active branches in the code repository
It's best to delete branches once they merge. There's no hard limit on the active branches contained within a repository. Keeping a repository with vast volumes of active branches can only lead to problems.
One of these is creating complexities in a code repository. When this happens, developers may need help understanding the codebase state. It can lead to confusion and mistakes. More active branches can also lead to merge conflicts.
This can happen when changes in one branch conflict with those in another. Merge conflicts can be time-consuming and challenging, primarily if multiple people work on different branches. Limiting the number of active branches can help reduce the likelihood of merge conflicts, making it easier to integrate changes.
Limited active branches also mean faster and more efficient code reviews. Code reviews are essential to the development process. However, they can become a bottleneck when there are too many branches to review. By limiting the number of active branches, developers can review code more quickly, speeding up the overall development process.
Knowing which branch developers need to roll back can be difficult when too many active branches exist. It can be challenging to pinpoint the last known good state.
Streamline Software Delivery With Trunk-Based Development
Trunk-based development is a robust, agile methodology that enables software development teams to deliver high-quality code faster and more efficiently. It allows developers to collaborate in real time, streamline processes, and reduce time-to-market.
By adopting trunk-based development, companies can respond more quickly to changes in market demands and customer needs. On top of these, trunk-based development is also critical in improving team productivity and ultimately achieving better software development.
With the fast-paced nature of modern business environments, it's becoming increasingly important for software development teams to implement agile methodologies. Trunk-based development is among those they should look into if they want to streamline their processes.
As such, organizations that embrace this approach are poised to lead the future of software development.
Respond to changing consumer preferences and market needs, and embrace an agile approach like trunk-based development. Explore trunk-based development and take the first step towards leading the future of software development with Brain Box Labs.