Blog>>Software development>>Trunk-based development: best practices and strategies

Trunk-based development: best practices and strategies

All development teams strive for an agile, flexible, and efficient software development process. There are many ways to achieve it. Trunk-based development is one of the more modern approaches that is gaining attention. Let’s take a closer look at what it is, how it works, and what benefits it can bring your team. 

What is trunk-based development?

Trunk-based development is a management practice. It is a source-control branching model focusing on a single branch of code called the trunk. Trunk-based development aims to keep the trunk ready for deployment at all times. Developers collaborate on small parts of code, which are frequently updated and merged into the core branch. 

software outsourcing services

It is an agile, rapid process - the code review is efficient, and developing and merging the branches shouldn’t take more than a few days. Trunk-based development is close to the ideas of the DevOps methodology, and it also takes advantage of automated testing and integration. 

Fig.1: Trunk-based development diagram
a diagram showing trunk-based development

This model aims to achieve CI/CD (continuous integration/continuous delivery) and increase the team's efficiency and the product's quality. When properly deployed, it can streamline the merging process and help avoid integration hell. It is useful in a situation where merging is problematic, and new parts of code must be fixed, debugged, and sometimes completely rewritten to be integrated into the existing code. 

Trunk based-development stands in opposition to traditional branching models, which require developing long-lived branches. This process takes much longer, as the branches are more complex. Also, merging difficulties can set the team back and slow down progress. 

Fig.2: Feature branch development diagram
diagram of feature branch development

Benefits of trunk-based development

The trunk-based approach makes developers’ lives easier in many ways. Most importantly, merging runs as smoothly as possible. Let’s take a closer look at the trunk-based model's benefits.

Benefits of trunk-based development

Reduced merging complexity

Complex, time-consuming, complicated merging is precisely the issue that trunk-based development aims to solve. Reducing the size of new branches helps to keep integration more manageable, saving time, labor, and money.

Continuous integration

Trunk-based development is one of the ways to achieve the CI/CD pipeline. A slower, less flexible approach would make continuous integration much harder. There is a constant stream of new parts of code flowing into the main branch. Therefore automated testing and integration are a must. 

Continuous code review

The smaller size of newly developed branches in trunk-based development encourages instant feedback. Moreover, focused changes make the code review process smoother and more efficient. Additionally, these branches are less complex than in the traditional model, so finding the source of a problem and making the necessary changes is quicker and easier.

You can check out this blog post for more information on software development best practices. It provides valuable insights and guidelines to help streamline your development workflows.

The smaller size of newly developed branches makes it possible to acquire instant feedback. Branches are also less complex than in the traditional model, so finding the source of a problem and making necessary changes is quicker and easier. 

No duplicated work

Because the teams deploy their branches frequently, there is less possibility for duplication of work, even if various developers are working on similar issues. As a result, teams stay on the same page, staying up-to-date with all the changes happening in the trunk. 

Uninterrupted code releases

According to the rudiments of the trunk-based model, the core branch should always be ready for deployment. It is possible thanks to instant feedback and the constant fixing of new parts of the code. Automated testing also plays a significant role here, making the process less time-consuming.

All the above benefits come together to provide a smooth, efficient software development process. Using this model saves time and labor and, therefore, results in a more cost-efficient development.

Trunk-based development implementation strategies

Below, you will find some strategies that can facilitate the implementation of trunk-based development. By implementing these, the main branch will be ready to release at every point. 

Feature flags and toggles

Feature flags and toggles are tools for adopting the "Branch by Abstraction" approach. This technique dispenses with the need for traditional feature branches, instead allowing you to transition between code states by toggling abstractions seamlessly.

Feature flags and toggles create a dynamic environment where new features can be gradually rolled out, experimentation becomes easier, merge conflicts are minimized, and continuous integration is a breeze. This streamlined approach ensures a more efficient and collaborative development process.

Explain the concept of feature flags and how they enable gradual feature rollout and experimentation. Discuss different strategies for implementing feature flags and their benefits.

Continuous integration and deployment

Describe how continuous integration and deployment pipelines are crucial in trunk-based development. Highlight the importance of automated testing and how it ensures code quality. Here, the main aim is to keep the main ready for release on demand. 

Code reviews and quality control

Discuss how code reviews fit into the trunk-based workflow. Emphasize the need for smaller code reviews and how they contribute to faster integration.

Best practices for trunk-based development

Let's explore the key principles of a trunk-based development strategy that facilitates seamless integration and optimization of development workflows. 

By embracing these principles, you will embark on a journey toward a codebase that evolves gracefully, delivers value consistently, and adapts to the ever-evolving demands of modern software engineering.

Small but frequent commits

The practice of making small, incremental commits aligns perfectly with the principles of trunk-based development. Smaller commits reduce the likelihood of conflicts, make code reviews more manageable, and simplify the process of isolating and addressing issues. Each commit should represent a single logical change, accompanied by a descriptive commit message that communicates its purpose clearly.

Encourage your development team to adopt a culture of small, focused commits – by doing so, you'll contribute to a codebase that is easier to understand, maintain, and evolve.

Frequent integration and testing

Frequent integration and automated testing are cornerstones of trunk-based development, ensuring the codebase's stability and facilitating early detection of defects. By continuously integrating and testing changes, teams can quickly identify and address issues before they accumulate, reducing the risk of costly and time-consuming bug-fixing efforts.

Prioritize the creation of a robust suite of automated tests that cover different aspects of your application. Strive for quick feedback loops and rapid identification of problems. Adhering to well-defined pull request guidelines streamlines the review process and ensures that only well-tested, high-quality code makes its way into the mainline.

By making testing an integral part of your development workflow, you'll build a foundation of quality and reliability that underpins successful trunk-based development. 

Pull request guidelines

Pull requests are vital in trunk-based development, serving as a mechanism for reviewing and integrating code changes. 

Foster a collaborative and constructive code review culture within your team. Encourage open discussions and provide timely feedback to enhance the quality of contributions. 

Monitoring and feedback loop

Continuous monitoring and feedback collection are essential components of successful trunk-based development. Actively monitor the performance of deployed features and gather user feedback to assess the impact of changes on real-world usage. This feedback loop enables you to identify areas for improvement and make informed decisions for future iterations.

Invest in robust monitoring tools and establish clear channels for user feedback. Regularly analyze the collected data to identify trends and patterns. By maintaining a vigilant feedback loop, your team can fine-tune the development process, address user concerns promptly, and continuously enhance the user experience.

By incorporating these best practices into the fabric of your development workflow, you embark on a path toward creating functional, robust, responsive, and adaptable software – a true testament to the potential of trunk-based development.

Step-by-step guide to implementing trunk-based development

This step-by-step guide is designed to help you seamlessly transition to a trunk-based workflow. By adopting these strategies, you will enhance the efficiency of your development process and elevate the quality and agility of your codebase. 

Assessing current development workflow

Guide readers through evaluating their current development process and identifying pain points. By taking a comprehensive look at your existing practices, you'll gain the insights needed to tailor trunk-based development to your specific needs.

Setting up continuous integration and deployment pipelines

Provide a step-by-step process for implementing CI/CD pipelines, including tool recommendations and integration points. Learn how to automate build, test, and deployment processes, laying the foundation for seamless integration of code changes.

Introducing feature flags

Walk through the process of introducing feature flags, from basic implementation to more advanced strategies. Uncover the power of controlled feature releases and gradual deployments, enabling your team to navigate complexity easily.

Establishing code review processes

Help readers establish effective code review processes that align with trunk-based development. Discover the art of efficient reviews and collaboration, ensuring high-quality code consistently enters the mainline.

Monitoring and iterating in the workflow

Explain how to set up monitoring tools and how to analyze data to make iterative improvements to the trunk-based workflow. Maintaining a vigilant feedback loop will fine-tune your development process, optimizing for continuous enhancement.

This comprehensive toolkit facilitates agility, collaboration, and quality in the development process by assessing the current workflow, automating integration and deployment, wielding the power of feature flags, and refining the review and monitoring processes.

Trunk-based development vs. Gitflow

It’s important to note that trunk-based development is a successor to other practices that initially disrupted the scene of software development management. One of them is Gitflow. It was the workflow that provided a modern strategy for managing branches. However, it is much less popular than it used to be, as it is challenging to manage while maintaining the CI/CD pipeline. 

Gitflow is a branching model based on feature branches and multiple primary branches. It uses longer-lived branches than trunk-based development, and merging them is delayed until a feature is complete. 

This approach requires more collaboration during the merging process, making it more time-consuming. There is also a higher risk of issues compared to trunk-based development, and the core branch does not stay ready for deployment.

Gitflow flow diagram
Gitflow flow diagram

Gitflow vs. Trunk - when to use them?

Trunk-based development is now a common practice among software development teams, but Gitflow can still prove helpful in some instances. For example, Gitflow can be used in projects with a scheduled release cycle. 

Working on a single feature at once for an extended time also can have its benefits. For example, you can conduct isolated experiments. The stricter rules of Gitflow also ensure code quality - the branch can be integrated into the code only once specific individuals accept it. 

Use Gitflow when:

  • The project requires a strict and formalized workflow with clear separation of feature development, release cycles, and hotfixes.
  • The development team needs a structured approach that allows for parallel development of different features or bug fixes.
  • There is a need for a well-defined release process with distinct branches for feature development, release candidate testing, and production releases.
  • The project involves multiple teams or contributors working on different features concurrently, and a workflow that supports code isolation and collaboration.

Use Trunk when:

  • The project follows a continuous integration (CI) and continuous delivery (CD) approach, focusing on frequent and rapid releases.
  • The development team values simplicity and streamlined processes, without the overhead of maintaining multiple long-lived branches.
  • There is a high level of trust and collaboration within the development team, with a focus on fast feedback loops and quick iterations.
  • The team emphasizes automated testing, code quality, and robust deployment processes to ensure stability and minimize the risk of issues.

When is trunk-based development helpful?

Trunk-based development is a specific management style that will not work for every software project. When deciding whether to use this approach or not, you should consider two factors.

Rapid progress and transparent communication

First, it’s important to remember that this development style means rapid progress. The team should be ready for that, so there should be a transparent communication strategy. That way, the developers are on the same page and continuously updated about changes in the trunk.

Rapid progress also means that the team needs to move quickly. The trunk-based approach requires a motivated, competent development crew.

Trust in developers and short code review

The other thing to keep in mind is that trunk-based development puts a lot of trust in developers. There is a short code review process, so each developer needs to be careful about the changes they commit to the trunk.

That means that this style of software development management probably won't work for open-source projects where anyone can open pull requests.

Ideal for collaborative teams and startups

That said, trunk-based development is still a good choice for many situations. For example, it works great for startups and other teams that are good at collaborating and keeping high development speed.

If you're curious about implementing this approach in your project, check out our custom software development services.

How to make trunk-based development work?

Once you know what trunk-based development is and when to use it, you should learn how to make it work for your team. Here we share some best practices that make trunk-based development run smoothly and effectively.

How to make trunk-based development work

Three or less branches

That’s the number of branches that should usually be active on an application repositories’ version control systems. Sometimes it can differ due to a project’s specifics, but it’s important to set up a clear number. The number should be easy to manage and to make sure it's visible to everyone on the team.

No code freezes

Avoiding code freezes and pauses for integration phases is crucial for successful trunk-based development. In the CI/CD pipeline, there should be a constant flow of updates. Therefore, merging conflicts and code freezes that stop developers from submitting code should happen as rarely as possible.

Merging once a day

The frequency of merging is an important measurement for trunk-based development. The environment should encourage every developer to commit to the trunk at least once every 24 hours. It is a requirement for continuous integration.

Synchronous code review

An efficient code review that doesn’t take hours or days to get final approval is key for performing daily merging. Developers shouldn’t start new parts of code until the previous one is merged - that helps keep the team focused. The shorter the merging latency is, the less possibility there is for merging problems.

Measuring the success of trunk-based development

In order to track whether your team meets the trunk-based development standards, you need some measuring methods. These metrics are similar to the above-mentioned best practices, and they make it easy to recognize room for improvement.

The factors to test include:

  • the number of active branches on the application’s code repository (usually three or fewer)
  • the number of code freeze periods (should be zero)
  • frequency of merging branches (at least once a day)
  • time of approving code changes (should be as short as possible).

These four checkpoints can keep your team on track with the requirements of trunk-based development.

Conclusion

Trunk-based development is a good tool for keeping software development running smoothly. This practice aims to save the time and labor of developers, minimize the possibility of merging difficulties and utilize test automation. It has certain requirements and won’t work for every project, but it can significantly improve a team when deployed correctly. It is also a cost and time-effective version control management approach.

All development teams strive for an agile, flexible, and efficient software development process. There are many ways to achieve it. Trunk-based development is one of the more modern approaches that is gaining attention. Let’s take a closer look at what it is, how it works, and what benefits it can bring your team. 

Original post date 10/05/2022, update date 09/15/2023.

Manturewicz Maciej

Maciej Manturewicz

Director of Engineering

Maciej is a Director of Engineering with nearly two decades in the software industry. He started his career journey as a software engineer, and he gained experience on every step of the ladder before landing in his current leadership role. With a rich background in software engineering, Maciej possesses a...Read about author >

Read also

Get your project estimate

For businesses that need support in their software or network engineering projects, please fill in the form and we’ll get back to you within one business day.

For businesses that need support in their software or network engineering projects, please fill in the form and we’ll get back to you within one business day.

We guarantee 100% privacy.

Trusted by leaders:

Cisco Systems
Palo Alto Services
Equinix
Jupiter Networks
Nutanix