Cookies: Key/Value pairs vs separate cookies

Can anyone tell me what the pros/cons are of using a single cookie with multiple key/value pairs vs using multiple cookies with a single value? I'm wondering if there are performance benefits or issues, or other programming pros/cons for one or the other.
nzinsliAsked:
Who is Participating?
 
CodeCruiserConnect With a Mentor Commented:
There are a couple of reasons to use subkeys instead of separate cookies. Obviously, it is tidy to put related or similar information into a single cookie. In addition, because all the information is in a single cookie, cookie attributes such as expiration apply to all the information. (Conversely, of course, if you want to assign different expiration dates to different types of information, you should store the information in separate cookies.)

A cookie with subkeys also helps you keep cookie size down. As noted earlier under Cookie Limitations, cookies are limited to 4096 bytes and you can't store more than 20 cookies per site. By using a single cookie with subkeys, you use up fewer of those 20 cookies your site is allotted. In addition, a single cookie takes up about 50 characters for overhead (expiration information, and so on), plus the length of the value that you store in it, all of which counts toward the 4K limit. If you store five subkeys instead of five separate cookies, you save the overhead of the separate cookies and can save around 200 bytes.



http://msdn.microsoft.com/en-us/library/aa289495(v=vs.71).aspx#vbtchaspnetcookies101anchor6

0
 
Paul MacDonaldDirector, Information SystemsCommented:
I don't think there's a performance difference.  The only issue I can think of is keeping track of the different names.
0
 
nzinsliAuthor Commented:
Is the 4k limit per cookie, or as a total for all cookies for the domain?
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
Paul MacDonaldConnect With a Mentor Director, Information SystemsCommented:
4k per cookie, 20 cookies per site.
0
 
CodeCruiserCommented:
0
 
nzinsliAuthor Commented:
Thanks.

Is there any real performance concerns in .Net with the code needed to retrieve a cookie value vs parsing the key/value pairs?
0
 
Paul MacDonaldDirector, Information SystemsCommented:
Retrieving a cookie requires a round trip to the client.  That's subject to the vagarities of the network.  Parsing key/values will be done as fast as the local machine can make it happen.
0
 
CodeCruiserCommented:
> Retrieving a cookie requires a round trip to the client.  That's subject to the vagarities of the network.  Parsing key/values will be done as fast as the local machine can make it happen.
      

@paulmacd

It does not work that way. The cookies are posted to the server along the request by the client and are parsed on the server.

"When a browser makes a request to the server, it sends the cookies for that server along with the request."


http://msdn.microsoft.com/en-us/library/aa289495%28v=vs.71%29.aspx#vbtchaspnetcookies101anchor8
0
 
Paul MacDonaldDirector, Information SystemsCommented:
Hey, I learned something!

In any case, having fewer cookies means less overhead.
0
 
nzinsliAuthor Commented:
Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.