Enforcing deterministic random ordering on ephemeral file systems

2021-07-27MODULE: database_engine

Generating random values with a seed is natively unsupported in SQLite databases. Therefore, writing an SQL query to retrieve the same random sequence consistently fails. The following implementation preserves the random order.

On ephemeral disks (e.g., Heroku), file systems do not persist post-restart. However, for retaining non-critical runtime data, the following protocol forces deterministic randomization.

Implementation

Assuming the target table is questions, a new column is required to store the static random data vector.

UPDATE questions SET random_order = Random();

All rows now possess a permanent randomized integer. The data can be queried and ordered predictably based on this column.

SELECT * FROM questions ORDER BY random_order

Result: A deterministically ordered random database sequence optimized for ephemeral environments.