0

I got a simple WS that acts like a session server, nothing complex at all. When I create a session, I got an _id from Mongo that I cannot give back to user cause of the sequence used by mongo.

Example of ids sequence when chaining the creation:

536004fe2e1d59c95e9155a8
536004fe2e1d59c95e9155a9
536004fe2e1d59c95e9155aa
536004fe2e1d59c95e9155ab

As u can see, can ask a session and try to figure out next ones...

My question is: what sort of tricks do you use to create random but (nearly) unique id?

  • sha1/md5/... of the mongo _id, stored on the db. Implication is save + update
  • Using one-way encryption on the fly on the _id
  • Do not use a session db... Hey ! I have no choice !!
  • ... ?

2 Answers 2

1

I use Redis for the session data for our site because it offers high read/write performance and key lookup is the only thing needed for session data. Why are you using MongoDB for your session store?

In any case, you can assign the _id before saving it to MongoDB, and it will use that instead of auto-generating one. If you set the _id to a UUID (what I use for my key) or some other random value, you can use that without any performance degradation.

1
  • Cause sometimes, boss takes decisions :( Thanks for the tips to assign before saving.
    – farvilain
    Apr 29, 2014 at 20:35
-1

I tend to use MD5 of my current timestamp, it's almost "unique".

You could generate it and post it as any other field on a mongo document, no need to use _id.

Note that i've never used mongo, i use CouchBase and views.

1
  • I believe he is looking for two important features that a session id should have: randomness and uniqueness. MD5 of current timestamp may produce unique value but it is not random.
    – aks
    Apr 30, 2014 at 1:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.