Sharding, Replication, Consistency, Backup and Upgrades

Article · 6:00

Sharding and shard count immutability, replication and per-request consistency levels, the backup modules, restore, and rolling upgrade planning.

This lesson has 1 runnable example. Press Try yourself! under any of them to open it in the editor.

Sharding distributes a collection across nodes, and the critical constraint is that the shard count is fixed at collection creation. Weaviate does not reshard, so a collection created with two shards stays at two shards, and growing beyond them means creating a new collection and reimporting. That makes the shard count a decision to make deliberately, with the usual guidance being to allow for growth rather than match current size.

Multi-tenancy interacts with this well: a tenanted collection gives each tenant its own shard, so the tenant count is the shard count and it grows as tenants are added. That is a second strong argument for the multi-tenant design.

Replication is per collection, with a replication factor, and Weaviate uses a Raft-based mechanism for schema and cluster metadata with configurable consistency for data. The feature that matters is per-request consistency levels: ONE, QUORUM and ALL, chosen per operation, with the same R + W > RF arithmetic as the Cassandra course. QUORUM for both gives you strong consistency; ONE for a read is fast and may be stale. Setting it per request rather than globally is the point, because a product search can tolerate staleness and a stock check cannot.

Backup is a module, and which modules you enabled determines what you can do: backup-filesystem writes to a local path, backup-s3, backup-gcs and backup-azure to object storage. A backup can cover the whole instance or named collections, it runs online, and it is asynchronous with a status endpoint to poll. The properties that matter: a backup is consistent at its start; restore requires the collection not to exist, so a restore into a running instance means dropping first, which is a cutover step; and - the trap - the modules enabled on the restoring instance must include the ones the collections need, so restoring a text2vec-openai collection onto an instance without that module fails.

The same framing as the Pinecone course applies, and more strongly here: your vectors are derived data, so if you hold the source objects and the pipeline you can rebuild. With a vectoriser module the pipeline is partly in the schema, which makes a backup of the schema genuinely valuable - and it also means a restore reproduces the exact vectors, which a rebuild with a changed model would not.

Rolling upgrades work and need planning: Weaviate nodes in a cluster should run the same minor version, the schema is managed by the Raft layer so a mixed-version cluster during an upgrade is expected but not a steady state, and - the point that matters - read the release notes for index format changes, because a change that requires reindexing turns a rolling upgrade into a reimport. Take a backup first, always.

Laboratory exercise 5 - break it, then get it back.

Build a three-node cluster with Docker Compose. Create a collection with replication factor 3 and a deliberate shard count.

Demonstrate the shard count immutability: attempt to change it and capture the error. Then show that a tenanted collection's shard count grows with tenants.

Measure the consistency levels: write at ONE and read at ONE in a loop with a node under load, counting stale reads; then QUORUM/QUORUM and show zero. Record the latency of each combination. This is the Cassandra arithmetic in a vector database.

Kill a node under query and import load. Measure: queries continuing, the latency change, whether imports continued, and whether any acknowledged write was lost. Bring it back and measure the recovery.

Kill two of three and show the behaviour at each consistency level.

Configure the backup-filesystem module, take a backup, destroy the collection, and restore it. Record RPO and RTO, measured, and verify with a relevance measurement rather than an object count.

Demonstrate the restore traps: attempt a restore into an instance where the collection still exists, and attempt one onto an instance missing the vectoriser module. Capture both errors.

Then perform a rolling upgrade of one node and document the procedure, including the release-note check.

Deliverable: the cluster configuration, the shard immutability demonstration, the consistency level table with stale-read counts and latencies, the node loss measurements, the backup and restore with measured RPO and RTO verified by relevance, both restore traps, and the upgrade procedure.

PY Example 1 158 lines
# ==================================================================
# *** SHARDING: THE SHARD COUNT IS FIXED AT CREATION. ***
# ==================================================================
client.collections.create(
    name="ProductSharded",
    sharding_config=wvc.config.Configure.sharding(
        desired_count=4,          # *** DECIDE THIS ONCE. ***
        virtual_per_physical=128,
    ),
    replication_config=wvc.config.Configure.replication(factor=3),
    vectorizer_config=wvc.config.Configure.Vectorizer.text2vec_transformers(),
    properties=[...],
)
# *** WEAVIATE DOES NOT RESHARD. *** A collection created with two
# shards STAYS AT TWO SHARDS, and growing beyond them means A NEW
# COLLECTION AND A REIMPORT. So allow for growth rather than match
# current size. Demonstrate the immutability and capture the error.
print(products.config.get().sharding_config)

# *** MULTI-TENANCY INTERACTS WITH THIS WELL: a tenanted collection
# *** gives EACH TENANT ITS OWN SHARD, so THE TENANT COUNT IS THE
# *** SHARD COUNT and it GROWS AS TENANTS ARE ADDED. *** A second
# strong argument for the multi-tenant design, alongside the memory
# lifecycle from Module 3.

for node in client.cluster.nodes(output="verbose"):
    print(node.name, node.status, node.version)
    for s in node.shards:
        print("  ", s.collection, s.name, s.object_count, s.vector_indexing_status)

# ==================================================================
# *** PER-REQUEST CONSISTENCY LEVELS. The Cassandra arithmetic, in a
# *** vector database. ***
# ==================================================================
from weaviate.classes.config import ConsistencyLevel

#   RF=3:  W=QUORUM + R=QUORUM = 4 > 3   -> STRONG
#          W=ONE    + R=ONE    = 2 NOT > 3 -> a read CAN BE STALE
#
# *** SETTING IT PER REQUEST RATHER THAN GLOBALLY IS THE POINT: ***
# a product search can tolerate staleness and a stock check cannot.

search = client.collections.get("ProductSharded").with_consistency_level(
    ConsistencyLevel.ONE)          # fast; may be stale. Fine here.
r = search.query.hybrid(query="quiet keyboard", alpha=0.6, limit=10)

stock = client.collections.get("ProductSharded").with_consistency_level(
    ConsistencyLevel.QUORUM)       # strong. Correct here.
obj = stock.query.fetch_object_by_id(product_uuid)

writer = client.collections.get("ProductSharded").with_consistency_level(
    ConsistencyLevel.QUORUM)
writer.data.update(uuid=product_uuid, properties={"in_stock": False})

# THE MEASUREMENT - write then immediately read, 10,000 times, with
# a node under load:
#   W / R                 stale reads   write p95   read p95
#   ONE / ONE                 ___ %       ___ ms     ___ ms
#   QUORUM / ONE              ___ %       ___ ms     ___ ms
#   QUORUM / QUORUM             0 %       ___ ms     ___ ms
#   ALL / ONE                   0 %       ___ ms     ___ ms
# That table makes the per-request choice defensible rather than
# copied.

# ==================================================================
# BACKUP IS A MODULE. Which ones you enabled decides what you can do.
# ==================================================================
#   ENABLE_MODULES: '...,backup-filesystem'   BACKUP_FILESYSTEM_PATH: ...
#   or backup-s3 / backup-gcs / backup-azure

result = client.backup.create(
    backup_id="products-20261130",
    backend=weaviate.classes.backup.BackupStorage.FILESYSTEM,
    include_collections=["Product", "Brand"],
    wait_for_completion=True,
)
print(result.status)
#   It runs ONLINE and is ASYNCHRONOUS with a status endpoint:
print(client.backup.get_create_status(
    backup_id="products-20261130",
    backend=weaviate.classes.backup.BackupStorage.FILESYSTEM))

# RESTORE:
client.collections.delete("Product")        # <- *** REQUIRED ***
client.backup.restore(
    backup_id="products-20261130",
    backend=weaviate.classes.backup.BackupStorage.FILESYSTEM,
    include_collections=["Product"],
    wait_for_completion=True,
)

# *** THE TWO RESTORE TRAPS. Demonstrate both. ***
#
# 1. RESTORE REQUIRES THE COLLECTION NOT TO EXIST. So a restore into
#    a running instance means DROPPING FIRST - which is a CUTOVER
#    STEP, and a frightening one at 3 a.m. if the runbook does not
#    say so:
#      "collection Product already exists, cannot restore"
#    The safer pattern: restore into a DIFFERENT instance, verify,
#    then repoint - or accept the drop with a second backup taken
#    immediately before it.
#
# 2. *** THE MODULES ENABLED ON THE RESTORING INSTANCE MUST INCLUDE
#    *** THE ONES THE COLLECTIONS NEED. *** Restoring a
#    text2vec-openai collection onto an instance without that module
#    FAILS:
#      "no module with name 'text2vec-openai' present"
#    So the compose file is part of the backup. Put it in the
#    runbook, in the restore steps, above the restore command.

# ==================================================================
# THE DERIVED-DATA FRAMING - and why it is DIFFERENT here.
# ==================================================================
# As the Pinecone course: your vectors are DERIVED DATA, so if you
# hold the SOURCE OBJECTS and the PIPELINE you can REBUILD.
#
# *** AND WITH A VECTORISER MODULE THE PIPELINE IS PARTLY IN THE
# *** SCHEMA ***, which has two consequences:
#   1. a BACKUP OF THE SCHEMA is genuinely valuable, because it
#      records the model, the metric and the index parameters
#   2. *** A RESTORE REPRODUCES THE EXACT VECTORS, WHICH A REBUILD
#      *** WITH A CHANGED MODEL WOULD NOT *** - so here, unlike
#      Pinecone, restore and rebuild are NOT equivalent, and the
#      runbook should say which you are doing and why
#
# MEASURE BOTH:
#   restore from backup   RTO ___ min   vectors IDENTICAL
#   rebuild from source   RTO ___ min   vectors from the CURRENT
#                                        module version
print(json.dumps(products.config.get().to_dict(), indent=2))
#   ^ export the schema and version it in your repository. It is
#   small, it is the pipeline, and it is the thing a rebuild needs.

# *** VERIFY A RESTORE WITH A RELEVANCE MEASUREMENT, NOT AN OBJECT
# *** COUNT. *** A full collection with the wrong vectors looks
# exactly like a correct one:
print(products.aggregate.over_all(total_count=True).total_count)
print(evaluate(products, GROUND_TRUTH))      # <- THIS is the check

# ==================================================================
# ROLLING UPGRADES
# ==================================================================
#   1. *** TAKE A BACKUP. Always, first. ***
#   2. *** READ THE RELEASE NOTES FOR INDEX FORMAT CHANGES *** - a
#      change that requires REINDEXING turns a rolling upgrade into
#      a REIMPORT, and that is a different plan with a different
#      duration. This is the step people skip.
#   3. upgrade ONE NODE at a time, waiting for it to rejoin and
#      report healthy before the next
#   4. nodes in a cluster SHOULD RUN THE SAME MINOR VERSION - a
#      mixed-version cluster during an upgrade is EXPECTED, and NOT
#      A STEADY STATE
#   5. the schema is managed by the Raft layer, so verify schema
#      agreement after each node
#   6. re-run the relevance measurement afterwards
for node in client.cluster.nodes(output="verbose"):
    print(node.name, node.version, node.status)
client.close()

Open this example in the editor and change it. Nothing you type is saved.

Discussion

No questions on this topic yet. Sign in to ask the first one.