The S3 Proxy Cache Service is a Go-based middleware that provides a caching layer in front of S3-compatible storage services. It implements a RESTful API using the standard net/http
package to handle object storage operations while maintaining an in-memory cache to improve read performance and reduce backend load.
- Provide a caching layer for S3-compatible storage services
- Reduce latency for frequently accessed objects
- Minimize backend storage load
- Maintain consistency between cache and storage
- Handle concurrent requests efficiently
- Follow clean architecture principles
graph TD
Client[Client] -->|HTTP Request| Middleware[Middleware Layer]
Middleware -->|Request Processing| Router[Router Layer]
Router -->|Route to Handler| Handlers[Handler Layer]
Handlers -->|Cache Check| Cache[In-Memory Cache]
Cache -->|Cache Hit| Handlers
Handlers -->|Cache Miss| Storage[Storage Layer]
Storage -->|Response| Handlers
Handlers -->|Update Cache| Cache
graph TD
subgraph API_Layer[API Layer]
Router[Router Package]
Middleware[Middleware Package]
Handlers[Handlers Package]
end
subgraph Cache_Layer[Cache Layer]
CacheEntry[CacheEntry Structure]
CacheManagement[Cache Management]
CleanupRoutine[Cleanup Routine]
end
subgraph Storage_Layer[Storage Layer]
MinioClient[Minio Client]
S3Operations[S3 Operations]
end
Router --> Handlers
Middleware --> Router
Handlers --> Cache_Layer
Cache_Layer --> Storage_Layer
.
├── cmd/
│ └── server/
│ └── main.go
└── internal/
├── middleware/
│ ├── logging.go
│ └── middleware.go
├── handlers/
│ ├── health.go
│ └── object.go
├── router/
│ └── router.go
├── cache/
│ └── cache.go
├── config/
│ └── config.go
└── storage/
└── storage.go
- Framework: Standard
net/http
- Components:
- Router setup and configuration
- Middleware chain management
- Route registration
- Logging middleware with request/response tracking
- Middleware chaining support
- Extensible middleware architecture
- Object Handler:
GET /objects/:bucket/*key
: Retrieve objects with cachingPUT /objects/:bucket/*key
: Upload objects and invalidate cacheDELETE /objects/:bucket/*key
: Remove objects and invalidate cacheHEAD /objects/:bucket/*key
: Retrieve object metadata with caching
- Health Handler:
GET /health
: Service health check
- Type: In-memory cache using
sync.Map
- Configuration:
- Default TTL: 5 minutes
- Maximum cache size: 100MB
- Cleanup interval: 1 minute
- Features:
- Thread-safe operations
- LRU-like eviction policy
- Automatic cleanup of expired entries
- Size-based eviction
- Compression support for large objects
- Client: Minio SDK
- Features:
- S3-compatible interface
- Configurable endpoints
- SSL support
- Error handling for common scenarios
type CacheEntry struct {
Data []byte
CompressedData []byte
ContentType string
Size int64
LastModified time.Time
ETag string
ExpiresAt time.Time
IsCompressed bool
}
S3_ENDPOINT
: S3-compatible storage endpoint (default: "localhost:9000")S3_ACCESS_KEY
: Access key for authentication (default: "minioadmin")S3_SECRET_KEY
: Secret key for authentication (default: "minioadmin")S3_USE_SSL
: Enable/disable SSL (default: "false")
minio-go
: S3 client SDKlog/slog
: Structured logging
- Description: Retrieves an object from cache or storage
- Parameters:
- bucket: Storage bucket name
- key: Object key path
- Response:
- 200: Success with object data
- 404: Object not found
- 500: Internal server error
- Headers:
- Content-Type: Object MIME type
- Content-Length: Object size
- Last-Modified: Object modification time
- ETag: Object entity tag
- Content-Encoding: gzip (when compressed)
- Description: Uploads an object and invalidates cache
- Parameters:
- bucket: Storage bucket name
- key: Object key path
- Request:
- Body: Object data
- Content-Type: Object MIME type
- Content-Encoding: gzip (optional)
- Response:
- 200: Success
- 400: Bad request
- 500: Internal server error
- Description: Removes an object and invalidates cache
- Parameters:
- bucket: Storage bucket name
- key: Object key path
- Response:
- 204: Success
- 404: Not found
- 500: Internal server error
- Description: Retrieves object metadata from cache or storage
- Parameters:
- bucket: Storage bucket name
- key: Object key path
- Response:
- 200: Success with metadata headers
- 404: Object not found
- 500: Internal server error
- Uses
log/slog
for structured JSON logging - Log levels: INFO, ERROR
- Key metrics logged:
- Request duration
- Response size
- Status code
- Remote address
- User agent
- Cache hit/miss ratio
- Cache size utilization
- Request latency
- Error rates
- Backend storage operations
- Uses static credentials for S3 authentication
- Supports SSL for secure communication
- Uses
sync.Map
for thread-safe cache operations - Implements mutex locks for cache size management
- Handles concurrent requests safely
- Separation of concerns with distinct packages
- Dependency injection for better testability
- Interface-based design
- Middleware chain pattern
- Size-based eviction
- Automatic cleanup routine
- Cache consistency
- Compression support for large objects
- Proper error propagation
- Specific error status codes
- Storage-specific error handling
- Graceful degradation
- In-memory caching
- Efficient memory usage
- Concurrent operations
- Compression support
-
Implementation Enhancements:
- Add request validation middleware
- Implement rate limiting
- Add cache statistics endpoints
- Implement request tracing
-
Security Enhancements:
- Add authentication middleware
- Implement bucket access controls
- Add request signing
- Enhance audit logging
-
Operational Enhancements:
- Add metrics middleware
- Implement distributed caching
- Add cache warmup strategies
- Include backup procedures
-
Performance Enhancements:
- Optimize compression strategies
- Implement partial object caching
- Add batch operations
- Include cache preloading
- Regular cache size monitoring
- Performance profiling
- Security updates
- Dependency management