Skip to content

belikedeep/caching-proxy-server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 

Repository files navigation

⚑ Caching Proxy Server

A high-performance, concurrent Go-based caching proxy server.

This project implements a CLI tool that acts as a caching proxy. It forwards requests to an origin server, caches the responses in-memory, and serves subsequent requests for the same URL directly from the cache, significantly reducing latency and origin load.

Go Version License: MIT


πŸš€ Key Features

  • Concurrent-Safe Caching: Uses sync.Mutex to ensure thread-safe access to the in-memory cache, allowing the proxy to handle multiple simultaneous requests efficiently.
  • CLI-Driven Configuration: Easily specify the proxy port and origin server via command-line flags.
  • Cache Management: Includes a dedicated flag to clear the cache, allowing for easy manual cache invalidation.
  • Cache Headers: Automatically adds X-Cache: HIT or X-Cache: MISS headers to responses to indicate cache status.

πŸ› οΈ Technical Implementation

The proxy is built using Go's standard library (net/http), emphasizing simplicity and performance.

Logic Flow:

  1. Request Interception: The proxy listens for incoming HTTP requests on the configured port.
  2. Cache Lookup: It checks its internal map for a cached response for the specific URL.
  3. HIT: If found, it returns the cached body immediately with an X-Cache: HIT header.
  4. MISS: If not found, it fetches the resource from the origin server, stores it in the cache for future use, and returns it with an X-Cache: MISS header.

πŸ“– Usage

1. Installation

git clone https://github.com/belikedeep/caching-proxy-server.git
cd caching-proxy-server
go build -o caching-proxy

2. Running the Proxy

Start the proxy on port 3000, forwarding to https://api.github.com:

./caching-proxy --port 3000 --origin https://api.github.com

3. Testing

# First request (MISS)
curl -i http://localhost:3000/users/belikedeep

# Second request (HIT)
curl -i http://localhost:3000/users/belikedeep

4. Clearing the Cache

./caching-proxy --clear-cache

🎯 Engineering Considerations

  • Memory Management: Currently uses an unbounded in-memory map. For production use, a Least Recently Used (LRU) eviction policy would be implemented to prevent memory exhaustion.
  • Timeout Handling: Implements basic error handling for origin server connectivity.
  • Scalability: The use of Go's http.ListenAndServe provides a solid foundation for handling concurrent connections.

βš–οΈ License

MIT - See LICENSE for details.

About

Concurrent Go-based proxy server with thread-safe in-memory storage and CLI cache management.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages