1.6. Mini-Project: Simple Key-Value Store in Go
Last updated
Last updated
In this mini-project, you'll design and implement a simple key-value store in Go. This project will help you apply the concepts we've discussed, including basic system design principles, trade-offs, and non-functional requirements.
Implement basic CRUD (Create, Read, Update, Delete) operations for key-value pairs.
Add simple persistence to disk.
Implement basic error handling and logging.
Consider and implement at least one aspect of scalability or performance optimization.
Set up the project:
Create a new Go project.
Set up a basic directory structure.
Implement in-memory storage:
Create a struct to represent your key-value store.
Implement methods for Get, Set, Update, and Delete operations.
Add persistence:
Implement functions to save the key-value store to a file.
Implement functions to load the key-value store from a file.
Decide when to persist (e.g., after each write operation or at regular intervals).
Implement error handling and logging:
Use Go's error handling mechanisms.
Implement basic logging to track operations and errors.
Optimize for performance or scalability:
Consider implementing one of the following:
A simple cache to speed up frequent reads.
Concurrent access using Go's synchronization primitives.
A basic sharding mechanism to distribute keys.
Write tests:
Implement unit tests for your CRUD operations.
Test error handling and edge cases.
Functional testing:
Test each CRUD operation individually.
Test combinations of operations.
Verify that data persists after restarting the program.
Error handling:
Test how your program handles invalid inputs.
Test scenarios like trying to read a non-existent key.
Performance testing (if implemented):
Test how your store performs with a large number of keys.
If you implemented concurrency, test with multiple simultaneous operations.
Persistence testing:
Verify that data is correctly saved to and loaded from disk.
Test scenarios like unexpected program termination.
As you work on this project, keep in mind the system design principles we've discussed:
Modularity: How can you structure your code to be modular and easy to understand?
Trade-offs: What trade-offs are you making in your design decisions?
Non-functional requirements: How does your implementation address requirements like performance, reliability, and scalability?
If you finish the basic implementation and want to challenge yourself further, consider:
Implementing a simple HTTP server to interact with your key-value store.
Adding a time-to-live (TTL) feature for keys.
Implementing a simple replication mechanism to store data on multiple "nodes".
Remember, the goal is to apply the concepts you've learned and gain practical experience. Don't worry about creating a production-ready system. Focus on understanding the design decisions you're making and their implications.
Good luck, and enjoy building your key-value store!