Per user database replication with couchdb
Asked Answered
I

1

7

I have an application that will contain information that is sensitive to a user. From what I can tell I should use a database per user architecture. I'd like each user's db (potentially client side) to replicate to a database located on a public server and allow users to access the application from any device such that the user would log in on the device, the database will be discovered by some middle tier on the public server and then replicated client side and synchronizing happen between the device and the public server.

It seems that's what CouchDB is good for (based on my Google searching) but are there any example apps that do what I describe (or close to it)? I'm using couchdb 1.1.0.

Igloo answered 4/11, 2011 at 12:10 Comment(0)
F
9

Yes, CouchDB sounds like a great fit for this — its simple protocol makes it a great fit for web apps [even offline, see pouchdb] and mobile/desktop apps [again even offline, see Couchbase Mobile.

Unfortunately, I don't know of a great publicly available code-level example offhand, but the basic idea is to use a combination of filtered replication and document validation:

The basic idea is that for your server-side copy of the user database you have validation functions set up so that desired document schemas and access control is enforced. The end user gets an replica of this database that can be used for low-latency and offline access — theoretically they could subvert their copy, but when replicating back the validation function will prevent the server-side database from getting corrupted.

You can even set up a master database that is not public accessible, then use filtered replication to sync each users' data to the server side per-user databases — useful for centralized messaging, aggregate stats, needing only to back up one database, etc.

There's a few more high-level examples in this "New Features in Replication" article, especially the "DesktopCouch" and "Need-to-know-based Data Sharing" use case sections towards the end.

UPDATE (2015/03/10): I no longer recommend using CouchDB's filtered replication as described above. There are several performance and scalability problems (if not also reliability concerns) that come up when you try to replicate more than a few filtered feeds off of a central database. You might look into trying Couchbase and its Sync Gateway if you need document-level read permissions, or build out your own per-user changes views (secured behind custom middleware) using _local_seq.

Folketing answered 16/11, 2011 at 18:30 Comment(4)
Thanks for the direction. I'll dig around in this and update once I have something concreteIgloo
Can you elaborate just a little bit on the filter replication from the central non-public db? How would a client replicate to this non-public DB securely? I'll open a new question if you don't have a reference link handy.Kutzer
In the context of this question, the client doesn't replicate to the central DB. You set up the replications via a trusted account, then add privileges to their (per-user) database so the client can access.Folketing
@MarioMichelli what exactly is hoodie good for? It looks like it's focused on marketing - I couldn't really understand where it fits in or how it integrates. Some of the taglines sounded good though, like 'Offline first', is exactly what I need. Can you elaborate?Lubra

© 2022 - 2024 — McMap. All rights reserved.