This approach creates a clear boundary between individual users' data, making the leaking of data between users impossible (bar the loss of security credentials, of course). In practice, when an application becomes popular and generates thousands or even hundreds of thousands of users, it becomes tricky to deal with many small pots of data. It becomes especially difficult if you want to deal with the data set as a whole for backup purposes or for reporting and analytics.
Cloudant Envoy is an open-source project actively managed by IBM. It's released under the Apache-2.0 license, so you can use it for free in your own application. Envoy makes it simple to create offline-first applications that continue to work even with no network connection, giving your uses 100% uptime and a great user experience.
How does Cloudant Envoy work?
The mobile device thinks it's talking to a Cloudant/CouchDB service, as Envoy emulates enough of the Cloudant API that it can accept pull or push replication API calls. Envoy's main job is to keep each mobile users' data separately. It does this by manipulating the document ids.
Envoy modifies the id to be <hash of user id>:abc – prefixing the id with the hash of the requesting user's id. This keeps each user's data sorted in the index and compartmentalised so that other users can't access it.
If the mobile device requests document abc, Envoy performs the same id manipulation and fetches <hash of user id>:abc instead.
The effect of this is to allow many thousands of users to replicate with a single Cloudant database – the mobile device is under the illusion that is using a one-database-per-user architecture, but the server-side database is one database for all users.
In addition to manipulating the document IDs as they pass through, Envoy also keeps an SQLite database of changes. This allows the changes feed to be indexed by the user, making it the speed of fetching the changes feed to remain constant as the size of the database grows.
Querying data across many databases (e.g. for analytics) can be difficult. Cloudant does not have native support for a cross-database query and aggregating the data into a single database using e.g. replication can be resource intensive or require manual scheduling.
There is a high management cost of many (thousands of) databases. Consider backup, replication in cross-region scenarios, design document management. These all require coordination/resource management outside of Cloudant.
Envoy aims to work around these problems by emulating database-per-user using a single backend database and a proxy.
Deploy Cloudant Envoy to Bluemix by clicking the Deploy to Bluemix button below.
After those variables are set, you can start the Envoy server with npm start. Note that the port is the port that Envoy will listen to, not the port of the Cloudant server.