๐—ช๐—ต๐˜† ๐—๐—ช๐—ง ๐—˜๐˜…๐—ถ๐˜€๐˜๐˜€: ๐—ง๐—ต๐—ฒ ๐—ฅ๐—ฒ๐—ฎ๐—น ๐—ก๐˜‚๐—บ๐—ฏ๐—ฒ๐—ฟ๐˜€

I used JWT in every project. I knew how to use it. I did not know why it existed. I stopped watching tutorials. I looked at the numbers.

The speed of operations:

Most apps used sessions before JWT. The server stores a session in a database. The browser sends a cookie. The server looks up the session in the database. This takes 50ms per request. 10,000 users create 10,000 database calls per second. Your database slows down. It becomes a single point of failure.

Some think about using bcrypt for every request. Bcrypt takes 100ms on purpose. It stops brute force attacks. But 1,000 users need 100 seconds of compute per second. Your server crashes.

JWT solves this with math. A JWT has three parts:

The server re-computes the signature. It takes 5ms. No database lookup. No network call.

Warning: the payload is public. The payload is open to everyone. Avoid putting passwords in the payload. The signature only proves the data is not modified.

JWTs have one weakness. Logging people out is hard. Sessions are easy to delete from a database. JWTs last until they expire.

Engineers fix this with:

Knowing the syntax is not engineering. Engineering is understanding the why. The numbers explain the tool.

Source: https://dev.to/vikrant_98a676bd9e7ba34/why-jwt-exists-what-i-figured-out-by-looking-at-real-numbers-4gm8