Skip to main content

Infrastructure

A Vox installation needs a Vox database and a Nexus database. Redis, a search engine, subscription transports and a Vox Serve projection store are optional, each added for a need. This page says what each requires and how the Vox database is migrated in production.

Databases​

The Vox database holds the entities and Vox's own records of the work each change causes: dependencies, the change journal, memberships, publication state, subscriptions and outboxes. Those records are part of what keeps Vox correct, not only bookkeeping: a committed change leaves durable evidence of everything it may invalidate, so the background work can be delayed and retried without missing anything, whatever isolation level the database uses, eg read committed snapshot on SQL Server. See dependency and change tracking.

The Nexus database holds the queues and jobs of imports and projection publications. On SQL Server and PostgreSQL it's Vox's database unless ConnectionStrings:Nexus names another, which a busy installation does, see Nexus's database.

Vox's storage runs on these databases, each with a package of its own that connects both Vox and Nexus, see installation:

  • SQL Server with database compatibility level 130 (SQL Server 2016) or later, since Vox uses OPENJSON: AddSqlServerStorage() from CommerceMind.Vox.SqlServer. A database that the startup creates has read committed snapshot on.
  • PostgreSQL: AddPostgresStorage() from CommerceMind.Vox.Postgres.
  • SQLite, for development and small installations: AddSqliteStorage() from CommerceMind.Vox.Sqlite. A SQLite database file of Vox needs Pooling=False in its connection string, which the package adds: Vox's parallel workers otherwise get the same connection from the pool, see installation. Nexus gets a file of its own next to Vox's, eg vox-nexus.db, in WAL mode: in SQLite's default journal mode, a Nexus statement that commits while another connection reads can fail with "database is locked" and fail its job run.

Another provider or setup registers Vox's Entity Framework storage itself with vox.AddEntityFrameworkStorage(options => options.ConfigureDbContext = ...), and Nexus's connection with vox.ConfigureNexus(...).

For the database rights that Vox needs, see security.

Storage per entity type​

Every entity type has exactly one authoritative storage, which every read of it reads. Storage is deployment configuration rather than part of an entity class, so the same model can use different infrastructure in different applications, eg Redis for carts in production and SQLite on a developer's machine:

var vox = builder.AddVox(options =>
{
// Sessions in a Redis of their own; carts go to the transient storage
options.EntityStorage.UseStorage<BrowserSession>("sessions");
});

vox.AddSqlServerStorage();

vox.AddRedisEntityStorage("redis", options =>
options.ConnectionString = redisConnectionString);

vox.AddRedisEntityStorage("sessions", options =>
options.ConnectionString = sessionsRedisConnectionString);

An entity type is stored in the storage that UseStorage selects for it. Otherwise a transient entity type is stored in the transient storage, and every other entity type in the default storage. Entity Framework storage is the default storage unless another is set (EntityStorage.DefaultStorageName), and the first Redis entity storage is the transient storage unless another is set (EntityStorage.TransientStorageName). Without a transient storage, transient entity types are stored in the default storage, under the same rules. Vox validates the selections when it's built, as the host is built.

A Vox transaction can only change entities of one storage: one that changes a cart in Redis and a product in Entity Framework storage fails, see update several entities atomically.

Redis for transient entities​

Redis stores only transient entity types: large, short-lived working state such as carts, checkouts, reservations and sessions, read by id or by a query that one index answers. Building Vox rejects an ordinary entity type stored in Redis with an error that names the type and the storage. How transient entities behave in Redis, eg what's immediately consistent and what follows, is described with them, see where transient entities are stored.

Run Redis with maxmemory-policy noeviction. An evicting policy, eg volatile-lru, the default of Azure Cache for Redis, silently removes carts before their lifetime has passed. Vox logs an error when the Redis entity storage first connects to a server with another policy.

Changes of Redis entities that subscriptions or side effects need reach the Vox database through background work on one processing host (vox_entity_storage_outbox), right after each write. If that work fails, eg while the Vox database is down, subscribers get the changes late, not never.

Connections and key spaces. Redis storage takes either a StackExchange.Redis connection string or an application-owned connection factory:

vox.AddRedisEntityStorage("redis", options =>
{
options.ConnectionFactory = services =>
services.GetRequiredService<IConnectionMultiplexer>();
options.Database = 2;
options.KeyPrefix = "shop:vox";
});

Connections that Vox creates are shared for the lifetime of the application. Connections returned by a factory stay owned by the application. Each named registration has its own key namespace, also when several use the same Redis server.

Search and subscription transports​

Elasticsearch is an optional search index for queries, see querying and search. RabbitMQ, Redis streams and webhooks are transports of subscriptions, see events. Both are updated after a change commits, outside its transaction, by the background work. Vox Serve's projection store, Redis or SQL, is described with Vox Serve.

Migrating in production​

Vox applies the model in code to the stored data, see model evolution: it compares the model with the one that was applied, plans the difference, and stops before changing anything if a difference is ambiguous. Tables and columns that are no longer used are kept unless AllowDestructiveMigrations is set.

By default every host applies the model at startup. In production, apply it in the pipeline instead, while the previous release still serves traffic, and let the hosts only check it:

dotnet MyApp.dll vox migrate
vox.AddSqlServerStorage(storage => storage.ConfigureEntityFramework = entityFramework =>
// Check that the pipeline applied the model in code, and load it, instead of migrating
entityFramework.RequireAppliedModel = true);

RunVoxAsync runs the command, with the same Program.cs and configuration as the hosts, and exits.

The hosts then fail their start if the model in code hasn't been applied. When the pipeline migrated with the same build, a host's start changes no schema, so the hosts can run with a database user that can't change it, see security. vox migrate --plan shows what a migration would do, and which changes are incompatible with the running release, see deployments.

Long work, eg converging stored documents or checking a new unique constraint for duplicates, runs afterwards on the processing hosts, in bounded batches that resume after a restart. Its progress, the capabilities still being built and the values that couldn't be converted are on the Model page of the admin UI and at GET /vox/api/model. A large migration needs space for both the old and the new data until the old is dropped, and adds database I/O and transaction log: run it when the database has the capacity.

A start where nothing changed is fast. Entity Framework storage stores a key of what its tables were built from, and compares the schema only when the key differs, and then only the tables that changed. The key doesn't cover what the application's ConfigureDbContext changes in how Entity Framework builds the model, eg conventions of its own. After changing those, force a full comparison by deleting the stored key, which is always safe:

DELETE FROM Vox_Schema WHERE Id = 3

with the application's table prefix if it isn't Vox_.

Resetting a development database​

A database from a version of Vox whose storage can't be migrated is rejected at startup with an error that names the table, rather than upgraded partly. Such a development database is reset:

  1. Stop every application and worker that uses it, including the other Vox applications it synchronizes with.
  2. Drop the database, or point the application at a new one, and start it, which creates the current schema. Recreate the seed or sample content and the synchronized applications' state.
  3. Remove the queued imports of the earlier version through the Nexus queues' own tools: dropping the database doesn't remove them. The rest of the pending work, eg calculations, scheduled publications and subscription deliveries, is in the Vox database and goes with it. A message of an earlier format that still arrives is rejected as obsolete.
  4. Clear what was derived from the old content outside the database, eg search indexes and delivered documents, issue new preview grants, and update the API clients' default scopes.
  5. Start the applications and workers together, so that they rebuild their derived state and synchronize new snapshots, and recreate the schedules and releases that are still wanted.

Only the Vox state of the installation is reset: other queues and the data of other applications are left alone.