[{"data":1,"prerenderedAt":1748},["ShallowReactive",2],{"blog-en-session-cookies-vs-jwt":3,"blog-related-en-session-cookies-vs-jwt":223},{"id":4,"title":5,"author":6,"body":7,"date":204,"description":205,"extension":206,"featured":207,"image":208,"meta":209,"navigation":210,"path":211,"readingTime":212,"seo":213,"slug":214,"stem":215,"tags":216,"__hash__":222},"blog\u002Fblog\u002Fen\u002Fsession-cookies-vs-jwt.md","Session Cookies vs. JWTs in 2026: Why the Industry is Moving Back to Stateful Sessions","Adam Abdel-Djamal",{"type":8,"value":9,"toc":194},"minimark",[10,18,26,29,32,37,40,43,62,70,81,83,87,114,116,120,123,133,138,174,176,180,187],[11,12,13,14],"p",{},"A decade ago, the tech industry fell in love with JSON Web Tokens (JWTs). The promise was irresistible: ",[15,16,17],"em",{},"\"Stateless authentication! No database lookups on every request! Infinite scalability!\"",[11,19,20,21,25],{},"Yet in 2026, tech leaders and senior architects across GitHub, Stripe, and modern SaaS startups are actively migrating back to ",[22,23,24],"strong",{},"stateful, cookie-backed sessions",".",[11,27,28],{},"Why did the stateless dream fail in practice, and why are stateful sessions the superior architecture for 95% of web applications?",[30,31],"hr",{},[33,34,36],"h2",{"id":35},"_1-the-myth-of-the-stateless-jwt","1. The Myth of the \"Stateless\" JWT",[11,38,39],{},"A JWT works by having the server cryptographically sign a payload containing user identity and permissions. Any server that knows the secret key can verify the signature without hitting a database.",[11,41,42],{},"That works beautifully — until you need to answer basic security questions:",[44,45,46,52,57],"ol",{},[47,48,49],"li",{},[22,50,51],{},"How do you immediately log out a user when their password is changed?",[47,53,54],{},[22,55,56],{},"How do you instantly revoke access when an employee is fired or a token is leaked?",[47,58,59],{},[22,60,61],{},"How do you invalidate sessions across all devices?",[11,63,64,65,69],{},"With pure stateless JWTs, you cannot. A token remains valid until its expiration time (",[66,67,68],"code",{},"exp",") elapses. If a user sets a 7-day token, a compromised token can access your API for 7 days.",[11,71,72,73,76,77,80],{},"To solve this, developers invent ",[22,74,75],{},"token blacklists in Redis",". But the moment you query Redis on every request to check if a token is blacklisted, ",[22,78,79],{},"your architecture is no longer stateless",". You now have all the operational complexity of stateful sessions, but with none of the benefits.",[30,82],{},[33,84,86],{"id":85},"_2-token-bloat-vs-lean-session-ids","2. Token Bloat vs. Lean Session IDs",[88,89,90,100],"ul",{},[47,91,92,95,96,99],{},[22,93,94],{},"JWT Size",": An encoded JWT with claims and scopes typically weighs ",[22,97,98],{},"800 to 2,500 bytes",". Sent on every HTTP request and API header, this adds significant bandwidth overhead, especially on mobile networks.",[47,101,102,105,106,109,110,113],{},[22,103,104],{},"Session Cookie Size",": A cryptographically random UUID session ID weighs ",[22,107,108],{},"36 bytes"," (",[66,111,112],{},"session=c4847a61-18f1-4ace-afbd-7d7f50532b33",").",[30,115],{},[33,117,119],{"id":118},"_3-the-modern-stateful-session-stack","3. The Modern Stateful Session Stack",[11,121,122],{},"The gold standard for modern web applications in 2026 looks like this:",[124,125,131],"pre",{"className":126,"code":128,"language":129,"meta":130},[127],"language-text","Browser                           Backend API                       Redis Store\n   │                                   │                                 │\n   ├────── Request with Cookie ───────►│                                 │\n   │  (HttpOnly, Secure, SameSite)     ├───── MGET session:c484... ─────►│\n   │                                   │◄──── { userId, role, orgId } ───┤ (\u003C 0.5ms)\n   │◄───── Valid Response ─────────────┤                                 │\n","text","",[66,132,128],{"__ignoreMap":130},[134,135,137],"h3",{"id":136},"key-properties","Key Properties:",[88,139,140,152,164],{},[47,141,142,147,148,151],{},[22,143,144],{},[66,145,146],{},"HttpOnly",": JavaScript cannot access the session cookie (",[66,149,150],{},"document.cookie","), completely immunizing your auth tokens against XSS theft.",[47,153,154,163],{},[22,155,156,159,160],{},[66,157,158],{},"SameSite=Lax"," or ",[66,161,162],{},"Strict",": The browser never sends the cookie on cross-site requests, neutralizing CSRF attacks by default.",[47,165,166,169,170,173],{},[22,167,168],{},"Instant Revocation",": Want to revoke a session? ",[66,171,172],{},"redis.del(\"session:\" + sessionId)"," executes in 0.2ms. The user is logged out immediately.",[30,175],{},[33,177,179],{"id":178},"when-do-jwts-make-sense","When DO JWTs Make Sense?",[11,181,182,183,186],{},"JWTs are not inherently bad; they are just misused for browser session management. JWTs excel when used as ",[22,184,185],{},"short-lived (5-15 minute) bearer tokens between distinct microservices",", or for decentralized single-sign-on (OpenID Connect).",[11,188,189,190,193],{},"For user authentication in web apps, ",[22,191,192],{},"HttpOnly session cookies backed by Redis"," remain faster, safer, and infinitely easier to manage.",{"title":130,"searchDepth":195,"depth":195,"links":196},2,[197,198,199,203],{"id":35,"depth":195,"text":36},{"id":85,"depth":195,"text":86},{"id":118,"depth":195,"text":119,"children":200},[201],{"id":136,"depth":202,"text":137},3,{"id":178,"depth":195,"text":179},"2026-09-08","Discussing the trade-offs: why the complexity of token invalidation, refresh token rotation, and XSS risks is pushing modern SaaS back to HttpOnly server sessions with Redis.","md",false,null,{},true,"\u002Fblog\u002Fen\u002Fsession-cookies-vs-jwt",7,{"title":5,"description":205},"session-cookies-vs-jwt","blog\u002Fen\u002Fsession-cookies-vs-jwt",[217,218,219,220,221],"security","auth","backend","architecture","redis","HpAab_X_au9R3etsJ1lnj2797wyfGw9_AE1ATvsgKiY",[224,1081],{"id":225,"title":226,"author":6,"body":227,"date":1070,"description":1071,"extension":206,"featured":207,"image":208,"meta":1072,"navigation":210,"path":1073,"readingTime":372,"seo":1074,"slug":208,"stem":1075,"tags":1076,"__hash__":1080},"blog\u002Fblog\u002Fen\u002Fapi-common-mistakes.md","6 Common API Mistakes (And How to Fix Them)",{"type":8,"value":228,"toc":1061},[229,232,234,238,249,286,307,309,313,320,413,418,538,540,544,547,607,610,612,616,619,757,764,766,770,773,952,955,957,961,964,998,1005,1007,1011,1014,1057],[11,230,231],{},"Building an API feels straightforward until your first production incident. Most bugs aren't complex — they're the same six mistakes repeated across thousands of codebases. Here's what they are and how to stop making them.",[30,233],{},[33,235,237],{"id":236},"_1-using-the-wrong-http-method","1. Using the Wrong HTTP Method",[11,239,240,241,244,245,248],{},"The most visible mistake. Using ",[66,242,243],{},"GET"," to delete a resource, or ",[66,246,247],{},"POST"," for everything because \"it works.\"",[124,250,254],{"className":251,"code":252,"language":253,"meta":130,"style":130},"language-http shiki shiki-themes github-light github-dark","# ❌ Deleting with GET — catastrophic if a browser prefetches this URL\nGET \u002Fapi\u002Fusers\u002F42\u002Fdelete\n\n# ✅ Correct\nDELETE \u002Fapi\u002Fusers\u002F42\n","http",[66,255,256,264,269,274,280],{"__ignoreMap":130},[257,258,261],"span",{"class":259,"line":260},"line",1,[257,262,263],{},"# ❌ Deleting with GET — catastrophic if a browser prefetches this URL\n",[257,265,266],{"class":259,"line":195},[257,267,268],{},"GET \u002Fapi\u002Fusers\u002F42\u002Fdelete\n",[257,270,271],{"class":259,"line":202},[257,272,273],{"emptyLinePlaceholder":210},"\n",[257,275,277],{"class":259,"line":276},4,[257,278,279],{},"# ✅ Correct\n",[257,281,283],{"class":259,"line":282},5,[257,284,285],{},"DELETE \u002Fapi\u002Fusers\u002F42\n",[11,287,288,289,291,292,294,295,298,299,302,303,306],{},"HTTP methods carry semantic meaning that clients, proxies, and caches rely on. ",[66,290,243],{}," must be idempotent and safe (no side effects). ",[66,293,247],{}," creates. ",[66,296,297],{},"PUT","\u002F",[66,300,301],{},"PATCH"," update. ",[66,304,305],{},"DELETE"," removes. Violating these conventions breaks caching, browser history, and every HTTP client that follows the spec.",[30,308],{},[33,310,312],{"id":311},"_2-wrong-status-codes","2. Wrong Status Codes",[11,314,315,316,319],{},"Returning ",[66,317,318],{},"200 OK"," for an error response is the most common mistake in junior APIs.",[124,321,325],{"className":322,"code":323,"language":324,"meta":130,"style":130},"language-json shiki shiki-themes github-light github-dark","\u002F\u002F ❌ Status 200, but it's an error\n{\n  \"success\": false,\n  \"error\": \"User not found\"\n}\n\n\u002F\u002F ✅ Status 404 — the client knows immediately without parsing the body\n{\n  \"error\": \"User not found\",\n  \"code\": \"USER_NOT_FOUND\"\n}\n","json",[66,326,327,333,339,354,365,370,375,380,385,397,408],{"__ignoreMap":130},[257,328,329],{"class":259,"line":260},[257,330,332],{"class":331},"sJ8bj","\u002F\u002F ❌ Status 200, but it's an error\n",[257,334,335],{"class":259,"line":195},[257,336,338],{"class":337},"sVt8B","{\n",[257,340,341,345,348,351],{"class":259,"line":202},[257,342,344],{"class":343},"sj4cs","  \"success\"",[257,346,347],{"class":337},": ",[257,349,350],{"class":343},"false",[257,352,353],{"class":337},",\n",[257,355,356,359,361],{"class":259,"line":276},[257,357,358],{"class":343},"  \"error\"",[257,360,347],{"class":337},[257,362,364],{"class":363},"sZZnC","\"User not found\"\n",[257,366,367],{"class":259,"line":282},[257,368,369],{"class":337},"}\n",[257,371,373],{"class":259,"line":372},6,[257,374,273],{"emptyLinePlaceholder":210},[257,376,377],{"class":259,"line":212},[257,378,379],{"class":331},"\u002F\u002F ✅ Status 404 — the client knows immediately without parsing the body\n",[257,381,383],{"class":259,"line":382},8,[257,384,338],{"class":337},[257,386,388,390,392,395],{"class":259,"line":387},9,[257,389,358],{"class":343},[257,391,347],{"class":337},[257,393,394],{"class":363},"\"User not found\"",[257,396,353],{"class":337},[257,398,400,403,405],{"class":259,"line":399},10,[257,401,402],{"class":343},"  \"code\"",[257,404,347],{"class":337},[257,406,407],{"class":363},"\"USER_NOT_FOUND\"\n",[257,409,411],{"class":259,"line":410},11,[257,412,369],{"class":337},[11,414,415],{},[22,416,417],{},"The codes that matter most:",[419,420,421,434],"table",{},[422,423,424],"thead",{},[425,426,427,431],"tr",{},[428,429,430],"th",{},"Code",[428,432,433],{},"When to use it",[435,436,437,448,458,468,478,488,498,508,518,528],"tbody",{},[425,438,439,445],{},[440,441,442],"td",{},[66,443,444],{},"200",[440,446,447],{},"Success with a body",[425,449,450,455],{},[440,451,452],{},[66,453,454],{},"201",[440,456,457],{},"Resource created (POST)",[425,459,460,465],{},[440,461,462],{},[66,463,464],{},"204",[440,466,467],{},"Success, no body (DELETE)",[425,469,470,475],{},[440,471,472],{},[66,473,474],{},"400",[440,476,477],{},"Invalid input from the client",[425,479,480,485],{},[440,481,482],{},[66,483,484],{},"401",[440,486,487],{},"Not authenticated",[425,489,490,495],{},[440,491,492],{},[66,493,494],{},"403",[440,496,497],{},"Authenticated but not authorized",[425,499,500,505],{},[440,501,502],{},[66,503,504],{},"404",[440,506,507],{},"Resource doesn't exist",[425,509,510,515],{},[440,511,512],{},[66,513,514],{},"422",[440,516,517],{},"Input syntactically valid but semantically wrong",[425,519,520,525],{},[440,521,522],{},[66,523,524],{},"429",[440,526,527],{},"Rate limit exceeded",[425,529,530,535],{},[440,531,532],{},[66,533,534],{},"500",[440,536,537],{},"Server error (never the client's fault)",[30,539],{},[33,541,543],{"id":542},"_3-trusting-client-input","3. Trusting Client Input",[11,545,546],{},"Every value sent by a client is a potential attack vector.",[124,548,552],{"className":549,"code":550,"language":551,"meta":130,"style":130},"language-php shiki shiki-themes github-light github-dark","\u002F\u002F ❌ Never do this\n$id = $_GET['id'];\n$query = \"SELECT * FROM users WHERE id = $id\";\n\u002F\u002F SQL injection: ?id=1 OR 1=1\n\n\u002F\u002F ✅ Always validate and sanitize\n$id = filter_var($_GET['id'], FILTER_VALIDATE_INT);\nif (!$id) {\n    return response()->json(['error' => 'Invalid ID'], 400);\n}\n$user = User::findOrFail($id); \u002F\u002F parameterized query\n","php",[66,553,554,559,564,569,574,578,583,588,593,598,602],{"__ignoreMap":130},[257,555,556],{"class":259,"line":260},[257,557,558],{},"\u002F\u002F ❌ Never do this\n",[257,560,561],{"class":259,"line":195},[257,562,563],{},"$id = $_GET['id'];\n",[257,565,566],{"class":259,"line":202},[257,567,568],{},"$query = \"SELECT * FROM users WHERE id = $id\";\n",[257,570,571],{"class":259,"line":276},[257,572,573],{},"\u002F\u002F SQL injection: ?id=1 OR 1=1\n",[257,575,576],{"class":259,"line":282},[257,577,273],{"emptyLinePlaceholder":210},[257,579,580],{"class":259,"line":372},[257,581,582],{},"\u002F\u002F ✅ Always validate and sanitize\n",[257,584,585],{"class":259,"line":212},[257,586,587],{},"$id = filter_var($_GET['id'], FILTER_VALIDATE_INT);\n",[257,589,590],{"class":259,"line":382},[257,591,592],{},"if (!$id) {\n",[257,594,595],{"class":259,"line":387},[257,596,597],{},"    return response()->json(['error' => 'Invalid ID'], 400);\n",[257,599,600],{"class":259,"line":399},[257,601,369],{},[257,603,604],{"class":259,"line":410},[257,605,606],{},"$user = User::findOrFail($id); \u002F\u002F parameterized query\n",[11,608,609],{},"Validate: type, format, length, range. Sanitize before any database or filesystem operation. Never trust what arrives in the request body, headers, or query string.",[30,611],{},[33,613,615],{"id":614},"_4-leaking-sensitive-data","4. Leaking Sensitive Data",[11,617,618],{},"Returning the full database object without filtering is an invitation to a data breach.",[124,620,622],{"className":322,"code":621,"language":324,"meta":130,"style":130},"\u002F\u002F ❌ Returning the raw User model\n{\n  \"id\": 42,\n  \"email\": \"user@example.com\",\n  \"password\": \"$2b$12$...\",\n  \"stripe_customer_id\": \"cus_xxx\",\n  \"internal_notes\": \"flagged for review\",\n  \"created_at\": \"2024-01-01\"\n}\n\n\u002F\u002F ✅ Return only what the client actually needs\n{\n  \"id\": 42,\n  \"email\": \"user@example.com\",\n  \"created_at\": \"2024-01-01\"\n}\n",[66,623,624,629,633,645,657,669,681,693,703,707,711,716,721,732,743,752],{"__ignoreMap":130},[257,625,626],{"class":259,"line":260},[257,627,628],{"class":331},"\u002F\u002F ❌ Returning the raw User model\n",[257,630,631],{"class":259,"line":195},[257,632,338],{"class":337},[257,634,635,638,640,643],{"class":259,"line":202},[257,636,637],{"class":343},"  \"id\"",[257,639,347],{"class":337},[257,641,642],{"class":343},"42",[257,644,353],{"class":337},[257,646,647,650,652,655],{"class":259,"line":276},[257,648,649],{"class":343},"  \"email\"",[257,651,347],{"class":337},[257,653,654],{"class":363},"\"user@example.com\"",[257,656,353],{"class":337},[257,658,659,662,664,667],{"class":259,"line":282},[257,660,661],{"class":343},"  \"password\"",[257,663,347],{"class":337},[257,665,666],{"class":363},"\"$2b$12$...\"",[257,668,353],{"class":337},[257,670,671,674,676,679],{"class":259,"line":372},[257,672,673],{"class":343},"  \"stripe_customer_id\"",[257,675,347],{"class":337},[257,677,678],{"class":363},"\"cus_xxx\"",[257,680,353],{"class":337},[257,682,683,686,688,691],{"class":259,"line":212},[257,684,685],{"class":343},"  \"internal_notes\"",[257,687,347],{"class":337},[257,689,690],{"class":363},"\"flagged for review\"",[257,692,353],{"class":337},[257,694,695,698,700],{"class":259,"line":382},[257,696,697],{"class":343},"  \"created_at\"",[257,699,347],{"class":337},[257,701,702],{"class":363},"\"2024-01-01\"\n",[257,704,705],{"class":259,"line":387},[257,706,369],{"class":337},[257,708,709],{"class":259,"line":399},[257,710,273],{"emptyLinePlaceholder":210},[257,712,713],{"class":259,"line":410},[257,714,715],{"class":331},"\u002F\u002F ✅ Return only what the client actually needs\n",[257,717,719],{"class":259,"line":718},12,[257,720,338],{"class":337},[257,722,724,726,728,730],{"class":259,"line":723},13,[257,725,637],{"class":343},[257,727,347],{"class":337},[257,729,642],{"class":343},[257,731,353],{"class":337},[257,733,735,737,739,741],{"class":259,"line":734},14,[257,736,649],{"class":343},[257,738,347],{"class":337},[257,740,654],{"class":363},[257,742,353],{"class":337},[257,744,746,748,750],{"class":259,"line":745},15,[257,747,697],{"class":343},[257,749,347],{"class":337},[257,751,702],{"class":363},[257,753,755],{"class":259,"line":754},16,[257,756,369],{"class":337},[11,758,759,760,763],{},"Use serializers, DTOs, or API Resources (Laravel) to explicitly define what gets exposed. The rule: ",[22,761,762],{},"opt-in, not opt-out."," Start with nothing, add what's needed.",[30,765],{},[33,767,769],{"id":768},"_5-inconsistent-error-format","5. Inconsistent Error Format",[11,771,772],{},"When every endpoint returns errors differently, every client has to handle every format.",[124,774,776],{"className":322,"code":775,"language":324,"meta":130,"style":130},"\u002F\u002F ❌ Endpoint A\n{ \"message\": \"Not found\" }\n\n\u002F\u002F ❌ Endpoint B\n{ \"error\": true, \"msg\": \"Validation failed\", \"fields\": [\"email\"] }\n\n\u002F\u002F ❌ Endpoint C\n\"Internal server error\"\n\n\u002F\u002F ✅ One format, everywhere\n{\n  \"error\": {\n    \"code\": \"VALIDATION_FAILED\",\n    \"message\": \"The request contains invalid fields.\",\n    \"details\": [\n      { \"field\": \"email\", \"issue\": \"Invalid email format\" }\n    ]\n  }\n}\n",[66,777,778,783,799,803,808,845,849,854,859,863,868,872,879,891,903,911,935,941,947],{"__ignoreMap":130},[257,779,780],{"class":259,"line":260},[257,781,782],{"class":331},"\u002F\u002F ❌ Endpoint A\n",[257,784,785,788,791,793,796],{"class":259,"line":195},[257,786,787],{"class":337},"{ ",[257,789,790],{"class":343},"\"message\"",[257,792,347],{"class":337},[257,794,795],{"class":363},"\"Not found\"",[257,797,798],{"class":337}," }\n",[257,800,801],{"class":259,"line":202},[257,802,273],{"emptyLinePlaceholder":210},[257,804,805],{"class":259,"line":276},[257,806,807],{"class":331},"\u002F\u002F ❌ Endpoint B\n",[257,809,810,812,815,817,820,823,826,828,831,833,836,839,842],{"class":259,"line":282},[257,811,787],{"class":337},[257,813,814],{"class":343},"\"error\"",[257,816,347],{"class":337},[257,818,819],{"class":343},"true",[257,821,822],{"class":337},", ",[257,824,825],{"class":343},"\"msg\"",[257,827,347],{"class":337},[257,829,830],{"class":363},"\"Validation failed\"",[257,832,822],{"class":337},[257,834,835],{"class":343},"\"fields\"",[257,837,838],{"class":337},": [",[257,840,841],{"class":363},"\"email\"",[257,843,844],{"class":337},"] }\n",[257,846,847],{"class":259,"line":372},[257,848,273],{"emptyLinePlaceholder":210},[257,850,851],{"class":259,"line":212},[257,852,853],{"class":331},"\u002F\u002F ❌ Endpoint C\n",[257,855,856],{"class":259,"line":382},[257,857,858],{"class":363},"\"Internal server error\"\n",[257,860,861],{"class":259,"line":387},[257,862,273],{"emptyLinePlaceholder":210},[257,864,865],{"class":259,"line":399},[257,866,867],{"class":331},"\u002F\u002F ✅ One format, everywhere\n",[257,869,870],{"class":259,"line":410},[257,871,338],{"class":337},[257,873,874,876],{"class":259,"line":718},[257,875,358],{"class":343},[257,877,878],{"class":337},": {\n",[257,880,881,884,886,889],{"class":259,"line":723},[257,882,883],{"class":343},"    \"code\"",[257,885,347],{"class":337},[257,887,888],{"class":363},"\"VALIDATION_FAILED\"",[257,890,353],{"class":337},[257,892,893,896,898,901],{"class":259,"line":734},[257,894,895],{"class":343},"    \"message\"",[257,897,347],{"class":337},[257,899,900],{"class":363},"\"The request contains invalid fields.\"",[257,902,353],{"class":337},[257,904,905,908],{"class":259,"line":745},[257,906,907],{"class":343},"    \"details\"",[257,909,910],{"class":337},": [\n",[257,912,913,916,919,921,923,925,928,930,933],{"class":259,"line":754},[257,914,915],{"class":337},"      { ",[257,917,918],{"class":343},"\"field\"",[257,920,347],{"class":337},[257,922,841],{"class":363},[257,924,822],{"class":337},[257,926,927],{"class":343},"\"issue\"",[257,929,347],{"class":337},[257,931,932],{"class":363},"\"Invalid email format\"",[257,934,798],{"class":337},[257,936,938],{"class":259,"line":937},17,[257,939,940],{"class":337},"    ]\n",[257,942,944],{"class":259,"line":943},18,[257,945,946],{"class":337},"  }\n",[257,948,950],{"class":259,"line":949},19,[257,951,369],{"class":337},[11,953,954],{},"Define your error envelope once. Apply it everywhere via a global exception handler. Clients — including your own frontend — will thank you.",[30,956],{},[33,958,960],{"id":959},"_6-no-api-versioning","6. No API Versioning",[11,962,963],{},"Changing an endpoint response breaks every client using it. Without versioning, you can never evolve the API.",[124,965,967],{"className":251,"code":966,"language":253,"meta":130,"style":130},"# ❌ No version — any breaking change affects all clients immediately\nGET \u002Fapi\u002Fusers\n\n# ✅ Versioned — v1 stays stable, v2 introduces breaking changes safely\nGET \u002Fapi\u002Fv1\u002Fusers\nGET \u002Fapi\u002Fv2\u002Fusers\n",[66,968,969,974,979,983,988,993],{"__ignoreMap":130},[257,970,971],{"class":259,"line":260},[257,972,973],{},"# ❌ No version — any breaking change affects all clients immediately\n",[257,975,976],{"class":259,"line":195},[257,977,978],{},"GET \u002Fapi\u002Fusers\n",[257,980,981],{"class":259,"line":202},[257,982,273],{"emptyLinePlaceholder":210},[257,984,985],{"class":259,"line":276},[257,986,987],{},"# ✅ Versioned — v1 stays stable, v2 introduces breaking changes safely\n",[257,989,990],{"class":259,"line":282},[257,991,992],{},"GET \u002Fapi\u002Fv1\u002Fusers\n",[257,994,995],{"class":259,"line":372},[257,996,997],{},"GET \u002Fapi\u002Fv2\u002Fusers\n",[11,999,1000,1001,1004],{},"Version from day one, even if you only have v1. The cost of adding ",[66,1002,1003],{},"\u002Fv1\u002F"," later — after clients are in production — is much higher than the cost of including it from the start.",[30,1006],{},[33,1008,1010],{"id":1009},"the-takeaway","The Takeaway",[11,1012,1013],{},"None of these mistakes require advanced knowledge to fix. They require discipline: a checklist before every endpoint ships.",[88,1015,1018,1027,1033,1039,1045,1051],{"className":1016},[1017],"contains-task-list",[47,1019,1022,1026],{"className":1020},[1021],"task-list-item",[1023,1024],"input",{"disabled":210,"type":1025},"checkbox"," HTTP method matches the operation's semantics",[47,1028,1030,1032],{"className":1029},[1021],[1023,1031],{"disabled":210,"type":1025}," Status code is accurate",[47,1034,1036,1038],{"className":1035},[1021],[1023,1037],{"disabled":210,"type":1025}," All input is validated before use",[47,1040,1042,1044],{"className":1041},[1021],[1023,1043],{"disabled":210,"type":1025}," Response only exposes necessary fields",[47,1046,1048,1050],{"className":1047},[1021],[1023,1049],{"disabled":210,"type":1025}," Errors follow the global format",[47,1052,1054,1056],{"className":1053},[1021],[1023,1055],{"disabled":210,"type":1025}," URL includes an API version",[1058,1059,1060],"style",{},"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);}html pre.shiki code .sJ8bj, html code.shiki .sJ8bj{--shiki-default:#6A737D;--shiki-dark:#6A737D}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}",{"title":130,"searchDepth":195,"depth":195,"links":1062},[1063,1064,1065,1066,1067,1068,1069],{"id":236,"depth":195,"text":237},{"id":311,"depth":195,"text":312},{"id":542,"depth":195,"text":543},{"id":614,"depth":195,"text":615},{"id":768,"depth":195,"text":769},{"id":959,"depth":195,"text":960},{"id":1009,"depth":195,"text":1010},"2026-06-04","From wrong HTTP methods to leaking sensitive data — the errors most developers make when building their first API, and the concrete fixes.",{},"\u002Fblog\u002Fen\u002Fapi-common-mistakes",{"title":226,"description":1071},"blog\u002Fen\u002Fapi-common-mistakes",[1077,219,1078,1079],"api","rest","best-practices","z2NMprz3c9WpXKotgDMzEF9WM_LLLolyFqidBZu4I6s",{"id":1082,"title":1083,"author":6,"body":1084,"date":1738,"description":1739,"extension":206,"featured":207,"image":208,"meta":1740,"navigation":210,"path":1741,"readingTime":372,"seo":1742,"slug":1743,"stem":1744,"tags":1745,"__hash__":1747},"blog\u002Fblog\u002Fen\u002Fapi-rate-limiting-redis.md","API Rate Limiting in Practice: Token Bucket vs. Sliding Window with Redis",{"type":8,"value":1085,"toc":1729},[1086,1089,1092,1103,1105,1109,1113,1120,1124,1127,1135,1139,1142,1144,1148,1155,1657,1659,1663,1666,1723,1726],[11,1087,1088],{},"Without API rate limiting, a single rogue script or misconfigured webhook client can fire 10,000 requests per second at your login or checkout endpoint, exhausting your database connection pool and taking your entire application down.",[11,1090,1091],{},"Rate limiting is the first line of defense against bot attacks, credential stuffing, and API resource abuse. But which algorithm should you implement?",[11,1093,1094,1095,1098,1099,1102],{},"In this article, we compare the two industry-standard algorithms — ",[22,1096,1097],{},"Token Bucket"," and ",[22,1100,1101],{},"Sliding Window Log"," — and show how to implement a high-speed sliding window rate limiter in Redis.",[30,1104],{},[33,1106,1108],{"id":1107},"_1-comparing-the-algorithms","1. Comparing the Algorithms",[134,1110,1112],{"id":1111},"a-fixed-window-counter-the-flawed-default","A. Fixed Window Counter (The Flawed Default)",[11,1114,1115,1116,1119],{},"Divides time into fixed buckets (e.g. 10:00 to 10:01). If a user can make 100 requests per minute, they can send 100 requests at 10:00:59, and another 100 requests at 10:01:01. That means ",[22,1117,1118],{},"200 requests in 2 seconds"," — double the intended burst limit!",[134,1121,1123],{"id":1122},"b-token-bucket","B. Token Bucket",[11,1125,1126],{},"A bucket holds a fixed number of tokens (burst capacity). Tokens are continuously replenished at a constant rate. Each incoming request consumes one token.",[88,1128,1129],{},[47,1130,1131,1134],{},[22,1132,1133],{},"Great for",": APIs that want to allow legitimate temporary traffic spikes while capping sustained throughput.",[134,1136,1138],{"id":1137},"c-sliding-window-counter-the-production-standard","C. Sliding Window Counter (The Production Standard)",[11,1140,1141],{},"Combines the low memory usage of fixed windows with the smoothness of a rolling window. It calculates the request rate by weighting the request count of the previous window and the current window.",[30,1143],{},[33,1145,1147],{"id":1146},"_2-sliding-window-rate-limiter-with-redis-sorted-sets","2. Sliding Window Rate Limiter with Redis Sorted Sets",[11,1149,1150,1151,1154],{},"Redis Sorted Sets (",[66,1152,1153],{},"ZSET",") allow us to track requests by timestamp with microsecond accuracy:",[124,1156,1160],{"className":1157,"code":1158,"language":1159,"meta":130,"style":130},"language-typescript shiki shiki-themes github-light github-dark","import Redis from 'ioredis'\n\nconst redis = new Redis(process.env.REDIS_URL!)\n\nexport async function checkRateLimit(identifier: string, limit = 60, windowInSeconds = 60) {\n  const now = Date.now()\n  const windowStart = now - (windowInSeconds * 1000)\n  const key = `ratelimit:${identifier}`\n\n  \u002F\u002F Atomic Redis Pipeline (Runs in a single round-trip)\n  const multi = redis.multi()\n  \u002F\u002F 1. Remove all request timestamps older than the active window\n  multi.zremrangebyscore(key, 0, windowStart)\n  \u002F\u002F 2. Count requests currently in the active window\n  multi.zcard(key)\n  \u002F\u002F 3. Add the current timestamp as a new request\n  multi.zadd(key, now, `${now}-${Math.random()}`)\n  \u002F\u002F 4. Set key expiration to auto-clean idle users\n  multi.expire(key, windowInSeconds)\n\n  const results = await multi.exec()\n  const currentCount = (results?.[1]?.[1] as number) || 0\n\n  if (currentCount >= limit) {\n    return {\n      allowed: false,\n      remaining: 0,\n      retryAfter: Math.ceil(windowInSeconds - ((now - windowStart) \u002F 1000))\n    }\n  }\n\n  return {\n    allowed: true,\n    remaining: limit - currentCount - 1,\n    retryAfter: 0\n  }\n}\n","typescript",[66,1161,1162,1177,1181,1211,1215,1264,1283,1309,1326,1330,1335,1352,1357,1374,1379,1389,1394,1428,1433,1443,1448,1469,1508,1513,1528,1537,1547,1557,1586,1592,1597,1602,1610,1620,1638,1647,1652],{"__ignoreMap":130},[257,1163,1164,1168,1171,1174],{"class":259,"line":260},[257,1165,1167],{"class":1166},"szBVR","import",[257,1169,1170],{"class":337}," Redis ",[257,1172,1173],{"class":1166},"from",[257,1175,1176],{"class":363}," 'ioredis'\n",[257,1178,1179],{"class":259,"line":195},[257,1180,273],{"emptyLinePlaceholder":210},[257,1182,1183,1186,1189,1192,1195,1199,1202,1205,1208],{"class":259,"line":202},[257,1184,1185],{"class":1166},"const",[257,1187,1188],{"class":343}," redis",[257,1190,1191],{"class":1166}," =",[257,1193,1194],{"class":1166}," new",[257,1196,1198],{"class":1197},"sScJk"," Redis",[257,1200,1201],{"class":337},"(process.env.",[257,1203,1204],{"class":343},"REDIS_URL",[257,1206,1207],{"class":1166},"!",[257,1209,1210],{"class":337},")\n",[257,1212,1213],{"class":259,"line":276},[257,1214,273],{"emptyLinePlaceholder":210},[257,1216,1217,1220,1223,1226,1229,1232,1236,1239,1242,1244,1247,1249,1252,1254,1257,1259,1261],{"class":259,"line":282},[257,1218,1219],{"class":1166},"export",[257,1221,1222],{"class":1166}," async",[257,1224,1225],{"class":1166}," function",[257,1227,1228],{"class":1197}," checkRateLimit",[257,1230,1231],{"class":337},"(",[257,1233,1235],{"class":1234},"s4XuR","identifier",[257,1237,1238],{"class":1166},":",[257,1240,1241],{"class":343}," string",[257,1243,822],{"class":337},[257,1245,1246],{"class":1234},"limit",[257,1248,1191],{"class":1166},[257,1250,1251],{"class":343}," 60",[257,1253,822],{"class":337},[257,1255,1256],{"class":1234},"windowInSeconds",[257,1258,1191],{"class":1166},[257,1260,1251],{"class":343},[257,1262,1263],{"class":337},") {\n",[257,1265,1266,1269,1272,1274,1277,1280],{"class":259,"line":372},[257,1267,1268],{"class":1166},"  const",[257,1270,1271],{"class":343}," now",[257,1273,1191],{"class":1166},[257,1275,1276],{"class":337}," Date.",[257,1278,1279],{"class":1197},"now",[257,1281,1282],{"class":337},"()\n",[257,1284,1285,1287,1290,1292,1295,1298,1301,1304,1307],{"class":259,"line":212},[257,1286,1268],{"class":1166},[257,1288,1289],{"class":343}," windowStart",[257,1291,1191],{"class":1166},[257,1293,1294],{"class":337}," now ",[257,1296,1297],{"class":1166},"-",[257,1299,1300],{"class":337}," (windowInSeconds ",[257,1302,1303],{"class":1166},"*",[257,1305,1306],{"class":343}," 1000",[257,1308,1210],{"class":337},[257,1310,1311,1313,1316,1318,1321,1323],{"class":259,"line":382},[257,1312,1268],{"class":1166},[257,1314,1315],{"class":343}," key",[257,1317,1191],{"class":1166},[257,1319,1320],{"class":363}," `ratelimit:${",[257,1322,1235],{"class":337},[257,1324,1325],{"class":363},"}`\n",[257,1327,1328],{"class":259,"line":387},[257,1329,273],{"emptyLinePlaceholder":210},[257,1331,1332],{"class":259,"line":399},[257,1333,1334],{"class":331},"  \u002F\u002F Atomic Redis Pipeline (Runs in a single round-trip)\n",[257,1336,1337,1339,1342,1344,1347,1350],{"class":259,"line":410},[257,1338,1268],{"class":1166},[257,1340,1341],{"class":343}," multi",[257,1343,1191],{"class":1166},[257,1345,1346],{"class":337}," redis.",[257,1348,1349],{"class":1197},"multi",[257,1351,1282],{"class":337},[257,1353,1354],{"class":259,"line":718},[257,1355,1356],{"class":331},"  \u002F\u002F 1. Remove all request timestamps older than the active window\n",[257,1358,1359,1362,1365,1368,1371],{"class":259,"line":723},[257,1360,1361],{"class":337},"  multi.",[257,1363,1364],{"class":1197},"zremrangebyscore",[257,1366,1367],{"class":337},"(key, ",[257,1369,1370],{"class":343},"0",[257,1372,1373],{"class":337},", windowStart)\n",[257,1375,1376],{"class":259,"line":734},[257,1377,1378],{"class":331},"  \u002F\u002F 2. Count requests currently in the active window\n",[257,1380,1381,1383,1386],{"class":259,"line":745},[257,1382,1361],{"class":337},[257,1384,1385],{"class":1197},"zcard",[257,1387,1388],{"class":337},"(key)\n",[257,1390,1391],{"class":259,"line":754},[257,1392,1393],{"class":331},"  \u002F\u002F 3. Add the current timestamp as a new request\n",[257,1395,1396,1398,1401,1404,1407,1409,1412,1415,1417,1420,1423,1426],{"class":259,"line":937},[257,1397,1361],{"class":337},[257,1399,1400],{"class":1197},"zadd",[257,1402,1403],{"class":337},"(key, now, ",[257,1405,1406],{"class":363},"`${",[257,1408,1279],{"class":337},[257,1410,1411],{"class":363},"}-${",[257,1413,1414],{"class":337},"Math",[257,1416,25],{"class":363},[257,1418,1419],{"class":1197},"random",[257,1421,1422],{"class":363},"()",[257,1424,1425],{"class":363},"}`",[257,1427,1210],{"class":337},[257,1429,1430],{"class":259,"line":943},[257,1431,1432],{"class":331},"  \u002F\u002F 4. Set key expiration to auto-clean idle users\n",[257,1434,1435,1437,1440],{"class":259,"line":949},[257,1436,1361],{"class":337},[257,1438,1439],{"class":1197},"expire",[257,1441,1442],{"class":337},"(key, windowInSeconds)\n",[257,1444,1446],{"class":259,"line":1445},20,[257,1447,273],{"emptyLinePlaceholder":210},[257,1449,1451,1453,1456,1458,1461,1464,1467],{"class":259,"line":1450},21,[257,1452,1268],{"class":1166},[257,1454,1455],{"class":343}," results",[257,1457,1191],{"class":1166},[257,1459,1460],{"class":1166}," await",[257,1462,1463],{"class":337}," multi.",[257,1465,1466],{"class":1197},"exec",[257,1468,1282],{"class":337},[257,1470,1472,1474,1477,1479,1482,1485,1488,1490,1493,1496,1499,1502,1505],{"class":259,"line":1471},22,[257,1473,1268],{"class":1166},[257,1475,1476],{"class":343}," currentCount",[257,1478,1191],{"class":1166},[257,1480,1481],{"class":337}," (results?.[",[257,1483,1484],{"class":343},"1",[257,1486,1487],{"class":337},"]?.[",[257,1489,1484],{"class":343},[257,1491,1492],{"class":337},"] ",[257,1494,1495],{"class":1166},"as",[257,1497,1498],{"class":343}," number",[257,1500,1501],{"class":337},") ",[257,1503,1504],{"class":1166},"||",[257,1506,1507],{"class":343}," 0\n",[257,1509,1511],{"class":259,"line":1510},23,[257,1512,273],{"emptyLinePlaceholder":210},[257,1514,1516,1519,1522,1525],{"class":259,"line":1515},24,[257,1517,1518],{"class":1166},"  if",[257,1520,1521],{"class":337}," (currentCount ",[257,1523,1524],{"class":1166},">=",[257,1526,1527],{"class":337}," limit) {\n",[257,1529,1531,1534],{"class":259,"line":1530},25,[257,1532,1533],{"class":1166},"    return",[257,1535,1536],{"class":337}," {\n",[257,1538,1540,1543,1545],{"class":259,"line":1539},26,[257,1541,1542],{"class":337},"      allowed: ",[257,1544,350],{"class":343},[257,1546,353],{"class":337},[257,1548,1550,1553,1555],{"class":259,"line":1549},27,[257,1551,1552],{"class":337},"      remaining: ",[257,1554,1370],{"class":343},[257,1556,353],{"class":337},[257,1558,1560,1563,1566,1569,1571,1574,1576,1579,1581,1583],{"class":259,"line":1559},28,[257,1561,1562],{"class":337},"      retryAfter: Math.",[257,1564,1565],{"class":1197},"ceil",[257,1567,1568],{"class":337},"(windowInSeconds ",[257,1570,1297],{"class":1166},[257,1572,1573],{"class":337}," ((now ",[257,1575,1297],{"class":1166},[257,1577,1578],{"class":337}," windowStart) ",[257,1580,298],{"class":1166},[257,1582,1306],{"class":343},[257,1584,1585],{"class":337},"))\n",[257,1587,1589],{"class":259,"line":1588},29,[257,1590,1591],{"class":337},"    }\n",[257,1593,1595],{"class":259,"line":1594},30,[257,1596,946],{"class":337},[257,1598,1600],{"class":259,"line":1599},31,[257,1601,273],{"emptyLinePlaceholder":210},[257,1603,1605,1608],{"class":259,"line":1604},32,[257,1606,1607],{"class":1166},"  return",[257,1609,1536],{"class":337},[257,1611,1613,1616,1618],{"class":259,"line":1612},33,[257,1614,1615],{"class":337},"    allowed: ",[257,1617,819],{"class":343},[257,1619,353],{"class":337},[257,1621,1623,1626,1628,1631,1633,1636],{"class":259,"line":1622},34,[257,1624,1625],{"class":337},"    remaining: limit ",[257,1627,1297],{"class":1166},[257,1629,1630],{"class":337}," currentCount ",[257,1632,1297],{"class":1166},[257,1634,1635],{"class":343}," 1",[257,1637,353],{"class":337},[257,1639,1641,1644],{"class":259,"line":1640},35,[257,1642,1643],{"class":337},"    retryAfter: ",[257,1645,1646],{"class":343},"0\n",[257,1648,1650],{"class":259,"line":1649},36,[257,1651,946],{"class":337},[257,1653,1655],{"class":259,"line":1654},37,[257,1656,369],{"class":337},[30,1658],{},[33,1660,1662],{"id":1661},"_3-returning-standard-http-rate-limit-headers","3. Returning Standard HTTP Rate Limit Headers",[11,1664,1665],{},"Whenever your server answers a request, communicate rate limit states in the response headers (following IETF draft standards):",[124,1667,1669],{"className":251,"code":1668,"language":253,"meta":130,"style":130},"HTTP\u002F1.1 429 Too Many Requests\nRetry-After: 32\nX-RateLimit-Limit: 60\nX-RateLimit-Remaining: 0\nX-RateLimit-Reset: 1773489240\nContent-Type: application\u002Fjson\n\n{\n  \"error\": \"Too Many Requests\",\n  \"message\": \"Rate limit exceeded. Please retry in 32 seconds.\"\n}\n",[66,1670,1671,1676,1681,1686,1691,1696,1701,1705,1709,1714,1719],{"__ignoreMap":130},[257,1672,1673],{"class":259,"line":260},[257,1674,1675],{},"HTTP\u002F1.1 429 Too Many Requests\n",[257,1677,1678],{"class":259,"line":195},[257,1679,1680],{},"Retry-After: 32\n",[257,1682,1683],{"class":259,"line":202},[257,1684,1685],{},"X-RateLimit-Limit: 60\n",[257,1687,1688],{"class":259,"line":276},[257,1689,1690],{},"X-RateLimit-Remaining: 0\n",[257,1692,1693],{"class":259,"line":282},[257,1694,1695],{},"X-RateLimit-Reset: 1773489240\n",[257,1697,1698],{"class":259,"line":372},[257,1699,1700],{},"Content-Type: application\u002Fjson\n",[257,1702,1703],{"class":259,"line":212},[257,1704,273],{"emptyLinePlaceholder":210},[257,1706,1707],{"class":259,"line":382},[257,1708,338],{},[257,1710,1711],{"class":259,"line":387},[257,1712,1713],{},"  \"error\": \"Too Many Requests\",\n",[257,1715,1716],{"class":259,"line":399},[257,1717,1718],{},"  \"message\": \"Rate limit exceeded. Please retry in 32 seconds.\"\n",[257,1720,1721],{"class":259,"line":410},[257,1722,369],{},[11,1724,1725],{},"This ensures honest third-party API clients and mobile SDKs can automatically back off and retry without overwhelming your infrastructure.",[1058,1727,1728],{},"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);}html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .s4XuR, html code.shiki .s4XuR{--shiki-default:#E36209;--shiki-dark:#FFAB70}html pre.shiki code .sJ8bj, html code.shiki .sJ8bj{--shiki-default:#6A737D;--shiki-dark:#6A737D}",{"title":130,"searchDepth":195,"depth":195,"links":1730},[1731,1736,1737],{"id":1107,"depth":195,"text":1108,"children":1732},[1733,1734,1735],{"id":1111,"depth":202,"text":1112},{"id":1122,"depth":202,"text":1123},{"id":1137,"depth":202,"text":1138},{"id":1146,"depth":195,"text":1147},{"id":1661,"depth":195,"text":1662},"2026-09-14","Comparing rate-limiting algorithms to protect your backend against scrapers, credential stuffing, and bot spam with minimal latency overhead.",{},"\u002Fblog\u002Fen\u002Fapi-rate-limiting-redis",{"title":1083,"description":1739},"api-rate-limiting-redis","blog\u002Fen\u002Fapi-rate-limiting-redis",[1077,217,221,219,1746],"performance","KXTsuQOwj895dkbCDObhyO_wOncRKtB2ufJbjc-AUyc",1792236100654]