LogoLogo
API ReferenceGitHubSlackService StatusLogin
v3.9.16
v3.9.16
  • 🏠Deep Lake Docs
  • List of ML Datasets
  • 🏗️SETUP
    • Installation
    • User Authentication
      • Workload Identities (Azure Only)
    • Storage and Credentials
      • Storage Options
      • Setting up Deep Lake in Your Cloud
        • Microsoft Azure
          • Configure Azure SSO on Activeloop
          • Provisioning Federated Credentials
          • Enabling CORS
        • Google Cloud
          • Provisioning Federated Credentials
          • Enabling CORS
        • Amazon Web Services
          • Provisioning Role-Based Access
          • Enabling CORS
  • 📚Examples
    • Deep Learning
      • Deep Learning Quickstart
      • Deep Learning Guide
        • Step 1: Hello World
        • Step 2: Creating Deep Lake Datasets
        • Step 3: Understanding Compression
        • Step 4: Accessing and Updating Data
        • Step 5: Visualizing Datasets
        • Step 6: Using Activeloop Storage
        • Step 7: Connecting Deep Lake Datasets to ML Frameworks
        • Step 8: Parallel Computing
        • Step 9: Dataset Version Control
        • Step 10: Dataset Filtering
      • Deep Learning Tutorials
        • Creating Datasets
          • Creating Complex Datasets
          • Creating Object Detection Datasets
          • Creating Time-Series Datasets
          • Creating Datasets with Sequences
          • Creating Video Datasets
        • Training Models
          • Splitting Datasets for Training
          • Training an Image Classification Model in PyTorch
          • Training Models Using MMDetection
          • Training Models Using PyTorch Lightning
          • Training on AWS SageMaker
          • Training an Object Detection and Segmentation Model in PyTorch
        • Updating Datasets
        • Data Processing Using Parallel Computing
      • Deep Learning Playbooks
        • Querying, Training and Editing Datasets with Data Lineage
        • Evaluating Model Performance
        • Training Reproducibility Using Deep Lake and Weights & Biases
        • Working with Videos
      • Deep Lake Dataloaders
      • API Summary
    • RAG
      • RAG Quickstart
      • RAG Tutorials
        • Vector Store Basics
        • Vector Search Options
          • LangChain API
          • Deep Lake Vector Store API
          • Managed Database REST API
        • Customizing Your Vector Store
        • Image Similarity Search
        • Improving Search Accuracy using Deep Memory
      • LangChain Integration
      • LlamaIndex Integration
      • Managed Tensor Database
        • REST API
        • Migrating Datasets to the Tensor Database
      • Deep Memory
        • How it Works
    • Tensor Query Language (TQL)
      • TQL Syntax
      • Index for ANN Search
        • Caching and Optimization
      • Sampling Datasets
  • 🔬Technical Details
    • Best Practices
      • Creating Datasets at Scale
      • Training Models at Scale
      • Storage Synchronization and "with" Context
      • Restoring Corrupted Datasets
      • Concurrent Writes
        • Concurrency Using Zookeeper Locks
    • Deep Lake Data Format
      • Tensor Relationships
      • Version Control and Querying
    • Dataset Visualization
      • Visualizer Integration
    • Shuffling in Dataloaders
    • How to Contribute
Powered by GitBook
On this page
  • How to Implement External Locks using Zookeeper
  • Setup
  • Write Locks
  • Read Locks (Optional)

Was this helpful?

Edit on GitHub
  1. Technical Details
  2. Best Practices
  3. Concurrent Writes

Concurrency Using Zookeeper Locks

Using Zookeeper for locking Deep Lake datasets.

PreviousConcurrent WritesNextDeep Lake Data Format

Was this helpful?

This tutorial assumes the reader has knowledge of Deep Lake APIs and does not explain them in detail. For more information, check out our or .

How to Implement External Locks using Zookeeper

is a tool that can be used to manage Deep Lake locks and ensure that only 1 worker is writing to a Deep Lake dataset at a time. It offers a simple API for managing locks using a few lines of code.

Setup

First, let's install Zookeper and launch a local server using Docker in the CLI.

pip install zookeeper

docker run --rm -p 2181:2181 zookeeper

Write Locks

All write operations should be executed while respecting the lock.

Let's connect a Python client to the local server and create a WriteLock using:

from kazoo.client import KazooClient

zk = KazooClient(hosts="127.0.0.1:2181")
zk.start()
deeplake_writelock = zk.WriteLock("/deeplake")

The client can be blocked from performing operations without a WriteLock using the code below. The code will wait until the lock becomes available, and the internal Deep Lake lock should be disabled by specifying lock_enabled=False:

from deeplake.core.vectorstore import VectorStore

with deeplake_writelock:

    # Initialize the Vector Store
    vector_store = VectorStore(<vector_store_path>, lock_enabled=False)
    
    # Add data
    vector_store.add(text = <your_text>, 
                     metadata = <your_metadata>, 
                     embedding_function = <your_embedding_function>)

    # This code can also be used with the Deep Lake LangChain Integration
    # from langchain.vectorstores import DeepLake
    # db = DeepLake(<dataset_path>, embedding = <your_embedding_function>)
    # db.add_texts(tests = <your_texts>, metadatas = <your_metadatas>, ...)

    # This code can also be used with the low-level Deep Lake API
    # import deeplake
    # ds = deeplake.load(dataset_path)
    # ds.append({...})

Read Locks (Optional)

When Writes are Append-Only

If the write operations are only appending data, it is not necessary to use locks during read operations like as vector search. However, the Deep Lake datasets must be reloaded or re-initialized in order to have the latest available information from the write operations.

from deeplake.core.vectorstore import VectorStore

# Initialize the Vector Store 
vector_store = VectorStore(<vector_store_path>, read_only = True)

# Search for data
search_results = vector_store.search(embedding_data = <your_prompt>, 
                                     embedding_function = <your_embedding_function>)


# This code can also be used with the Deep Lake LangChain Integration
# from langchain.vectorstores import DeepLake
# db = DeepLake(<dataset_path>, embedding = <your_embedding_function>, read_only = True)
# retriever = db.as_retriever()
# qa = RetrievalQA.from_llm(llm = <your_model>, retriever = retriever)


# This code can also be used with the low-level Deep Lake API
# import deeplake
# ds = deeplake.load(<dataset_path>, read_only = True)
# dataloader = ds.dataloader().pytorch(...)

When Writes Update and Delete Data

If the write operations are updating or deleting rows of data, the read operations should also lock the dataset in order to avoid corrupted read operations.

Let's connect a Python client to the same local server above and create a ReadLock . Multiple clients can have a ReadLock without blocking each other, but they will all be blocked by the WriteLock above.

from kazoo.client import KazooClient

zk = KazooClient(hosts="127.0.0.1:2181")
zk.start()
deeplake_readlock = zk.ReadLock("/deeplake")

The syntax for restricting operations using the ReadLock is:

from deeplake.core.vectorstore import VectorStore

with deeplake_readlock:

    # Initialize the Vector Store 
    vector_store = VectorStore(<vector_store_path>, read_only = True)

    # Search for data
    search_results = vector_store.search(embedding_data = <your_prompt>, 
                                        embedding_function = <your_embedding_function>)


    # This code can also be used with the Deep Lake LangChain Integration
    # from langchain.vectorstores import DeepLake
    # db = DeepLake(<dataset_path>, embedding = <your_embedding_function>, read_only = True)
    # retriever = db.as_retriever()
    # qa = RetrievalQA.from_llm(llm = <your_model>, retriever = retriever)


    # This code can also be used with the low-level Deep Lake API
    # import deeplake
    # ds = deeplake.load(<dataset_path>, read_only = True)
    # dataloader = ds.dataloader().pytorch(...)

Congrats! You just learned how manage your own lock for Deep Lake using Zookeeper! 🎉

🔬
Deep Learning Quickstart
Vector Store Quickstart
Apache Zookeeper