Muhammad Saeed Raza (saeedraza.work@gmail.com)
A simple HTTP key-value store using only Python standard library. Thread-safe and ready for production use.
- In-memory key-value storage
- HTTP API with JSON responses
- Thread-safe operations
- Docker support
- No external dependencies
- Complete test coverage
# Start the server
python3 -m app.main
# Test the API
curl -X PUT http://localhost:8080/kv/hello \
-H "Content-Type: application/json" \
-d '{"value": "world"}'
curl http://localhost:8080/kv/hello
# Response: {"key": "hello", "value": "world"}# Build the Docker image
docker build -t px-kvstore .
# Run the container
docker run -p 8080:8080 px-kvstore
# Test the containerized service
curl http://localhost:8080/healthzPUT /kv/{key} - Create or update
curl -X PUT http://localhost:8080/kv/username \
-H "Content-Type: application/json" \
-d '{"value": "alice"}'Returns: 201 (created) or 200 (updated)
GET /kv/{key} - Get value
curl http://localhost:8080/kv/usernameReturns: {"key": "username", "value": "alice"} or 404
DELETE /kv/{key} - Remove key
curl -X DELETE http://localhost:8080/kv/usernameReturns: 204 (deleted) or 404
GET /healthz - Health check
curl http://localhost:8080/healthzReturns: {"ok": true}
# Run tests
python3 -m unittest discover tests/
# Or use make
make testSet these environment variables:
# Change port (default: 8080)
PORT=9000 python3 -m app.main
# Change host (default: 0.0.0.0)
HOST=127.0.0.1 PORT=9000 python3 -m app.main# Build and run
make docker-build
make docker-run
# Or manually
docker build -t px-kvstore .
docker run -p 8080:8080 px-kvstoreThings we could add later:
- Persistence: Save data to disk or database
- Authentication: API keys or user login
- Caching: LRU cache for better performance
- Logging: Better logs and monitoring
- Prefix search:
GET /kv?prefix=user:to find keys - TTL: Auto-delete keys after timeout
- Swagger docs: Interactive API documentation
- Rate limiting: Prevent too many requests
- Clustering: Multiple servers working together
- Backup: Save and restore data
""" Design an undo operation for your key-value store and server.
- The undo operation should be able to revert the latest change to the key-value store.
- There are no input parameters to this API.
- The only outputs should be a boolean/status
my_kvstore = {}
my_kvstore = { 'my_key': 100 }
my_kvstore = { 'my_key': 25 }
my_kvstore = {}
my_kvstore = { 'my_key': 25 }
my_kvstore = { 'my_key': 100 }
my_kvstore = {}
do nothing """