How to Tackle High Concurrency: Prevent Data Chaos and Server Overload
This article explains the consequences of high‑traffic spikes, presents practical database and code‑level strategies to keep data consistent, and outlines server‑side architectures—including load balancing, caching, and Redis queues—to sustain massive concurrent requests without crashing.
1. Consequences of High Concurrency
Server side: CPU, memory and DB resources become saturated, leading to crashes and inconsistent data such as duplicate records or multiple point additions.
User side: Slow responses make users abandon the site.
Personal experience: Without proper concurrency handling, features like lotteries, sign‑ins, and point systems produce duplicate entries, extra points, or other logic errors.
2. Data Handling Under Concurrency
Use unique constraints in tables and wrap data operations in transactions to avoid chaos; apply server‑side locks to protect critical sections.
Example 1 – Table Design to Prevent Duplicate Sign‑Ins
Requirement: each user can sign in only once per day and earn points.
Solution: create a sign‑in record table with a unique index on (user_id, date). Insert the sign‑in record first, then add points, all inside a single SQL transaction.
Example 2 – Transaction + Update Lock for Lottery
Requirement: a lottery consumes one point, updates remaining prize count, and stops when points or prizes run out.
Solution: within a transaction, lock the prize row using
WITH (UPDLOCK)(or an UPDATE lock), deduct the user’s point, update the prize count, and commit; rollback on failure.
Example 3 – Code‑Level Lock for Cache Refresh
Requirement: cache refreshed at 10 am; many users may trigger the refresh simultaneously.
Problem: without protection, dozens of requests hit the DB at once.
Solution: in C# wrap the cache‑loading code with a
lockstatement so only one request fetches from the DB while others read the cache.
3. High‑Traffic Data‑Statistics API
Purpose: record product view counts for each user interaction.
Problem: a single page may generate tens of requests per scroll, leading to tens of thousands of requests under heavy load.
Solution: use a Node.js endpoint to push statistics into a Redis list, then run a background Node.js script that drains the list and persists batches to MySQL, sleeping when the list is empty.
4. Server Load Balancing and Deployment Strategies
Deploy Nginx as a reverse‑proxy load balancer across multiple servers.
Cluster MySQL, Redis, or MongoDB; offload static or rarely‑changed data to NoSQL caches.
Utilize caching layers (Redis, CDN) to reduce DB pressure.
Prefer high‑concurrency‑friendly languages (e.g., Node.js) for web APIs.
Separate image servers and serve static assets via CDN.
Optimize DB queries and indexes.
Use message queues (Redis list) for asynchronous persistence.
Implement client‑side throttling to avoid duplicate AJAX calls.
5. Recommended Concurrency Testing Tools
Apache JMeter
Microsoft Web Application Stress Tool
Visual Studio Load Test
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
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.