milvus-logo
LFAI
Home

Tune consistency

Milvus supports setting consistency level while creating a collection, conducting a vector query, and conducting a vector search (only on PyMilvus currently). Milvus supports four levels of consistency: Strong, Eventual, Bounded, and Session. By default, a collection created without specifying the consistency level is set with Bounded consistency level. This topic describes how to tune consistency.

Configure parameter

By default, the consistency level is set as Bounded, under which Milvus reads a less updated data view (usually several seconds earlier) when a search or query request comes. You can set the consistency level by configuring the parameter consistency_level while creating a collection and conducting a search or query. See Guarantee Timestamp in Search Requests for the mechanism behind.

Parameter Description Option
consistency_level Consistency level of the collection to create.
  • Strong
  • Bounded
  • Session
  • Eventually

Example

The following examples set the consistency level as Strong, meaning Milvus will read the most updated data view at the exact time point when a search or query request comes. The consistency level set in the search or query requests overwrites the one set while creating the collection. Without specifying the consistency level during a search or query, Milvus adopts the original consistency level of the collection.

When interacting with Milvus using Python code, you have the flexibility to choose between PyMilvus and MilvusClient (new). For more information, refer to Python SDK.

  • Create a collection
from pymilvus import Collection
collection = Collection(
    name=collection_name, 
    schema=schema, 
    using='default', 
    shards_num=2,
    consistency_level="Strong"
    )
  • Conduct a vector search
result = hello_milvus.search(
        vectors_to_search,
        "embeddings",
        search_params,
        limit=3,
        output_fields=["random"],
        # search will scan all entities inserted into Milvus.
        consistency_level="Strong",
        )
  • Conduct a vector query
res = collection.query(
  expr = "book_id in [2,4,6,8]", 
  output_fields = ["book_id", "book_intro"],
  consistency_level="Strong"
)
Feedback

Was this page helpful?