当前位置:网站首页>Architecture solutions

Architecture solutions

2022-06-24 01:00:00 ruochen

1、 Local cache 、 Out of heap memory off-heap、

3、redis (jedis cluster Of sharding jedisCluster Reading and writing lettuce Read / write separation )

4、 Multi level cache ( atypism Local cache update policy Penetrating thinking )

5、 Deduct inventory problems ( Row lock redis Optimism lock redis+lua Sync to db alisql)

One 、 Local cache

shortcoming : Occupy system memory oom、 There are consistency problems , When the distributed cache has a single point problem , In combination with

example :hibernate For caching Ehcatch, Yes jdbc encapsulation

Two 、 Out of heap memory off-heap

Avoid hot data , Occupy system memory

1、 advantage : 1) Reduce gc frequency Reduce pause time 2) Expand and use more memory 3) Eliminate physical memory and heap Copy between

2、 TaoBao jdk Realization : Long life objects from heap Move in and out , gc Unable to manage .ps: Escape analysis technology is mature , It can also be allocated on the stack

3、 How to use physical memory : Capacity can be limited , beyond oom

4、 When to release

DirectByteBuffer The object is gc when , The off heap memory is released together

3、 ... and 、redis

The local cache cannot be expanded ,cluster Capacity can be extended indefinitely

1、 be based on jedis Client operation :

2、cluster Of sharding

1)hash Algorithm : Capacity expansion , All historical data should be migrated

2) Uniformity hash Algorithm : No need to migrate all , but node When you are young , Meeting Data skew , At the node ip/ Add... After the host name Number , Let it be evenly distributed

3) Slotting : In between ,slot Fix , Always be routed to the same

3、 be based on jedisCluster Read and write operations

A single node can be specified

4、 be based on lettuce Read / write separation of the client

jedis Cluster read / write separation is not supported ,lettuce Can be synchronized / asynchronous , Bottom based nio Model netty

advantage : Levels increase , Infinite extension , There is no need to manually adjust the connection maxTotal, Avoid local cache penetration

Four 、 Multi level cache

Local : Share in-process heap, Save hotspot data

cdn: Save product pictures 、 video

1. Inconsistency

Due to time difference , allow Dirty reading , Display sold out when deducting inventory

2. Local cache update policy ( Lord / passive )

1) Passive update : Return to the source after expiration , Ensure single thread , Avoid invalidating a large number of requests , Penetration causes avalanches

guava

2) Active update : After modification , Write to the queue asynchronously , Update cache

3、 Cache penetration thinking

Get familiar with the hot spots from the operation before the promotion key, In the configuration center

5、 ... and 、 Deduct inventory problems

1、 Row lock

In the table version, Traffic spikes cause a large number of threads to compete for row locks , influence db tps,rt rising , Cause an avalanche

2、redis Optimism lock

(1) Basic commands

1) watch : monitor key( Can be more ), Before the event key Change change , Transaction rule Failure

2) multi : Set multiple items in a transaction command , In order of precedence Put it in line

3) exec : Finally atomicity Submit for execution

(2)watch Internal implementation principles

How the client perceives : Every db All are redis.h/redis.db Structural representation , Since the internal storage watch_keys( The target being monitored key)

multi.c/touchWatchKey function Yes watch_keys Dictionary check

, There are changes key Marked as redis_dirty_cas, Subsequent commit transactions are interrupted

(3)jedis Optimism lock

(4) The default routing implementation

shortcoming : The more concurrent ,watch The greater the probability of collision , solve : Reading and writing compose one command , The embedded lua Script

3、redis+lua ( Best performance )

redis2.6 after , built-in lua Interpreter , but eval / evalsha When the order is executed ,redis Make it a A single item is in execution

(1)lua Script

(2)eval / evalsha The embedded redis perform

1) eval: Repeat to redis Same transmission lua Script , Network overhead is high

2) evalsha : from redis Get cached scripts , save . But with evalsha front , First use script load The command to load

lua To cache in , etc. redis Will wait sha1 Check code , Follow up time , Just send the check code

use evalsha perform lua Script

4、 How to synchronize inventory changes to db

synchronized: It is better to use it in the fierce competition

cas:https://www.jianshu.com/p/6d1f3b2a3ac0 Will retry indefinitely , Take up too much cpu

5、AliSQL

Do... For hot data hash, Collect all inventory and submit at one time ( Set the threshold )

原网站

版权声明
本文为[ruochen]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/11/20211121160438065i.html

随机推荐