Designing Unique Keys(Primary Keys) for a heavily

2020-05-09 04:04发布

问题:

I am working on a web application related to Discussion forums using Java and Cassandra database.

I need to construct 'keys' for the rows storing the user's details and & another set of rows storing the content posted by the user.

One option is to get the randomly generated UUID provided by Java language, but these are 16 bytes long. and since NoSQL database involves heavy denormalization, I am concerned whether I would be wasting lots of disk space, RAM and other resources if the key could be generated in smaller sizes.

I need to generate two types of keys, one for the Users & other for Content Posted by Users.

For the Content posted by users, would timestamp+userId be a good key. where timestamp is the server time at which content was posted and userId refers to key of user row.

Any suggestions, comments appreciated ..

Thanks Marcos

回答1:

Is this a distributed application?

Then you could use a simple synchronized counter and initialize it on startup with the next available id.

On the other hand a database should be able to handle the UUID hashes as created by java. This is a standard for creating things like sessionIds, that need to be unique. Your problem is somewhat similar since a session in your context would represent a set of user input.