当前位置:网站首页>Four solutions of distributed session
Four solutions of distributed session
2022-07-26 21:32:00 【DC_ Zhang】
Server storage
Realization :session Copy (session Sync )
Is to synchronize these two servers session, For example, the server on the left saved a 1, The server on the right saved a 2, The two of them are synchronized , Then the server on the left saves 1,2, The server on the right also saves 1,2.
advantage :
- tomcat Native support , Just modify the configuration file , Many tomcat Can be copied between session
shortcoming
- session Synchronization requires data transmission through the network , There is a delay problem , At the same time, it will occupy a lot of bandwidth , This will compress the bandwidth of our entire business , It will reduce our processing capacity
If it is a large distributed cluster environment , Because of all web-server Save all data , So we don't use this scheme . And if it's a small system , Just 3/5 individual tomcat, If we want to use , It's OK to simply configure it
Client storage
Realization :
We let the client store it by itself session, What data does our server want to use , Read from the browser cookie that will do . This saves server resources
shortcoming
- Every time http request , Carry users in cookie Complete information in , Wasting network bandwidth
- session The data is placed in the browser cookie in , Some browsers follow different standards , Its length limit is different , Such as length limit 4k, Therefore, a large amount of information cannot be saved
- session The data is in cookie in , There is a leak 、 Tampering 、 Security risks such as theft
HASH Uniformity ( recommend )
principle :
Using our load balancing mechanism , We can use ip Hash consistency of , As long as it comes from the same ip Of , Then we will always locate it to the same server , We won't run it to the second server , Such as things stored in the green marked browser and the green marked server , No matter how many times you ask to come , Will fall on the green server , We can get
advantage
- Just change load balancing nginx Configuration of , Let it make a ip hash, Without modifying the application code
- The load is balanced : as long as hash The values of attributes are evenly distributed , More than one web-server The load is balanced
- Support web-server Horizontal expansion ( and session Copying the scheme is not enough , Limited by memory )
shortcoming
- session There is still web-server in , therefore web-server Suddenly flash off or restart , It may lead to part of session The loss of , These users only need to come back next time , All the data is gone , He needs to log in again , Everything has to be redone
- If our server wants to expand horizontally , If we fix it , But if it turns out to be 2 Servers , Now add 4 Servers , Now if you want to do hashing , It's equivalent to recalculating , Suppose we used to calculate hash in the simplest way , according to ip The address gets an integer hash , If only 2 Servers , Then it's OK to 2 Surplus operation , Find the remainder , If the remainder is 1, It fell to the first server , If there is no remainder , It will fall to the second server . But if it becomes 4 Servers , We are equivalent to being right 4 Carry out the remainder operation , If yu 1, Fall to the first server , more than 2 Fall to the second server , more than 3 To the third server , There is no remainder, we fall to the fourth server .( After horizontal expansion ,rehash after session Redistribute , Some users will not be able to route to the correct server )
Unified storage ( recommend )
Realization :
Then we can make session Unified storage , No matter which server you have , Which one? tomcat, Yours session Don't store it in your memory , All , Everyone can save to the database , perhaps redis Something faster nosql Middleware and so on , therefore , We can use this scheme
advantage
No potential safety hazard , It doesn't let the browser store it in cookie in , All data is stored in our background , The browser must be inaccessible , As long as we guarantee our backstage redis The safety of the , No one can tamper with the relevant data
Horizontal expansion is also easy , Whether we web How many servers ,10 individual ,100 individual ,1000 individual , Anyway, everyone goes redis Do access , Even if redis It's not enough , We do redis colony , Save a little in each , Save a little in each
Even if our server restarts 、 Downtime , Start again next time , We session It won't lose , because session All are redis There is , It has nothing to do with whether our business server is down
shortcoming
Getting data from memory is very fast , There is no need for network interaction , And if we store it redis Inside , We want to session Get data inside , We have to connect redis, Another network interaction
We need to modify the application code : If all the getSession Method is replaced by from Redis How to check the data
Link to the original text :https://mp.weixin.qq.com/s/wX5wmn8Wykxt4_bxzZrJ6Q
边栏推荐
- JVM learning - memory structure - program counter & virtual machine stack & local method stack & heap & method area
- Arm Mali GPU的噩梦:三星、华为纷纷转向自研!
- word-break: break-all VS word-wrap: break-word
- [HCIA security] NAT network address translation
- 一些意想不到的bug记录
- Deepfake pinches his face. It's hard to tell whether it's true or false. Tom Cruise is more like himself than himself!
- 在上传之前预览图像
- [HCIA security] user authentication
- 调整表格列的大小
- 华为发布2025十大趋势:5G、机器人、AI等上榜
猜你喜欢
![[HCIA security] user authentication](/img/6f/0ab6287eac36a4647dc2a0646ba969.png)
[HCIA security] user authentication

Mobile phone \ landline call forwarding setting method

From manual test to automatic test, it only took me a few months to double my salary

【HCIE安全】双机热备-主备备份

技术分享 | 服务端接口自动化测试, Requests 库的这些功能你了解吗?

留存收益率计算公式
![[HCIA security] NAT network address translation](/img/10/3b4d011963e00229d73a7435ce8c4b.png)
[HCIA security] NAT network address translation

JVM learning - memory structure - program counter & virtual machine stack & local method stack & heap & method area

Selenium自动化测试面试题全家桶

【Flutter -- GetX】弹框 - Dialog、Snackbar、BottomSheet
随机推荐
DeepFake捏脸真假难辨,汤姆·克鲁斯比本人还像本人!
contenteditable 元素的placeholder
记一次invalid bound statement xxxxxx 问题解决思路
2022-7-26 the seventh group of abstractions and interfaces
[HCIA security] bidirectional nat
Preview image before uploading
从手动测试,到自动化测试老司机,只用了几个月,我的薪资翻了一倍
Test cases should never be used casually, recording the thinking caused by the exception of a test case
一种用于实体关系抽取的统一标签空间
Arm Mali GPU的噩梦:三星、华为纷纷转向自研!
留存收益率计算公式
【HarmonyOS议题资料下载】HDD杭州站·线下沙龙专注应用创新 展现鸿蒙生态魅力
【HCIA安全】双向NAT
In depth study of efcore migrations
encodeURI VS encodeURIComponent
Registration conditions for information system project managers in the second half of 2022 (soft examination advanced)
Computer professional interview topic summary, general navigation
:active vs :focus
[virtual machine data recovery] data recovery of XenServer virtual machine unavailable due to unexpected power failure
拖放表格行