32 min

購物車系統的高並發改造(二):Redisson 分散式鎖、讀寫分離路由與 Docker HA 水平擴展

前言 第一篇解決了系統的吞吐量問題:Virtual Threads 讓執行緒不再阻塞在 I/O 上,HikariCP 調校讓資料庫連線不再是瓶頸,Redis 快取讓熱點讀取從記憶體直接回傳。 但吞吐量提升後,反而暴露出一個更深層的問題: 並發請求同時寫入同一份資料,怎麼保證正確性? 想像雙十一活動開始的那一秒,同一個用戶的兩個 Tab 同時按下「加入購物車」;或者同一個人網路不穩,重試了兩次「立即結帳」——這兩種場景都會導致重複訂單或資料不一致。 這篇介紹 PR #228 帶來的三個進階改造: Redisson 分散式鎖:防止同一用戶的並發寫入互相干擾 讀寫分離路由:把讀請求分流到 Replica,Primary 只處理寫入 Docker HA 水平擴展:Nginx + 多個 App 實例 + MySQL 主從複製 方案四:Redisson 分散式鎖 為什麼需要分散式鎖? 第一篇的 CartService.addToCart() 在高並發下有一個隱患: 1// Part 1 的版本(沒有鎖) 2public void addToCart(AddToCartDto addToCartDto, Product product, User user) { 3 Cart cart = new Cart(product, addToCartDto.getQuantity(), user); 4 cartRepository.save(cart); // ← 多個執行緒可能同時執行這一行 5} 當同一個用戶同時發出兩個「加入購物車」請求時: Thread A: new Cart(productX, qty=1, user) → save → 購物車有 1 個 productX Thread B: new Cart(productX, qty=1, user) → save → 購物車有 2 個 productX(重複!) cartRepository.

Spring Boot Java Redisson
30 min

購物車系統的高並發改造(一):Virtual Threads、HikariCP 與 Redis 快取三管齊下

前言 電商系統有一個非常典型的流量特徵:平時風平浪靜,一到促銷活動瞬間湧入幾千上萬個並發請求。 如果你的購物車系統跑在默認的 Spring Boot 設定上,這個瞬間幾乎必定會讓系統崩潰——不是 OOM,就是資料庫連線耗盡,要不然就是 Tomcat 執行緒池打滿、請求開始逾時。 這篇文章深入剖析我們在 PR #227 中對一個 Spring Boot 購物車系統進行的高並發改造,涵蓋三個核心技術方向: JDK 21 Virtual Threads:讓每個請求都跑在輕量級虛擬執行緒上 HikariCP 連線池調校:消除資料庫連線成為瓶頸的問題 Redis 分層快取:把熱點讀取從資料庫移到記憶體 系統架構概述 先看清楚我們在改什麼。這是一個標準的 Spring Boot 電商後端: Client (Web/App) ↓ HTTP Spring Boot API (port 9999) ├── CartController → CartService → MySQL (cart table) ├── ProductController → ProductService → MySQL (products table) ├── OrderController → OrderService → MySQL (orders/order_items) │ → Stripe API (付款) ├── UserController → UserService → MySQL (users) └── AuthController → AuthenticationService → MySQL (auth_tokens) 技術棧:Spring Boot 3.

Spring Boot Java High Concurrency
60 min

Building a Centralized User Access Control System with AWS Cognito and CDK

🎯 Introduction Building a centralized user access control system is one of the most critical architectural decisions for modern applications. Whether you’re managing a single application or a microservices ecosystem, having a robust, scalable authentication and authorization system is essential for: Single Source of Truth: One system managing all user identities and permissions Consistency: Uniform authentication experience across all services Security: Centralized security policies and compliance controls Scalability: Support for millions of users across multiple applications Developer Experience: Simple integration for new services Cost Efficiency: Managed service without operational overhead This comprehensive guide demonstrates how to design and implement a production-ready centralized access control system using AWS Cognito and CDK (TypeScript), with strategies for multi-tenancy, role-based access control (RBAC), and integration patterns for various services.

AWS Cognito CDK TypeScript
65 min

AWS Load Balancers: Complete Guide - Application, Network, Gateway, and Classic Load Balancers Comparison with Implementation

🎯 Introduction AWS Load Balancers are critical components for building highly available, fault-tolerant, and scalable applications in the cloud. They distribute incoming traffic across multiple targets, ensuring optimal resource utilization and system reliability. This comprehensive guide explores all AWS Load Balancer types, their unique features, and when to use each one for maximum effectiveness. Understanding the nuances between Application Load Balancer (ALB), Network Load Balancer (NLB), Gateway Load Balancer (GWLB), and Classic Load Balancer (CLB) is essential for architecting robust cloud solutions that can handle varying traffic patterns and requirements.

AWS Load Balancer ALB NLB
60 min

AWS API Gateway: Complete Guide with Load Balancer Comparison, Microservices Architecture, and Java Implementation

🎯 Introduction In modern distributed systems with dozens or hundreds of microservices, managing API traffic becomes increasingly complex. AWS API Gateway emerges as a critical component that acts as a single entry point for all client requests, solving major challenges in microservices architecture. This comprehensive guide explores API Gateway fundamentals, compares it with load balancers, and provides production-ready Java implementations. API Gateway transforms chaotic microservices communication into organized, secure, and scalable architecture patterns that are essential for enterprise-grade applications.

AWS API Gateway Load Balancer Microservices
55 min

Webhooks: Complete Guide with Java Implementation - Event-Driven Architecture, Real-Time Integrations, and Best Practices

🎯 Introduction Webhooks are HTTP callbacks that enable real-time, event-driven communication between applications. Instead of continuously polling for updates, webhooks allow systems to push data immediately when events occur. This comprehensive guide explores webhook architecture, compares different integration approaches, and provides production-ready Java implementations with real-world examples from Stripe, Shopify, and GitHub. Webhooks have become essential for modern distributed systems, enabling efficient, scalable, and responsive integrations that power everything from payment processing to CI/CD pipelines and e-commerce automation.

Webhooks Event-Driven Architecture Java
50 min

Redis Sentinel: Complete High Availability Setup Guide with Java Integration and Monitoring

🎯 Introduction Redis Sentinel provides high availability and monitoring for Redis deployments. It’s a distributed system that monitors Redis master and replica instances, performs automatic failover, and acts as a configuration provider for clients. This comprehensive guide covers Redis Sentinel architecture, setup procedures, Java integration, and production best practices. Redis Sentinel solves critical production challenges including automatic failover, service discovery, and configuration management, making it essential for mission-critical applications that require high availability and minimal downtime.

Redis Redis Sentinel High Availability
45 min

MySQL Sharding Strategies: A Comprehensive Guide to Horizontal Scaling, Partitioning Methods, and Implementation Patterns

🎯 Introduction Database sharding is a critical technique for achieving horizontal scalability in large-scale applications. As data volumes grow beyond what a single database instance can handle efficiently, sharding becomes essential for maintaining performance and availability. This comprehensive guide explores MySQL sharding strategies, comparing different approaches, implementation patterns, and real-world considerations. Sharding involves distributing data across multiple database instances (shards), where each shard contains a subset of the total data. This approach enables applications to scale beyond the limitations of vertical scaling and provides better resource utilization across multiple servers.

MySQL Database Sharding Horizontal Scaling
50 min

Essential Design Patterns in Java: A Comprehensive Guide to Creational, Structural, and Behavioral Patterns

🎯 Introduction Design patterns are proven solutions to commonly occurring problems in software design. They represent best practices evolved over time and provide a shared vocabulary for developers. This comprehensive guide explores the most essential design patterns in Java, demonstrating practical implementations with real-world examples. We’ll cover the three main categories of design patterns from the Gang of Four: Creational, Structural, and Behavioral patterns, showing how to implement them effectively in modern Java applications.

Java Design Patterns Software Architecture
45 min

Java Concurrency Part 3: Design Patterns with Thread Interfaces - Producer-Consumer, Observer, and Enterprise Patterns

🎯 Introduction Building upon our deep dive into Java concurrency fundamentals, this third part explores how classic design patterns can be elegantly implemented using thread interfaces. We’ll examine how Runnable, Callable, and other concurrency primitives can be combined with design patterns to create robust, scalable, and maintainable concurrent systems. This guide demonstrates practical implementations of essential design patterns in concurrent environments, showing how threading interfaces enhance traditional patterns while addressing the unique challenges of multi-threaded programming.

Java Concurrency Design Patterns
40 min

Java Concurrency Deep Dive Part 2: Mastering Runnable, Callable Patterns and Internal Mechanisms

🎯 Introduction Building upon our comprehensive overview of Java concurrency, this deep dive explores the fundamental building blocks that power Java’s threading mechanisms. We’ll dissect the internals of Runnable and Callable interfaces, examine thread synchronization primitives, understand the Java Memory Model, and explore advanced patterns that form the foundation of robust concurrent applications. This technical deep dive is essential for developers who want to understand not just how to use Java’s concurrency tools, but how they work under the hood and how to leverage them effectively in complex scenarios.

Java Concurrency Threading
35 min

Java Concurrency and Threading: Complete Guide to Runnable, Callable, and Modern Thread Patterns

🎯 Introduction Concurrency and threading are fundamental aspects of modern Java applications, enabling programs to perform multiple tasks simultaneously and efficiently utilize system resources. As applications become more complex and performance requirements increase, understanding Java’s threading mechanisms becomes crucial for building scalable, responsive applications. This comprehensive guide explores Java’s concurrency landscape, from basic threading concepts to advanced patterns, providing practical implementations and performance insights for enterprise applications. 🧵 Java Threading Fundamentals 🔍 Understanding Threads and Concurrency A thread is a lightweight sub-process that allows concurrent execution of multiple tasks within a single program.

Java Concurrency Threading
30 min

SAGA Pattern: Managing Distributed Transactions in Spring Boot Microservices

🎯 Introduction In the era of microservices architecture, managing transactions across multiple services presents significant challenges. Traditional distributed transaction mechanisms like Two-Phase Commit (2PC) often lead to tight coupling, reduced availability, and poor performance. The SAGA Pattern emerges as a powerful alternative, providing a way to manage distributed transactions through a sequence of local transactions, each with compensating actions for rollback scenarios. 📚 What is the SAGA Pattern? 🔍 Core Concepts The SAGA pattern is a design pattern for managing long-running distributed transactions across multiple microservices.

Java Spring Boot SAGA Pattern
25 min

Data Consistency Patterns in Java Enterprise Applications

🎯 Introduction Data consistency is one of the most critical challenges in modern Java enterprise applications. As systems scale and become distributed, maintaining data integrity while ensuring performance becomes increasingly complex. This comprehensive guide explores practical data consistency patterns implemented in real-world Java applications, complete with case studies, implementation details, and detailed trade-off analysis. 📊 The Data Consistency Challenge 🔍 Understanding Data Consistency Levels Data consistency refers to the guarantee that all nodes in a distributed system see the same data at the same time.

Java Spring Boot Data Consistency