This is a simple cache server in python. I wrote about it here.
Here are the steps for the first time an item is requested:
- Client requests item
- Cache server checks if it's stored in the cache
- The item is not found
- The cache server requests the item from the main server
- The main server sends the item back
- The cache server saves a copy of the item
- The cache server sends the client the item
We could definitely swap 6 and 7, especially if we're trying to send the item back to the client quickly.
Here's a diagram of a subsequent request for the same item:.
Here are the steps for the future request:
- Client requests item
- Cache server checks if it's stored in the cache
- The item is found
- The cache server sends the client the item
Although it looks like the main server is lonely to the right, it didn't have to worry about the client's request at all. Lucky main server!
Here's another way of looking at the problem. Here, the vertical axis is time increasing as you go down, and the horizontal arrows are the requests back and forth. The same actions are taken in the same exact order, but the actions are separated vertically by when they happen:
First, start the simple http server from the main_server
directory:
python httpserver.py 8000
Then, start the cache proxy server in a separate terminal instance from the cache_server
directory:
python cacheproxy.py 8001
Then, navigate or curl to localhost:8001
.
Thanks to joaoventura for the simple http server code!