Skip to content

Storage Overview

GoPotency requires a storage backend to persist request records and manage distributed locks. The choice of storage affects the performance, scalability, and durability of your idempotency system.

The storage backend handles two critical tasks:

  1. Result Caching: Storing the response body, status code, and headers so they can be replayed.
  2. Concurrency Control: Handling atomic locks to ensure that two identical requests don’t start processing at the exact same time.
BackendBest Use CaseDistributed?Complexity
In-MemoryDevelopment, Testing, Single NodeNoVery Low
RedisHigh-scale, Distributed APIsYesMedium
SQLPostgres/SQLite, minimal dependenciesYesMedium
GORMExisting GORM projectsYesLow

If you need to support a different database (e.g., MongoDB, DynamoDB), you can implement the Storage interface:

type Storage interface {
Get(ctx context.Context, key string) (*idempotency.Record, error)
Set(ctx context.Context, record *idempotency.Record, ttl time.Duration) error
Delete(ctx context.Context, key string) error
Exists(ctx context.Context, key string) (bool, error)
TryLock(ctx context.Context, key string, ttl time.Duration) (bool, error)
Unlock(ctx context.Context, key string) error
Close() error
}