How big can a user agent string get?

HTTP specification does not limit length of headers at all. However web-servers do limit header size they accept, throwing 413 Entity Too Large if it exceeds.

Depending on web-server and their settings these limits vary from 4KB to 64KB (total for all headers).


My take on this:

  • Use a dedicated table to store only UserAgents (normalize it)
  • In your related tables, store an Foreign Key value to point back to the UserAgent auto-increment primary key field
  • Store the actual UserAgent string in a TEXT field and care not about the length
  • Have another UNIQUE BINARY(32) (or 64, or 128 depending on your hash length) and hash the UserAgent

Some UA strings can get obscenely long. This should spare you the worries. Also enforce a maximum length in your INSERTer to keep UA strings it under 4KB. Unless someone is emailing you in the user-agent, it should not go over that length.