Simplicity
Simple and elegant systems are easier to design correctly, more efficient in execution, and more reliable. That simplicity requires hard work and discipline. Simplicity is not the first attempt. It is the hardest revision. It takes thought, multiple passes, and the willingness to throw work away. The goal is to find the idea that solves multiple problems at once.Technical debt
Unkey has a zero technical debt policy. Fix problems when they are discovered. Do not allow issues to slip through with a “fix it later” comment.Safety
Assertions
Crash the request, not the system. When an invariant is violated, return a 500 error immediately. Do not attempt to recover from programmer errors. Assertions downgrade catastrophic correctness bugs into liveness bugs. Assertions detect programmer errors, not user errors. A user sending invalid input should get a 400. Code reaching an impossible state should return a 500.Error handling
All errors must be handled. Never ignore errors. If you think an error cannot happen, assert that assumption explicitly.Scope
Declare variables at the smallest possible scope. Minimize the number of variables in play at any point. This reduces the probability of using the wrong variable and makes code easier to reason about. Calculate or check variables close to where they are used. Do not introduce variables before they are needed or leave them around when they are not.Failure
Unkey builds distributed systems. Networks partition. Disks fail. Processes crash. Memory runs out. Clocks drift. The question is not whether things will fail, but how gracefully they fail.Expect failure
Design for failure from the start. Every external call can fail. Every database query can time out. Every message can be lost. Write code that assumes these things will happen.Fail gracefully
Contain the blast radius. One bad request should not bring down the system. One slow dependency should not cascade into total unavailability. Use circuit breakers to stop calling failing dependencies and give them time to recover. Unkey providespkg/circuitbreaker for this.
Retry with backoff
Transient failures are common. Retry them, but retry intelligently. Unkey providespkg/retry for this.
Use exponential backoff so you do not hammer a struggling service. Add jitter to randomize retry timing and prevent thundering herds. Set budgets to limit total retry time.
Idempotency
Exactly-once delivery is a myth. Messages arrive zero times, once, or multiple times. Design operations to be safe to retry. Use idempotency keys for operations that must not be duplicated.Observability
Instrument critical paths. Log errors with context. Emit metrics for failure rates. Trace requests across service boundaries.Developer experience
Order matters
Order matters for readability even when it does not affect semantics. In Go files, exported functions come before unexported ones. The primary type or function that the file is named after comes first.Comments
Comments are sentences. Start with a capital letter and end with a period. Comments explain the reasoning, tradeoffs, and context that future readers need. For documentation guidance, see Documentation.Function length
Keep functions short. If a function does not fit on a screen, it is probably doing too much. Aim for functions under 50 lines. If a function grows to 200 lines, step back and consider whether it should be broken up.Naming
Great names capture what a thing is or does. Append qualifiers to names. Units, bounds, and modifiers come at the end. This groups related variables together and makes scanning easier.ctx, err, req, res, db, id as the exceptions.

