Java spike high concurrency summary

table of Contents

1. CDN static page acceleration

2.redis as backend cache

3. Perform a spike:

4. Bottleneck analysis:

5. Optimization analysis:

6. Optimization summary


Ideas to optimize the performance of the spike system:

Write to memory instead of hard disk, asynchronous processing instead of synchronous processing, distributed processing.

1. CDN static page acceleration

2.redis as backend cache

Redis optimized address exposure interface

Introduce a custom serialization tool (protostuff) to serialize java objects to redis.

3. Perform a spike:

Atomic counter---redis/nosql (incr limits the number of requests for a phone/session within a specified time)

Record behavior messages---distributed MQ

Consumption of news and landing---MySQL

4. Bottleneck analysis:

update reduce inventory rowLock

|(Network Delay/GC)

insert purchase details

|(Network Delay/GC)

commit/rollback freeLock

5. Optimization analysis:

Row-level locks are released after commit

  1. Optimization direction: Reduce row-level lock holding time.

  2. Put the client logic on the MYSQL server to avoid the influence of network delay and GC. (Use a stored procedure: the entire transaction is completed in MySQL).

Simple optimization

Replace the insert and update statements (insert will return the number of rows affected). Because it is only locked during update, putting the insert in front will reduce the row lock holding time.

Stored procedures replace java and Mysql logic procedures

--Second kill to execute the stored procedure DELIMITER $$ --console; Convert to $$ --Define the stored procedure - Parameters: in input parameters; out output parameters --row_cout(); return to the previous modification type sql(delete,insert, The number of rows affected by update)--row_count(); 0 has not been modified; >0 the number of modified rows; <0 sql error CREATE PROCEDURE  seckill.execute_seckill(in v_seckill bitint,in v_phone bigint, in v_kill_time timestamp,out r_result int) BEGIN DECLARE insert_cout int DEFAULT 0; START TRANSACTION; insert ignore into success_killed(seckill_id,user_phone,create_time) values (v_seckill_id,v_phone,v_kill_time); select row_count() into insert_count; IF(insert_count <0) THEN ROLLBACK; set r_resout = -1; ELSEIF(insert_count<0) THEN ROOLBACK; SET R_RESULT = -2; ELSE update seckill set number=number-1 where seckill_id=v_seckill_id and end_time> v_kill_time and start_time< v_kill_time and number>0 into insert_count; IF(inser_count==0) THEN ROLLBACK; set r_result = 0; ELSEIF(insert_count<0) THEN ROLLBACK; set r_result = -2; ELSE COMMIT; set r_result = 1; END IF; END IF; END; $$ DELIMITER ;

set @r_result=-3; call execute_seckill(1003,1350327,new(),@r_result);

select @r_result;

6. Optimization summary

Front-end control: exposed interface, key anti-duplication (jquery key anti-duplication one event)

Separation of dynamic and static data: CDN cache, back-end cache

Transaction competition optimization: less transaction lock time

Guess you like

Origin blog.csdn.net/sinat_37138973/article/details/85051553