invariant
A rule that must always hold for an object or system, such as a count never being negative.
In more detail
A broken invariant means the program has a bug and its state can no longer be trusted. Enforcing invariants where objects are created means no invalid instance ever exists, and when an invariant spans several values that concurrent code updates, a lock or a single owner must protect it. Problems that can happen in normal operation, such as a missing file or an unreachable host, are not invariants and should be handled as ordinary errors.
Examples
In AWS: A rule about the data that must always hold true, such as an account balance never going below zero.Go: a broken invariant may panic; expected failures return an errorJava: a record's compact constructor throws IllegalArgumentException when itemCount is negative.Related terms
- transaction — A group of database changes that succeed or fail together, so the data is never left half-updated.
- relational database — A database that stores data in tables and links rows in one table to rows in another through shared key values.
- panic — A Go run-time failure that stops normal execution, runs deferred functions and, unless recovered, crashes the program with a stack trace.
- recover — The built-in Go function that, called inside a deferred function, stops a panic and returns the value it was raised with.
- error — In Go, an ordinary value of the built-in error type that a function returns to report that something went wrong.
- compact constructor — A record constructor written without a parameter list, which runs checks before the record's fields are assigned.
- domain exception — An exception named for a business situation, such as OrderMissing or StorageFailure, rather than for a technical cause.
- synchronized — The Java keyword that lets only one thread at a time run a block of code guarded by the same lock object.
- unique constraint — A constraint that no two rows may have the same value in a column or combination of columns.
- conditional write — A write that only succeeds if the current value still matches a stated condition, such as the key being absent.
- assertion function — A function whose return type is asserts condition or asserts val is Type, which throws if the check fails and otherwise narrows the caller's code.
Taught in
- When to Pick Which Database — Cloud Infrastructure & Hyperscaler Architecture
- Panics and Recover — Concurrent Systems Programming with Go
- Records And Data Classes — Modern Java: Virtual Threads & High-Throughput Services
- Checked Versus Unchecked Exceptions — Modern Java: Virtual Threads & High-Throughput Services
- The Cost Of Checked Exceptions — Modern Java: Virtual Threads & High-Throughput Services
- Concurrency Basics: Threads And Runnable — Modern Java: Virtual Threads & High-Throughput Services
- The Storage Decision: Relational Versus Document — Scalable System Design & Distributed Architecture
- The Storage Decision: Key Value, Search And Blob — Scalable System Design & Distributed Architecture
- Consistency, And The Truth About CAP — Scalable System Design & Distributed Architecture
- Monolith Versus Services: The Honest Trade Offs — Scalable System Design & Distributed Architecture
- Custom Type Predicates: Functions Returning is Type — Production TypeScript
- Assertion Signatures: asserts condition And Invariants — Production TypeScript
- Core Utilities: Partial, Required, Readonly, Pick And Omit — Production TypeScript