Skip to content

Marten: Storage Size ​

Encrypting personal data makes Marten documents larger. Plaintext names, addresses and dates compress well in PostgreSQL; ciphertext and blind indexes do not compress at all. This page covers where the extra bytes come from, how to reduce them, and how to plan a projection rebuild that has to write them.

Where the bytes go ​

An encrypted value carries a fixed 31-byte envelope (format version, key version, a 12-byte nonce and a 16-byte authentication tag) next to the ciphertext, which is as long as the plaintext. The whole is stored as Base64, which adds a third. A string member's value becomes about 4 × (31 + length) / 3 characters: a 9-character BSN is stored as 56 characters, and a 6-character last name as 52.

A non-string member (an int, a date, a record) is serialized to JSON first, then encrypted into its byte[]? companion the same way. Under Marten's System.Text.Json serializer the member itself is left out of the document, so only the companion is stored.

A blind index is 64 hex characters by default, for every index and for every element of an [ArrayBlindIndex].

Member names are stored in every document, and the names Tayra's companions use (ClientBsnIndex, NameSearchTermsIndex) are long.

The fixed envelope is what makes short values expensive: for a BSN it is three times the data. It cannot be shrunk safely, so the levers below reduce how many envelopes and indexes a document carries, and how long they are.

Use 128-bit blind indexes ​

This halves every blind index, in every document and in every database index over it, with no migration for existing data:

cs
services.AddTayra(opts => opts.BlindIndex.Use128BitIndexes = true);
anchor

See 128-bit Blind Indexes.

Encrypt a value object whole ​

[DeepPersonalData] encrypts every [PersonalData] member of a nested object on its own, so a four-member name pays the envelope four times:

cs
// One envelope per member: four envelopes for one name.
public class ClientName
{
    [PersonalData] public string Initials { get; set; } = "";
    [PersonalData] public string Prefix { get; set; } = "";
    [PersonalData] public string LastName { get; set; } = "";
    [PersonalData] public string PostalCode { get; set; } = "";
}

public class PatientWithDeepName
{
    public Guid Id { get; set; }

    [DataSubjectId]
    public string SubjectId { get; set; } = "";

    [DeepPersonalData]
    public ClientName Name { get; set; } = new();
}
anchor

When the object's members are never searched or masked on their own, put [PersonalData] on the member instead. Tayra serializes the whole object and encrypts it into one byte[]? companion, with one envelope:

cs
// One envelope for the whole name. The value object needs no attributes.
public record Name(string Initials, string Prefix, string LastName, string PostalCode);

public class PatientWithWholeName
{
    public Guid Id { get; set; }

    [DataSubjectId]
    public string SubjectId { get; set; } = "";

    [PersonalData]
    public Name? Name { get; set; }

    public byte[]? NameEncrypted { get; set; }
}
anchor

Measured on a Marten document with a four-member name, the document shrinks from 342 to 237 characters (356 to 245 bytes stored), even though the member names now sit inside the ciphertext. The more members the object has, the larger the saving.

What you give up:

  • No blind index on a member of the object, since only the whole object is encrypted. Keep [DeepPersonalData] for a value object with a blind-indexed member.
  • No masking or redaction per member. After crypto-shredding, the whole object is replaced, not each member.

Shorten stored member names ​

Companion names are stored in every document. [JsonPropertyName] stores a shorter one, and blind index lookups keep working, because Marten translates the query through the serializer:

cs
[PersonalData]
[BlindIndex]
public string ClientBsn { get; set; } = "";

// Stored as "bsnIx" in every document instead of "ClientBsnIndex".
[JsonPropertyName("bsnIx")]
public string? ClientBsnIndex { get; set; }
anchor

This saves the difference in name length per member per document, so it matters most for small documents with many companions.

Changing the stored shape ​

Rebuild or migrate, never just redeploy

Encrypting a value object whole and renaming a member both change the shape of the stored document. A document written in the old shape does not load correctly into the new model: a renamed member reads as empty and lookups on it miss, and an object that was encrypted member by member is not decrypted at all. Make these changes only to documents you rebuild from events, such as projections, or migrate the documents first. Turning on 128-bit blind indexes does not change the shape and needs neither.

Planning a projection rebuild ​

A rebuild rewrites every document it touches, and each rewrite goes through PostgreSQL's write-ahead log (WAL). With encrypted documents, that WAL is larger and compresses less, because ciphertext does not compress: wal_compression gains little on it. On a large store, the WAL a rebuild produces, together with the new tables, can fill a disk long before the rebuild finishes.

  • Rebuild into new tables. Marten's projection versioning writes a new version to separate tables while the old version keeps serving reads. Keep the old tables until the new ones are verified, then drop them.
  • Size the disk for the rebuild, not for the data. Measure the WAL a rebuild of one shard or tenant produces per document, and multiply by the number of documents before starting. Keep headroom for the old and the new tables at once.
  • Rebuild one database at a time when documents are spread across shards or tenant databases, and check free space between each.
  • Tune checkpoints for the rebuild. After every checkpoint, the first change to each page writes the whole page to the WAL. A larger max_wal_size and a longer checkpoint_timeout during the rebuild mean fewer checkpoints, and so fewer full pages written.
  • Prefetch keys. A rebuild decrypts every event, so fetch data keys in bulk: see bulk key prefetch and the rolling-window pacer.

Make storage changes, such as whole-object encryption or shorter names, part of a rebuild you were going to run anyway, so their cost is paid once.

See Also ​