Write it at the front

Recently I was reading a book from Daniel, the technical team of Taobao , Name is 《 Large website system and Java Middleware practice 》. The opening chapter details the evolution of a website architecture from small to large , In the process of upgrading from stand-alone architecture to cluster architecture, it focuses on session Synchronization problem , This is also a topic that many people can't get around when they talk about distributed . Let's sort out the contents of the book , It's a Book note , For future reference .

Where does the problem come from

do web Development students should session I can't be more familiar , It is the session ID assigned by the server to the client , Every time the browser asks, it will bring this identification to tell the server who I am , The server stores these different session information in memory , This is used to identify which session the request comes from . Total environments deployed on a stand-alone basis , because web The server and session It's all on the same machine , Therefore, the corresponding session data can be found . But if you have 2 platform web The server (A and B) Provide services , If the first request falls A And created session, So how to ensure the next time B Can read session data ?


There are the following 4 Common solutions in .

1、Session Sticky

This is the simplest Method , The core idea is to make all requests for the same session land on the same server , It's like a single machine , We can do some identification and control forwarding in load balancing to achieve this goal . The advantage of this is that it can simplify the session Handle , It is also convenient for local caching , But the disadvantages are obvious :

  • If this server goes down or restarts , Then all session data will be lost , Lost the high availability feature of distributed cluster .

  • Increased load balancer burden , Make it stateful , And it will consume more resources , Easy to become a performance bottleneck .

2、Session Replication

seeing the name of a thing one thinks of its function , This is a kind of session Replicated scenarios , The core idea is to increase the number of servers session Synchronization mechanism to ensure data consistency .

It looks a lot easier than the first one , And there's no first flaw , But there are still serious problems in some application scenarios :

  • Data synchronization between servers brings extra network consumption , As the number of machines and data increases , There will be a lot of pressure on network bandwidth , It will inevitably lead to delay problems .

  • All session data is stored on each server , If the number of sessions is large, most of the server's memory space will be occupied .

At present, many application containers support this synchronization method , So it is a good solution when the cluster size and data volume are relatively small .

3、Session Centralized storage

The idea of this method is to store and manage all session data in a unified way , All application servers need to session Read and write through session Server to operate :

The advantage of this scheme is independence session Management of , Single responsibility ,session How servers are stored ( Memory 、 database 、 file 、NoSql wait ), Any way to provide external services is transparent . No additional overhead for application system and load balancing , Consistency without data synchronization , It should look perfect , But there are also some small defects :

  • Yes session Network operation required for reading and writing , Comparison session Directly stored in web Server time increases latency and instability , Fortunately session The server and web The server is usually deployed in the local area network , Can minimize this problem .

  • session Server issues will affect all web service , If multi machine deployment is adopted, data consistency problems will also arise .

Each scheme has its unique advantages , At the same time, it will bring new problems , It's just that it's not perfect , Only fit is the best . On the whole , This scheme is very advantageous when both the application server and the session data volume are large .

4、Cookie Base

This solution is based on cookie The transmission of , The core idea is very simple , It is to write the complete session data to the client after processing cookie, In the future, the client will bring this with every request cookie, And then the server analyzes cookie Data to get session information , As shown in the figure below :

The plan is simple and clear , There are no problems caused by the previous schemes , But the disadvantages are obvious :

  • First, through cookie It's not safe to pass on critical data , Even with special encryption .

  • If the client is disabled cookie, Will directly cause the service to be unavailable .

  • cookie The data of is limited in size , If the data passed exceeds the limit size , Data exception will be caused .

  • stay http Carrying a large amount of data in the request for transmission will increase the network burden , Again , The server responds to a large amount of data, which will slow down the request , When the concurrency is large, it will be terrible .


above 4 All kinds of schemes are feasible , As mentioned earlier , Each scheme has its own advantages and disadvantages , Not perfect , In practical application, trade-offs and trade-offs should be made according to requirements . These are all common solutions , I believe there will be other problems in the real practice and landing process , Experienced people may have some new ways “ tricks ”, Welcome to the discussion .

Distributed web In the framework of session Synchronization of common processing methods and advantages and disadvantages of more related articles

  1. Apache shiro Cluster implementation ( 8、 ... and ) web When the cluster session synchronous 3 Methods

    Apache shiro Cluster implementation ( One ) shiro Introduction Apache shiro Cluster implementation ( Two ) shiro Of INI To configure Apache shiro Cluster implementation ( 3、 ... and )shiro Identity Authentication (Shiro ...

  2. 【 turn 】web When the cluster session synchronous 3 Methods

    Please indicate the author of the reprint : Seafloor hawk address :http://blog.51yip.com/server/922.html Doing the web After cluster , You'll think about it first session Synchronization problem , Because after load balancing , The same IP visit ...

  3. web When the cluster session synchronous 3 Methods [ turn ]

    Doing the web After cluster , You'll think about it first session Synchronization problem , Because after load balancing , The same IP Accessing the same page is assigned to different servers , If session Out of sync , A login user , One is login status , Not for a while ...

  4. web When the cluster session synchronous 3 Methods

    Doing the web After cluster , You'll think about it first session Synchronization problem , Because after load balancing , The same IP Accessing the same page is assigned to different servers , If session Out of sync , A login user , One is login status , Not for a while ...

  5. Introduce web Common techniques and methods for session tracking in development

    because http It's a stateless agreement , This feature seriously hinders the dynamic interaction between the client and the server , For example, the shopping cart program , The customer added items to the shopping cart , How does the server know what's in the cart ? In order to support the interaction between client and server , To make up for it http ...

  6. Software architecture design learning summary (19): Explain in detail the session Synchronization problem

    A few weeks ago , A basin friend asked Lao Wang , Now there are multiple servers , How to solve the problem between these servers session Synchronization problem ? Lao Wang's spirit will come soon , Because in n Years ago , When Lao Wang was still in school with a few classmates, he started a business together , We have encountered similar problems . I checked ...

  7. About web In conversation session Expiration time settings

    About web In conversation session Expiration time settings 1. operating system : step : Start ——〉 Management tools ——〉Internet information service (IIS) Manager ——〉 Website ——〉 The default site ——〉 Right click “ attribute ”——〉 Home directory ——〉 To configure — ...

  8. ASP.NET Web Service Use in Session And Session Lost solutions To continue

    original text :ASP.NET Web Service Use in Session And Session Lost solutions To continue 1. About Session Summary of missing problem descriptions , Reference here 2. stay Web Servcie Use in Sessio ...

  9. javaEE Used in development session Synchronization and token Mechanism to prevent concurrent duplicate submission

    javaEE Used in development session Synchronization and token Mechanism to prevent concurrent duplicate submission Usually in normal operation , We don't have to deal with duplicate submissions , And there are many ways to prevent duplicate submissions . For example, during the landing process , By using redirect, can ...

Random recommendation

  1. Test development interview -java Continuous accumulation

    The difference between interface and abstract class Yes java Thread understanding Yes java Understanding of concurrency webservice Characteristics , use webservice Why Daemons and non daemons Implementation of single case , Single case concurrent How to implement and define a class , Only implement any of the interfaces ...

  2. discuss js String array splicing performance problems

    This article mainly introduces about js Performance problems of string array splicing , String concatenation has always been js One of the lowest neutral operations , How to solve it ? Please refer to the introduction of this article We know , stay js in , String concatenation is one of the lowest performing operations . for example : Copy ...

  3. Use exe4j pack Java Program

    Tools : exe4j Software ( Cloud disk storage ) It can run Java programmatic jar package Open what we have installed exe4j Software , The first thing you see is a welcome screen , We directly [next] That's all right. : 2 In the second step, we choose [JAR in ...

  4. GWT Download and install

    1. Get ready install jdk: Download path :http://download.oracle.com/otn-pub/java/jdk/8u92-b14/jdk-8u92-windows-i586.exe?Aut ...

  5. json Value transfer and ajax receive

    In the beginning, I entered the company , I'm at a loss to start a project , Now a month has passed , More and more handy , Here is json Forward value and front end ajax receive , Write it down for yourself and share it with you . These are two different types of json And ajax Data interaction ( aggregate . ...

  6. BTrace: DTrace for Java2

    BTrace: DTrace for Java… ish Time  2012-04-24 16:17:55  dtrace.org original text   http://dtrace.org/blogs/ahl/2012 ...

  7. JS Advanced programming learning notes Chapter 3 basic concepts ( grammar , data type , Flow control statement , function )—— Leak filling

    One . grammar : Case sensitive ; 2. identifier : It means variable . function . Property name , Or the parameters of the function a. The rules of the flag :① The first character must be a letter . Underline (_) Or a dollar sign ($).                   ...

  8. 5、 ... and : springMVC Interceptor

    springMVC Interceptors are generally implemented in two ways The first way is to define Interceptor Class is about to be implemented Spring Of HandlerInterceptor Interface The second way is to inherit and realize HandlerInte ...

  9. Little turtle OD Learning the second 1 speak

    Our goal in this lecture is to revise Hello.exe The title and content of the dialog , As shown in the figure Put the program in OD, Press F8 Step by step , Until the dialog box pops up , Then, set the breakpoint at the position of the pop-up dialog box Press Ctrl+F2 Reload the program , Then press F9 straight ...

  10. Console program (C#) Don't pop up an authentication window to connect to Dynamics CRM Online Of Web API

    Abstract : My official account is WeChat and Yi Shun. : Microsoft News CRM Expert Luo Yong , reply 271 perhaps 20180602 This article is easy to access , At the same time, I can get the latest blog information in the first room ,follow me! My website is www.luoyon ...