Client Variables in ColdFusion is Good or Bad?

Client variables is storage method to persist information with a client and application and saved between user sessions. Normally I look as an alternative of session variables to store data. Main difference between session and client is we have option to choose storage of client variables.
Client variables offers following storage:

  1. Registry
  2. Cookie
  3. Database

Registry (default) : Applicable only for windows OS. I really hate to store in Registry here is another blog entry explaining why? 

Cookie : Well, every developer should aware of pros/cons of cookie so I am not explaining this here.

Database: This one is my favorite storage method to store client variables. Since this will work with all platform, it doesn’t mind if I have huge client variables value.

In this post I am going to discuss Good and Bad part with Database storage. Registry and Cookie I am not considering for this blog post since for registry I have mention every thing my another blog post and Cookie I never use for client variables.

Good Part:

  1. There will not be limitation of data size of client variables for database.
  2. Client variables can be use to store complex data by serializing to JSON and we can deserialize it whenever we need back.

Bad Part:

  1. ColdFusion will fire query to look for client variables in database on every request.
  2. Performance may degrade little bit due to database access.

Optimize Client Variables Storage:

If you have make mind to use client variables with storage database make sure you have taken following precaution.

  1. Keep client variables database separate from application database if possible.
  2. Purge interval for unvisited user keep smaller as much as possible. I preferred 2 days but you can adjust as per your client variables importance and frequent visit of your users.
  3. Check the box "Disable global client variable updates" : If you are not really using hitcount and lastvisit records to track your user, I will suggest to disable it. Since this will reduce number of database transaction. If this option enable system will always update hitcount and lastvisit variable in database for every request and this may impact performance. Also database will grow if you have heavy traffic on your site.
  4. Since ColdFusion give ability to create client variables tables if not already existing in database and we normally use this option. ColdFusion will create following two tables.
    1. CDATA
    2. CGLOBAL

But coldfusion doesn’t add any primary key or index on this table. I will suggest to add indexes on both table as follow.

For CDATA add index for APP,CFID column.
For CGLOBAL add index for CFID column.
ColdFusion perform following query on every requests to retrive client variable.
[code:sql]select data from CGLOBAL where cfid = @P1[/code]
[code:sql]select cfid,app,data from CDATA where cfid = @P1  and app = @P2[/code]

Notice WHERE condition and you will get idea why do I suggest to add index. If you have heavy traffic site then both tables size will increases and may cause slower query performance if purge interval is high and no indexing on table.

Hope this help and always welcome suggestion