The Goldilocks Zone: Finding the Best Size for Microservices
As more and more companies use microservices in their software, a big question pops up: how big should each microservice be? Like in the story of Goldilocks, we’re looking for the size that’s “just right” — not too big, not too small.
What are Microservices? Microservices are small pieces of an app that work on their own. Instead of one big program, you have lots of little ones that do their own thing but work together.
How Big Should They Be? Experts suggest that a microservice should have between 100 to 1,000 lines of code. This range is called the “Goldilocks zone”:
- Less than 100 lines might make things too complicated because you end up with too many tiny services.
- More than 1,000 lines, and you’re getting close to having a mini-big app again, which isn’t the goal.
Key Points to Consider:
- Functional Scope: Each microservice should do just one main thing. This keeps it simple and easy to work on.
- Low Coupling: Services should not depend too much on each other, making them easier to update or change.
- High Cohesion: Everything in a microservice should be closely related. This helps keep it straightforward and manageable.
- Simplicity: Keep it simple. Complex services are harder to manage.
Chris Richardson, a microservices expert, advises asking, “What’s the smallest piece of functionality that gives value to the user?” This idea fits with the “Single Responsibility Principle,” where each part of the program should have only one job.
Examples from Big Companies:
- Netflix keeps their microservices around 1,000 lines each.
- Amazon also focuses on small services, each handling one business task.
Flexibility in Size: The perfect size can change based on what the service needs to do. A service for user login might be small, while one handling all aspects of shopping might need to be larger.
Finding Balance: The goal is for each microservice to be:
- Small enough to be independent and agile.
- Big enough to be useful.
Companies should keep checking and tweaking their microservices to make sure they’re working well with the changing demands of the app.
Tools and Monitoring: Using tools to watch how well services are doing can help decide if they need to be bigger or smaller based on how they perform.
Conclusion: While there’s no perfect size for every microservice, aiming for 100 to 1,000 lines of code is a good start. By focusing on this and always improving, businesses can make the most out of microservices, creating applications that are easy to scale, change, and keep up to date.
References:
* Fowler, M. (2015). Microservices. Retrieved from https://martinfowler.com/articles/microservices.html
* Martin, R. C. (2002). Agile Software Development: Principles, Patterns, and Practices. Prentice Hall.
* Newman, S. (2015). Building Microservices. O’Reilly Media.
* Richardson, C. (2018). Microservices Patterns. Manning Publications.