What is the best MYSQL or Maria DB data type for storing a JWT token?

I use the following technology stack

  • Laravel 5.2
  • MySQL

and for security I use JWT (JSON Web Tokens)

I was able to protect my applications with JWT.

I would like to save the JWT token in the mysql database.

QUESTION Which of the following data types is the best way to store a JWT token in a MySQL database?

  • VARCHAR
  • CLOB
  • TEXT
  • LONG TEXT
+5
source share
2 answers

As in any other case, the answer is "it depends."

First, you need to determine if saving a fully encoded JWT is the right solution. I try not to store the JWT string and instead store the applications used to build the JWT, which will save a lot of room in the database.

If you decide that saving JWT is the right method, we can look at your options.

TEXT and LONGTEXT are just CLOB types, so we can ignore this one.

TEXT and VARCHAR have 64K limits, so anything above will require LONGTEXT (or MEDIUMTEXT, which you did not mention but is an option).

The difference between TEXT and VARCHAR is that VARCHAR is stored in a string, but TEXT is basically a pointer. VARCHAR will be faster if you read JWT often, but larger lines will cause each individual line to be larger, which will result in a result.

With a lot of JWT, as a rule, I would say that TEXT is a pretty good choice for storing JWT in the database. If you are absolutely sure that the JWT will remain very small, then VARCHAR can improve read performance, but you would be better off checking the data in the real world to be sure.

If you need a field larger than TEXT, it can provide, I would repeat my recommendation to avoid storing the encoded JWT, but LONGTEXT is an option there.

+6
source

Based on this example, I would suggest this for a β€œencoded” base64 token:

TEXT CHARACTER SET ascii COLLATE ascii_bin 

In general, JSON should be TEXT or VARCHAR with CHARACTER SET utf8 or utf8mb4 . ( COLLATION probably doesn't matter.)

TEXT limited to 64 KB; There is no advantage to using a smaller VARCHAR .

Re: " TEXT is just a pointer" - Not quite right. On some ROW_FORMATs in InnoDB, either TEXT or VARCHAR can be a pointer to a string extension. The action depends mainly on ROW_FORMAT , and not on the data type.

+1
source

Source: https://habr.com/ru/post/1247174/


All Articles