KakaoTalk Reduces Mutable Contexts from 10 to 3 and Eliminates Circular Dependencies via Java App Server Refactoring
Key point
Hundreds of lines of code were deleted to improve maintainability and testability without degrading the performance of the live service.
Details
The KakaoTalk Messaging team refactored legacy code in the Java App Server, significantly improving maintainability and testability without degrading the performance of the live service. The core of the effort was cleaning up the overused mutable Context classes and breaking complex circular dependencies between services.
Reducing Mutable Context Classes
The KakaoTalk messaging server, now in its 12th year, had more than 10 mutable Context classes that held various states such as login status and chat participation status. These classes exchanged variables with each other and were used like global variables, making debugging and code comprehension difficult. Developers changed the approach from passing Context as a parameter to explicitly receiving or returning only the necessary values. Additionally, they modified the code to handle DB query results as local variables outside the loop instead of caching them in the Context. As a result, they reduced the number of Context classes to 3 and deleted hundreds of lines of code while maintaining the same functionality.
Decoupling Dependencies via Higher-Order Functions
To resolve Circular Dependencies, a common issue in Spring-based projects, they introduced a higher-order function approach using functional interfaces. Previously, field injection between service objects caused tight coupling, which was changed to function dependencies. For example, instead of ServiceA depending on ServiceB, ServiceB's method is passed as a Function type and called. This approach allows for unit testing by directly creating objects without relying on the Spring container or Mockito, and also enables quick code verification via JShell.
Managing Code Complexity
Code complexity was quantitatively managed using Cyclomatic Complexity (CC) and NPath Complexity metrics. Code that assigned multiple responsibilities to a single function was separated using the Extract Function technique, and nested conditional statements were changed to Guard Clauses to improve readability. However, since readability improvements could lead to an increase in NPath values, a balance was struck between metrics and readability, such as by explicitly using else if structures. After the refactoring was deployed to the live service, no operational performance degradation occurred.
This summary was generated automatically by AI. Check the original for the author's claims and context. Copyright belongs to the original author.
Our guide explains how the AI works. Report summary errors, attribution issues, or removal requests via Contact.