Sobes.tech
Middle — Senior

Как организовать обновление индекса в проде без downtime?

sobes.tech AI

Answer from AI

To update an index in production without downtime, the common strategies are "blue-green deployment" or "zero-downtime reindexing." The main idea is to create a new index alongside the current one, populate it with up-to-date data, and then switch traffic to the new index.

An approximate algorithm:

  1. Create a new index with an updated schema or data.
  2. Populate the new index with all necessary documents.
  3. Validate and test the new index.
  4. Switch requests from the current index to the new one (e.g., via alias or configuration).
  5. Delete or archive the old index.

This approach minimizes downtime and allows rollback to the old index if issues arise.

Example using Elasticsearch (pseudo-code):

# Create a new index
PUT /my_index_v2

# Populate the new index
POST /my_index_v2/_bulk
{...}

# Check the new index
GET /my_index_v2/_search

# Switch alias
POST /_aliases
{
  "actions": [
    { "remove": { "index": "my_index_v1", "alias": "my_index" }},
    { "add":    { "index": "my_index_v2", "alias": "my_index" }}
  ]
}

# Delete the old index
DELETE /my_index_v1

This approach is also applicable to other systems, including vector databases, where ensuring search continuity is important.