Problem
The similarity‑search service running on Google Compute Engine (GCE) instances intermittently returns IndexCorruptionError when executing nearest‑neighbor queries. Symptoms include:
- HTTP 500 responses from the
/searchendpoint. - Log entries such as
ERROR: index_load_failed: checksum mismatch (expected 0x3f2a..., got 0x7c1b...). - Inconsistent ranking results after a recent autoscaling event (instance added or removed).
- CPU usage spikes during index reload, followed by immediate termination of the worker process.
Root Cause
The service stores a disk‑based vector index (e.g., Faiss or Annoy) on a local SSD attached to each GCE instance. During an autoscaling event the instance group performs a rolling update:
- New instance boots, mounts a fresh local SSD, and copies the latest index artifact from Cloud Storage.
- The old instance is drained and its local SSD is detached.
- Because the index file is large (tens of GB) and the copy is performed with
gsutil cpwithout checksum verification, a partially written file can survive if the instance is terminated before the copy finishes. - The service starts before the copy completes, loads the truncated file, and marks the index as corrupted.
Two additional factors exacerbate the problem:
- Preemptible VM termination: Preemptible instances can be shut down with as little as a 30‑second warning, cutting off in‑flight copy operations.
- Filesystem caching: The OS buffers write operations; when the VM is killed the cache is not flushed, leaving the on‑disk file in an inconsistent state.
Debug
Log inspection
2026-11-22T14:03:12.345Z ERROR index_load_failed: checksum mismatch (expected 0x3f2a7c9d, got 0x7c1b2e5f)
2026-11-22T14:03:12.350Z INFO shutting down worker due to index error
2026-11-22T14:02:58.112Z INFO starting index download from gs://my-bucket/vectors.idx
2026-11-22T14:03:01.874Z WARN gsutil: Transfer interrupted (signal 15)
Instance metadata
$ curl -H "Metadata-Flavor: Google" \
http://metadata.google.internal/computeMetadata/v1/instance/attributes/autoscaling-group
my-similarity-group
Filesystem state check
$ sudo ls -lh /mnt/localssd/vectors.idx
-rw-r--r-- 1 root root 12G Nov 22 14:02 vectors.idx
$ sudo md5sum /mnt/localssd/vectors.idx
7c1b2e5f9d4a2e1c3f8b6a9d5e1f2c3d /mnt/localssd/vectors.idx
Network capture (optional)
A short tcpdump on port 443 showed the gsutil connection being reset exactly when the instance received the preempt signal.
12:03:01.123456 IP instance-1.compute.internal.443 > 169.254.169.254.80: Flags [R.], seq 0, ack 1, win 0, length 0
Reproducing the failure locally
Running the same copy command on a test VM and killing the process after 5 seconds reproduces the checksum mismatch.
$ gsutil cp gs://my-bucket/vectors.idx /tmp/vectors.idx &
[1] 12345
$ sleep 5 && kill -SIGTERM 12345
$ md5sum /tmp/vectors.idx
7c1b2e5f9d4a2e1c3f8b6a9d5e1f2c3d /tmp/vectors.idx
Solution
Atomic index deployment
Replace the direct copy with a two‑step process that guarantees a complete file before the service can read it.
# Before (faulty)
gsutil cp gs://my-bucket/vectors.idx /mnt/localssd/vectors.idx
service similarity start
# After (atomic)
TMP_PATH=$(mktemp /mnt/localssd/vectors.idx.XXXX)
gsutil -o "GSUtil:parallel_composite_upload_threshold=150M" cp \
gs://my-bucket/vectors.idx "$TMP_PATH"
if [ $? -ne 0 ]; then
echo "Index download failed"
exit 1
fi
# Verify checksum matches the object metadata
EXPECTED=$(gsutil stat gs://my-bucket/vectors.idx | grep "Hash (md5)" | awk '{print $3}')
ACTUAL=$(md5sum "$TMP_PATH" | awk '{print $1}')
if [ "$EXPECTED" != "$ACTUAL" ]; then
echo "Checksum verification failed"
rm -f "$TMP_PATH"
exit 1
fi
mv "$TMP_PATH" /mnt/localssd/vectors.idx
service similarity restart
Graceful shutdown hook
Configure the instance group manager to run a shutdown script that flushes the OS cache and waits for any active gsutil processes to finish.
# /etc/google-startup-scripts/shutdown-index.sh
#!/bin/bash
sync
# Wait up to 30 seconds for gsutil to exit
timeout 30 bash -c 'while pgrep -x gsutil >/dev/null; do sleep 1; done'
Use regional persistent disks for the index
Mount a regional-pd (replicated SSD) and store the index there. The disk survives instance termination, eliminating the need for per‑instance copy.
# Terraform snippet
resource "google_compute_disk" "vector_index" {
name = "vector-index-pd"
type = "pd-ssd"
size = 500
zone = var.region
replica_zones = ["${var.region}-a", "${var.region}-b"]
}
Verify
After applying the changes, perform the following checks:
- Trigger a manual scaling event (add a new instance) and watch the startup logs.
- Confirm the index download completes without errors and the checksum matches.
- Run a health‑check query:
$ curl -s http://localhost:8080/healthz
{"status":"ok","index_loaded":true}
- Execute a known‑vector nearest‑neighbor request and compare the result with a baseline.
$ curl -s -X POST http://localhost:8080/search \
-d '{"vector":[0.12,0.34,0.56,...]}' | jq .
{
"neighbors": [
{"id":"doc-123","score":0.987},
{"id":"doc-456","score":0.982}
]
}
Metrics to monitor:
| Metric | Expected value after fix |
|---|---|
| index_load_success_total | monotonically increasing |
| index_load_failure_total | zero |
| instance_preemptions | unchanged (preemptible behavior) |
Prevent
- Checksum‑validated deployments: Always verify object hashes after download.
- Atomic file replace: Use a temporary file and
mvto avoid partially written indexes. - Graceful preempt handling: Add a shutdown script that flushes caches and waits for background copy jobs.
- Prefer shared persistent storage for large read‑only artifacts; it removes per‑instance copy entirely.
- Monitoring: Alert on
index_load_failure_totalspikes and oninstance_preemptionsthat coincide with failures. - Versioned index objects: Include a semantic version in the object name (e.g.,
vectors.v1.4.idx) and store the expected version in instance metadata to avoid mismatched deployments.
Related Topic Hub: Cloud Infrastructure Troubleshooting Hub
FAQ
- Why does the index load succeed on some instances but not others?
Because the copy race only manifests when the instance is terminated before the download finishes. Instances that stay alive long enough see a complete file. - Can I keep using preemptible VMs for this workload?
Yes, provided you implement the atomic download and shutdown hook. The persistent‑disk approach also removes the dependency on instance lifetime. - Is there a way to detect a corrupted index without restarting the service?
Enable Faiss/Annoy integrity checks at load time (e.g.,faiss::Index::is_trained()) and expose the result via a health endpoint. - How do I know the checksum of the object in Cloud Storage?
Usegsutil stat gs://bucket/object | grep "Hash (md5)"or enable CRC32C verification withgsutil -o "GSUtil:check_hashes=always". - What if the index size exceeds the local SSD capacity?
Store the index on a regional persistent SSD and mount it read‑only on all instances; the same atomic replace logic applies.