[{"data":1,"prerenderedAt":171},["ShallowReactive",2],{"term-s\u002Fsticky-session":3,"related-s\u002Fsticky-session":156},{"id":4,"title":5,"acronym":6,"body":7,"category":138,"description":139,"difficulty":140,"extension":141,"letter":142,"meta":143,"navigation":83,"path":144,"related":145,"seo":150,"sitemap":151,"stem":154,"subcategory":6,"__hash__":155},"terms\u002Fterms\u002Fs\u002Fsticky-session.md","Sticky Session",null,{"type":8,"value":9,"toc":132},"minimark",[10,15,19,23,26,30,121,125,128],[11,12,14],"h2",{"id":13},"eli5-the-vibe-check","ELI5 — The Vibe Check",[16,17,18],"p",{},"Sticky sessions make sure a user always gets routed to the SAME server, like getting the same cashier every time you visit a store. It's needed when your app stores session data in memory on one server. The downside: if that server goes down, the user's session is gone.",[11,20,22],{"id":21},"real-talk","Real Talk",[16,24,25],{},"Sticky sessions (session persistence or session affinity) configure a load balancer to route a client's requests to the same backend server for the duration of a session, typically using a cookie or IP hash. Required when session state is stored locally on servers rather than in a shared cache.",[11,27,29],{"id":28},"show-me-the-code","Show Me The Code",[31,32,37],"pre",{"className":33,"code":34,"language":35,"meta":36,"style":36},"language-nginx shiki shiki-themes material-theme-lighter material-theme material-theme-palenight","# Nginx sticky sessions with ip_hash\nupstream backend {\n  ip_hash;  # Same IP always goes to same server\n  server app1.example.com;\n  server app2.example.com;\n}\n\n# Better: use shared session storage instead\n# Store sessions in Redis, not server memory\napp.use(session({\n  store: new RedisStore({ client: redisClient }),\n  secret: 'your-secret'\n}));\n","nginx","",[38,39,40,48,54,60,66,72,78,85,91,97,103,109,115],"code",{"__ignoreMap":36},[41,42,45],"span",{"class":43,"line":44},"line",1,[41,46,47],{},"# Nginx sticky sessions with ip_hash\n",[41,49,51],{"class":43,"line":50},2,[41,52,53],{},"upstream backend {\n",[41,55,57],{"class":43,"line":56},3,[41,58,59],{},"  ip_hash;  # Same IP always goes to same server\n",[41,61,63],{"class":43,"line":62},4,[41,64,65],{},"  server app1.example.com;\n",[41,67,69],{"class":43,"line":68},5,[41,70,71],{},"  server app2.example.com;\n",[41,73,75],{"class":43,"line":74},6,[41,76,77],{},"}\n",[41,79,81],{"class":43,"line":80},7,[41,82,84],{"emptyLinePlaceholder":83},true,"\n",[41,86,88],{"class":43,"line":87},8,[41,89,90],{},"# Better: use shared session storage instead\n",[41,92,94],{"class":43,"line":93},9,[41,95,96],{},"# Store sessions in Redis, not server memory\n",[41,98,100],{"class":43,"line":99},10,[41,101,102],{},"app.use(session({\n",[41,104,106],{"class":43,"line":105},11,[41,107,108],{},"  store: new RedisStore({ client: redisClient }),\n",[41,110,112],{"class":43,"line":111},12,[41,113,114],{},"  secret: 'your-secret'\n",[41,116,118],{"class":43,"line":117},13,[41,119,120],{},"}));\n",[11,122,124],{"id":123},"when-youll-hear-this","When You'll Hear This",[16,126,127],{},"\"We need sticky sessions because the app stores state in memory.\" \u002F \"Replace sticky sessions with a Redis session store so any server can handle any user.\"",[129,130,131],"style",{},"html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":36,"searchDepth":50,"depth":50,"links":133},[134,135,136,137],{"id":13,"depth":50,"text":14},{"id":21,"depth":50,"text":22},{"id":28,"depth":50,"text":29},{"id":123,"depth":50,"text":124},"networking","Sticky sessions make sure a user always gets routed to the SAME server, like getting the same cashier every time you visit a store.","intermediate","md","s",{},"\u002Fterms\u002Fs\u002Fsticky-session",[146,147,148,149],"Load Balancer","Session","Round Robin","Redis",{"title":5,"description":139},{"changefreq":152,"priority":153},"weekly",0.7,"terms\u002Fs\u002Fsticky-session","v4xkodv5d5IamD_m9F9nS0-iXrrkqOTCcf69Yk7Iwm8",[157,160,164,168],{"title":146,"path":158,"acronym":6,"category":138,"difficulty":140,"description":159},"\u002Fterms\u002Fl\u002Fload-balancer","A load balancer is like a traffic cop for servers.",{"title":149,"path":161,"acronym":6,"category":162,"difficulty":140,"description":163},"\u002Fterms\u002Fr\u002Fredis","backend","Redis is an incredibly fast database that lives entirely in memory (RAM). It's used as a cache, a session store, and a message queue.",{"title":148,"path":165,"acronym":6,"category":138,"difficulty":166,"description":167},"\u002Fterms\u002Fr\u002Fround-robin","beginner","Round Robin is the simplest load balancing strategy: send request 1 to server A, request 2 to server B, request 3 to server C, then back to server A, and s...",{"title":147,"path":169,"acronym":6,"category":138,"difficulty":166,"description":170},"\u002Fterms\u002Fs\u002Fsession","A session is the server's way of remembering who you are across multiple requests.",1776518315543]