Skip to main content

Part-6: - Multiple key spaces

Tutorial overview

Welcome to this part-6 of the key-value database tutorial for codehooks.io serverless Node.js backend.

Introducing Key-Value with Isolated Key Spaces

Key-Value databases are powerful tools for storing and retrieving data using a simple key-based approach. They offer flexibility, performance, and scalability for various applications. With the concept of isolated key spaces, you can take the benefits of Key-Value databases a step further.

Isolated key spaces allow you to logically separate different sets of keys within the same key-value database. Each isolated key space acts as an independent namespace, providing data isolation and organization. This means you can have multiple sets of keys with the same names, and they won't interfere with each other.

The key-value database API supports isolated key spaces for all API methods: set, get, getAll, incr, decr, del and delAll.

To set a key-value in an isolated key space you use the keyspace option value in each API method.

const conn = await Datastore.open();
const option = {
"keyspace": "foospace"
};
const result = await conn.set('somekey', 'some value', option);
console.log(result);

Similarily to get a key-value from the isolated key space the same option applies.

const conn = await Datastore.open();
const option = {
"keyspace": "foospace"
};
const result = await conn.get('somekey', option);
console.log(result);

This feature is particularly useful in scenarios where you need to store data from different sources or applications within a single database. By leveraging isolated key spaces, you can maintain data separation, avoid key collisions, and manage your data more efficiently.

Isolated key spaces enable you to:

  • Organize Data: Group related keys together within isolated key spaces, making it easier to manage and maintain your data.
  • Prevent Key Collisions: With isolated key spaces, keys with the same name can coexist without conflicts, as they belong to separate namespaces.
  • Achieve Data Isolation: Keep data from different applications or components separate, ensuring integrity and avoiding unintended data interactions.
  • Simplify Access Control: Apply different access permissions and security measures to each isolated key space, providing granular control over data access.
  • Enhance Scalability: Isolated key spaces allow for horizontal scaling by distributing data across multiple instances or clusters.

By utilizing key-value databases with isolated key spaces, you can improve data organization, scalability, and security while maintaining the simplicity and efficiency of key-value storage.