How do you control user access to records in a key-value database?
<p>I have a web application that accesses large amounts of JSON data.</p>

<p>I want to use a key value database for storing JSON data owned/shared by different users of the web application (not users of the database). Each user should only be able to access the records they own or share.</p>

<p>In a relational database, I would add a column <code>Owner</code> to the record table, or manage shared ownerships in a separate table, and check access on the application side (Python). For key value stores, two approaches come to mind.</p>

<h2>User ID as part of the key</h2>

<p>What if I use keys like <code>USERID_RECORDID</code> and then write code to check the <code>USERID</code> before accessing the record? Is that a good idea? It wouldn't work with records that are shared between users.</p>

<h2>User ID as part of the value</h2>

<p>I could store one or more <code>USERIDs</code> in the value data and check if the data contains the ID of the user trying to access the record. Performance is probably slower than having the user ID as part of the key, but shared ownerships are possible.</p>

<h2>What are typical patterns to do what I am trying to do?</h2>

<p>Both of the solutions you described have some limitations.</p>

<ul>
<li><p>You point yourself that including the owner ID in the key does not solve the problem of shared data. However, this solution may be acceptable, if you add another key/value pair, containing the IDs of the contents shared with this user <code>(key: userId:shared, value: [id1, id2, id3...])</code>.</p></li>
<li><p>Your second proposal, in which you include the list of users who were granted access to a given content, is OK if and only if you application needs to make a query to retrieve the list of users who have access to a particular content. If your need is to list all contents a given user can access, this design will lead you to poor performances, as the K/V store will have to scan all records -and this type of database engine usually don't allow you to create an index to optimise this kind of request.</p></li>
</ul>

<p>From a more general point of view, with NoSQL databases and especially Key/Value stores, the model has to be defined according to the requests to be made by the application. It may lead you to duplicate some information. The application has the responsibility of maintaining the consistency of the data.</p>

<p>By example, if you need to get all contents for a given user, whether this user is the owner of the content or these contents were shared with him, I suggest you to create a key for the user, containing the list of content Ids for that user, as I already said. But if your app also needs to get the list of users allowed to access a given content, you should add their IDs in a field of this content. This would result in something like :
<code>key: contentID, value: { ..., [userId1, userID2...]}</code></p>

<p>When you remove the access to a given content for a user, your app (and not the datastore) have to remove the userId from the content value, and the contentId from the list of contents for this user.</p>

<p>This design may imply for your app to make multiple requests: by example one to get the list of userIDs allowed to access a given content, and one or more to get these user profiles. However, this should not really be a problem as K/V stores usually have very high performances.</p>

python database nosql authorization key-value