Documentation
¶
Overview ¶
Package consistency provides utilities for handling eventual consistency in DynamoDB
This package offers several patterns for dealing with DynamoDB's eventual consistency:
1. ConsistentRead() - Use strongly consistent reads on main table queries 2. WithRetry() - Retry queries with exponential backoff for GSI eventual consistency 3. ReadAfterWriteHelper - Patterns for handling read-after-write scenarios
Example usage:
// Strong consistency on main table
err := db.Model(&User{}).
Where("ID", "=", "123").
ConsistentRead().
First(&user)
// Retry for GSI eventual consistency
err := db.Model(&User{}).
Index("email-index").
Where("Email", "=", "user@example.com").
WithRetry(5, 100*time.Millisecond).
First(&user)
// Read-after-write pattern
helper := consistency.NewReadAfterWriteHelper(db)
err := helper.CreateAndQueryGSI(
&user,
"email-index",
"Email",
"user@example.com",
&result,
)
Index ¶
- func RecommendedGSIPropagationDelay() time.Duration
- func RetryWithVerification(ctx context.Context, query core.Query, dest any, verify func(any) bool, ...) error
- type BestPractices
- type ConsistencyStrategy
- type ConsistentQueryBuilder
- type QueryAfterWriteOptions
- type ReadAfterWriteHelper
- func (h *ReadAfterWriteHelper) CreateWithConsistency(model any, opts *WriteOptions) error
- func (h *ReadAfterWriteHelper) QueryAfterWrite(model any, opts *QueryAfterWriteOptions) *ConsistentQueryBuilder
- func (h *ReadAfterWriteHelper) UpdateWithConsistency(model any, fields []string, opts *WriteOptions) error
- type RetryConfig
- type RetryableQuery
- type WriteAndReadPattern
- type WriteOptions
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RecommendedGSIPropagationDelay ¶
RecommendedGSIPropagationDelay returns the recommended delay for GSI propagation Based on AWS documentation, GSIs typically propagate within seconds
Types ¶
type BestPractices ¶
type BestPractices struct{}
BestPractices provides recommended patterns for different scenarios
func (*BestPractices) ForCriticalReads ¶
func (bp *BestPractices) ForCriticalReads() ConsistencyStrategy
ForCriticalReads returns the recommended approach for critical reads
func (*BestPractices) ForGSIQuery ¶
func (bp *BestPractices) ForGSIQuery() ConsistencyStrategy
ForGSIQuery returns the recommended approach for GSI queries after writes
func (*BestPractices) ForHighThroughput ¶
func (bp *BestPractices) ForHighThroughput() ConsistencyStrategy
ForHighThroughput returns the recommended approach for high-throughput scenarios
type ConsistencyStrategy ¶
type ConsistencyStrategy int
ConsistencyStrategy defines different approaches to handle consistency
const ( // StrategyStrongConsistency uses ConsistentRead on main table StrategyStrongConsistency ConsistencyStrategy = iota // StrategyRetryWithBackoff retries queries with exponential backoff StrategyRetryWithBackoff // StrategyDelayedRead waits before reading to allow propagation StrategyDelayedRead // StrategyMainTableFallback falls back to main table if GSI fails StrategyMainTableFallback )
type ConsistentQueryBuilder ¶
type ConsistentQueryBuilder struct {
// contains filtered or unexported fields
}
ConsistentQueryBuilder builds queries with consistency options
func (*ConsistentQueryBuilder) All ¶
func (b *ConsistentQueryBuilder) All(dest any) error
All executes the query and returns all results with consistency handling
func (*ConsistentQueryBuilder) First ¶
func (b *ConsistentQueryBuilder) First(dest any) error
First executes the query and returns the first result with consistency handling
func (*ConsistentQueryBuilder) Index ¶
func (b *ConsistentQueryBuilder) Index(indexName string) *ConsistentQueryBuilder
Index specifies the index to use
func (*ConsistentQueryBuilder) Where ¶
func (b *ConsistentQueryBuilder) Where(field string, op string, value any) *ConsistentQueryBuilder
Where adds a condition to the query
type QueryAfterWriteOptions ¶
type QueryAfterWriteOptions struct {
// UseMainTable forces the query to use the main table instead of GSI
UseMainTable bool
// RetryConfig configures retry behavior for GSI queries
RetryConfig *RetryConfig
// VerifyFunc custom verification function to check if data is consistent
VerifyFunc func(result any) bool
}
QueryAfterWriteOptions configures read-after-write query behavior
type ReadAfterWriteHelper ¶
type ReadAfterWriteHelper struct {
// contains filtered or unexported fields
}
ReadAfterWriteHelper provides patterns for handling read-after-write consistency
func NewReadAfterWriteHelper ¶
func NewReadAfterWriteHelper(db core.DB) *ReadAfterWriteHelper
NewReadAfterWriteHelper creates a new helper instance
func (*ReadAfterWriteHelper) CreateWithConsistency ¶
func (h *ReadAfterWriteHelper) CreateWithConsistency(model any, opts *WriteOptions) error
CreateWithConsistency creates an item and handles consistency
func (*ReadAfterWriteHelper) QueryAfterWrite ¶
func (h *ReadAfterWriteHelper) QueryAfterWrite(model any, opts *QueryAfterWriteOptions) *ConsistentQueryBuilder
QueryAfterWrite performs a query with read-after-write consistency handling
func (*ReadAfterWriteHelper) UpdateWithConsistency ¶
func (h *ReadAfterWriteHelper) UpdateWithConsistency(model any, fields []string, opts *WriteOptions) error
UpdateWithConsistency updates an item and handles consistency
type RetryConfig ¶
type RetryConfig struct {
MaxRetries int
InitialDelay time.Duration
MaxDelay time.Duration
BackoffFactor float64
RetryCondition func(result any, err error) bool
}
RetryConfig configures retry behavior for eventually consistent reads
func DefaultRetryConfig ¶
func DefaultRetryConfig() *RetryConfig
DefaultRetryConfig returns a default retry configuration
func RecommendedRetryConfig ¶
func RecommendedRetryConfig() *RetryConfig
RecommendedRetryConfig returns a recommended retry configuration
type RetryableQuery ¶
type RetryableQuery struct {
// contains filtered or unexported fields
}
RetryableQuery wraps a Query with retry capability for eventual consistency
func WithRetry ¶
func WithRetry(query core.Query, config *RetryConfig) *RetryableQuery
WithRetry adds retry capability to a query for handling eventual consistency
func (*RetryableQuery) All ¶
func (r *RetryableQuery) All(dest any) error
All executes the query with retries
func (*RetryableQuery) First ¶
func (r *RetryableQuery) First(dest any) error
First executes the query with retries
type WriteAndReadPattern ¶
type WriteAndReadPattern struct {
// contains filtered or unexported fields
}
WriteAndReadPattern demonstrates a complete write-then-read pattern
func NewWriteAndReadPattern ¶
func NewWriteAndReadPattern(db core.DB) *WriteAndReadPattern
NewWriteAndReadPattern creates a new pattern helper
func (*WriteAndReadPattern) CreateAndQueryGSI ¶
func (p *WriteAndReadPattern) CreateAndQueryGSI(item any, gsiName string, gsiKey string, gsiValue any, dest any) error
CreateAndQueryGSI creates an item and queries it via GSI with retry
func (*WriteAndReadPattern) UpdateAndVerify ¶
func (p *WriteAndReadPattern) UpdateAndVerify(item any, fields []string) error
UpdateAndVerify updates an item and verifies the update with strongly consistent read
type WriteOptions ¶
type WriteOptions struct {
// WaitForGSIPropagation adds a delay after write to allow GSI propagation
WaitForGSIPropagation time.Duration
// VerifyWrite performs a strongly consistent read after write to verify
VerifyWrite bool
}
WriteOptions configures write behavior