Web Project Code Refactoring: Practices, Challenges, and Solutions
The article details a year‑long refactoring case study of a fast‑iteration web project migrated from Python to Java/Go, describing inherited performance bugs, a prioritized migration plan, monitoring integration, concrete optimizations such as query reduction and cache redesign, and the resulting stability and latency gains, while outlining required developer skills and best‑practice recommendations.
This article presents a practical case study of refactoring a fast‑iteration web project originally written in Python and later migrated to Java/Go. It outlines the background problems, the need for change, and the initial refactoring ideas.
Background : The team inherited a five‑month‑old project with many hidden issues such as database queries inside loops, ineffective caching, and complex eviction mechanisms, leading to performance and stability problems.
Pre‑refactoring work : The team analyzed business flows, evaluated pain points, and assessed refactoring difficulty and urgency. A decision matrix (difficulty vs. urgency) guided the order of module migration.
Traffic migration plan : A new repository was created, authentication was unified, gateway rules (or Nginx) were used for traffic forwarding, and incremental migration was performed while monitoring for rollback.
Monitoring improvements : Trace, log, and metric systems were integrated. Example log entry:
{
"level":"error",
"ts":"2022-07-22T21:26:00.073+0800",
"caller":"api/foo_bar.go:38",
"msg":"[foo]bar",
"error":"Post \"https://xxx.com/abc/xxx\": context deadline exceeded",
"traceId":"0aee15dc63f617e751d17060xcf74b9c",
"content":{
"body":"json str",
"resp":""
}
}Typical problems and optimization solutions are listed in tables, covering areas such as:
Database query I/O reduction
Field reduction in API responses
Unified return types
Recursive tree generation with O(n) complexity
Cache redesign and eviction strategy
SQL index tuning
Redis slow‑query and key‑size optimization
Asynchronous consumer script reliability
Key code examples:
// List‑interface loop I/O problem (illustrative comment)
// ORM usage is coarse, extra GET methods query related tables per item,
// causing repeated DB network I/O. Pre‑load or map‑based aggregation can reduce it.Batch processing with checkpointing:
page := 1
pageSize := 10000
for {
// fetch a page of data
dataList := make([]itemStruct{}, 0)
// process batch logic
...
// record checkpoint (DB, Redis, etc.)
if len(dataList) < pageSize {
break
}
page += 1
}Results : After nearly a year of incremental refactoring, >80% of modules were updated, leading to:
Full trace, log, metric, and alert integration
Significant stability improvement
Elimination of most looped DB queries and slow queries
Major API latency reductions (e.g., from >10 s to <1 s)
Consolidated reusable logic and lower maintenance cost
Skill requirements for developers include proficiency in the project language(s), deep database knowledge, familiarity with middleware, solid fundamentals in OS, algorithms, and high‑availability design.
The article concludes with recommendations for making refactoring a series of small, manageable tasks, emphasizing linting, code review, unit and performance testing, and continuous skill growth.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
DeWu Technology
A platform for sharing and discussing tech knowledge, guiding you toward the cloud of technology.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
