Introduction to Vertical Slice Architecture
Learn about Vertical Slice Architecture in an intuitive way
One of the most commonly used software architectures is the Layered Architecture. It is also known as the N-Layer or N-Tier Architecture. It promotes the idea of separating software into horizontal layers and each layer having a specific responsibility. Each layer depends on and uses the layer directly beneath it. It usually consists of three layers:
- Presentation
- Application
- Data
The bottom layer is the Data layer. This layer is responsible for retrieving and persisting data. The middle layer is the Application layer. This layer contains the business logic and uses the Data layer. The top layer is the Presentation layer. This layer allows the "outside world" (i.e. other application or users) to access our application (e.g. by providing an API) and uses the Application layer.
This architecture is often used in conjunction with Domain-Driven Design or DDD for short. DDD is a software approach that promotes the idea that software should be modeled according to the domain for which it's built. For example, if the domain is education, the software would be modeled to contain entities that exist within an education system, such as: students, classes, teachers, grades, schedules and so on.
Putting the Layered Architecture and Domain-Driven Design together, this is how an education software system could look:
Image taken from: https://www.drawio.com/As we add different features to the software, we usually add code to the components (e.g. controllers, services, repositories) that correspond to the entities that those features concern. For example, we could have a feature to create a new student and retrieve all students. This is how our student related components would look like across the different layers:
Image taken from: https://www.drawio.com/As can be noticed, the components contain code for different features. This example contains only two features, but a complicated domain could have many more. The components would quickly grow and will be hard to read and maintain.
We can make our codebase easier to read and maintain if we employ the Vertical Slice Architecture. This architecture promotes the idea of vertically "slicing" the horizontal layers of the Layered Architecture. Every vertical slice corresponds to a feature. That means that we will have as many entity-related components in each layer as there are features related to that entity. This is how our education software system could look:
Image taken from: https://www.drawio.com/As can be noticed, we have three features. For each feature we have components in the different layers.
What are the benefits of the Vertical Slice Architecture?
- Features are easy to be identified by looking at the project file structure.
- Features can be added/modified/removed with less chance of breaking other features.
- Components contain less code and are easier to read and maintain.
What are the drawbacks of the Vertical Slice Architecture?
- Code duplication.
- More files.
If we think of the Layered Architecture as a cake 🎂, we can think of the Vertical Slice Architecture as a box of chocolates 🧁. The box of chocolates allows us variety of different colors, shapes and tastes. The cake is beautiful, but does not allow variety as easily.
My personal opinion is that the benefits outweigh the drawbacks. We have to accept that some code duplication is not a catastrophe. If many different features have duplicate code, we can extract that code to a separate component.
If you enjoy my articles, please consider supporting me.