A caching service using Caddy + Gin + Redis with docker-compose
This service is used to cache all GraphQL responses from the main Tarkov API in order to provide maximum performance ⚡
This service exists to cache all response from the Tarkov API for performance and to reduce load on our cloudflare workers. It is written in GoLang and is as simple as it needs to be.
This service caches requests only for a short period of time in order to keep data fresh and response times low
This service works by doing the following:
- Recieving requests to save a graphql query in its in-memory cache (redis)
- Serving requests for cached graphql queries from its in-memory cache (redis)
- Expiring cached items at a fixed interval so they can be refreshed
Traffic flow:
- Request hits the reverse proxy (caddy)
- The request is routed to the backend caching service (FastAPI)
- The request can either be a GET (retrieves from the cache) or a POST (saves to the cache)
To use this repo do the following:
-
Clone the repo
-
Run the following command:
docker-compose up --build
-
Create a request to the cache endpoint to set an item in the cache:
curl --location --request POST 'http://localhost/api/cache' \ --header 'Content-Type: application/json' \ --data-raw '{ "key": "mycoolquery", "value": "fake response" }'
-
Create a request to retrieve the item you just placed in the cache:
curl --location --request GET 'http://localhost/api/cache?key=mycoolquery' \ --header 'Content-Type: application/json' \ --data-raw '{}'
-
As an added bonus, inspect your response headers to see how much longer the item will live in the cached before it expires and the request returns a 404 (
X-CACHE-TTL
)
That's it!
Caddy automatically provisions TLS certificates for you. In order to make use of this awesome feature, do the following:
-
Ensure your server has ports
80
and443
open -
Have a DNS record pointed to your server for the domain you wish to obtain a certificate for (e.g.
app.example.org
-><IP address>
) -
Export the env var for the domain you wish to use:
export DOMAIN=app.example.org
-
Start the docker-compose stack:
docker-compose up --build
-
Navigate to your domain and enjoy your easy TLS setup with Caddy! -> https://app.example.org
Here is some extra info about the setup!
The docker-compose file creates three volumes:
./data/caddy_data:/data
./data/caddy_config:/config
./data/redis:/data
The config volume is used to mount Caddy configuration and Redis data
The data volume is used to store certificate information. This is really important so that you are not re-requesting TLS certs each time you start your container. Doing so can cause you to hit Let's Encrypt rate limits that will prevent you from provisioning certificates
If you run the stack without the DOMAIN
variable set in your environment, the stack will default to using localhost
. This is ideal for testing out the stack locally.
If you set the DOMAIN
variable, Caddy will attempt to provision a certificate for that domain. In order to do so, you will need DNS records pointed to that domain and you will need need traffic to access your server via port 80
and 443