Skip to main content

mphf:build

Builds the element type index: a compact, read-only table (a minimal perfect hash) which maps an element id to its node label or relationship type, so that requests can address elements with a typed query and use the id indices created by index:ensure. The table needs about one byte per element, is stored in the system bucket (S3_SYSTEM_BUCKET, default ember-nexus-system, key mphf/<yyyymmdd-hhiiss>.mphf) and loaded into memory-mapped storage by every API instance.

How it works:

  1. The Redis overlay is switched. Elements created from now on are remembered in the newly active overlay key (mphf-overlay:A / mphf-overlay:B), so nothing gets lost while the table is built.
  2. All element ids are exported from the graph database, per label and relationship type, and the table is built.
  3. The table is uploaded and published by setting the Redis key mphf-version. API instances notice the new version within seconds and download it. Afterwards the previous overlay key is deleted and old table files beyond MPHF_RETENTION_FILES are removed from the bucket.

Alongside the table, the same export fills a Bloom filter of all element ids in Redis (element-id-filter:A / element-id-filter:B, about 2 bytes per element at the default error rate). Creating an element always checks that its id is not taken; if the filter says “definitely unknown”, which is true for nearly every new id, that check needs no database query, otherwise the database decides as before. The filter is only trusted once a build has completed it (element-id-filter:ready), new ids are added to it before the element is written, and a Redis without Bloom support or a missing filter key silently falls back to the database check. Deleted elements stay in the filter, which only costs a database check if their id is reused.

The command runs on every instance every 5 minutes, and most runs end right away. A run builds only if one of these is true:

  • the Redis overlay tracks more than mphf.overlayThresholdInElements elements (default 100000),
  • the time of the next scheduled build (Redis key mphf-next-build, a Unix timestamp) has come. A missing or invalid value counts as “has come”, and so does one which is further in the future than a build could ever have scheduled it,
  • the --force option is used.

After every build the next time is set to now + mphf.intervalInSeconds + mphf.varianceIntervalInSeconds * a random number between -1 and 1 (default 24 hours and 3 hours), so the builds of parallel instances spread over the day. Set mphf-next-build to the string false to disable the scheduled builds of a Redis (the overlay threshold and --force still apply); the value is then never changed by builds.

Builds are never started less than 2 seconds after the last one ended (mphf-last-build), not even with --force, as the files are named by the second.

The command takes the lock lock:maintenance:mphf, so only one build runs at a time; it is skipped (successfully) if another build, a backup load or a database reset is running. A failed build exits with a non-zero code and publishes nothing. Until a table exists the API resolves elements with a slower untyped query, so a fresh instance works without it.

Related environment variables are listed in the configuration reference.

Help

php bin/console mphf:build --help
Description:
  Builds and publishes a new element type index (id to node label / relationship type).

Usage:
  mphf:build [options]

Options:
  -f, --force           Build even if no build is due (the overlay is small and the next scheduled time has not come yet).
  -h, --help            Display help for the given command. When no command is given display help for the list command
      --silent          Do not output any message
  -q, --quiet           Only errors are displayed. All other output is suppressed
  -V, --version         Display this application version
      --ansi|--no-ansi  Force (or disable --no-ansi) ANSI output
  -n, --no-interaction  Do not ask any interactive question
  -e, --env=ENV         The Environment name. [default: "prod"]
      --no-debug        Switch off debug mode.
      --profile         Enables profiling (requires debug).
  -v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Example

php bin/console mphf:build --force
    ▄
  ▄▀ ▀▄  Ember Nexus API
   ▀█▀   v0.3.0, production mode

  MPHF build

  Switching the Redis overlay, new elements are tracked there from now on.
  Making sure id indices exist.
  Exporting element ids.
  Exported 0 ids.
  Building the index from 0 ids.
  Uploading to the system bucket.
  Element id filter is complete and in use.
  Version 20261204-014633.mphf: 0 ids, 640 bytes, 10.0 s, 0 old file(s) deleted.
▶ Done.