In this article, we’re going to discuss caching at the edge. We'll create a simple caching project with HarperDB to cache the result of an API call, and compare the speed of an un-cached api route vs. a cached api route.
You'll see that caching the result of an api request will speed up subsequent requests, and also make it less likely that our app gets rate limited.
- Understand caching: Caching is a technique to save data or web pages for quick access in the future, reducing the need for repetitive processing or fetching from a database.
- Introduction to caching at the edge: Learn about "caching at the edge," a modern technique that brings computation and data storage closer to the data source, improving response times and reducing bandwidth.
- Introducing HarperDB: HarperDB is a distributed application and database platform that enables low-latency access to datasets and applications by distributing them globally.
- Edge computing and CDNs: Explore the concept of edge computing, which brings the server closer to the user, and content delivery networks (CDNs), which cache static files worldwide.
- Why HarperDB is suitable for caching: HarperDB's distributed architecture reduces latency by placing the application and data closer to the end user, making it an ideal choice for edge caching.
- Installing and setting up HarperDB: Install HarperDB locally and set up HarperDB Studio to connect to your local instance.
- Creating a caching project with HarperDB: Create a schema and table in HarperDB to store cached data.
- Setting up a custom function project: Create a custom functions project in HarperDB Studio to define custom routes that interact with the HarperDB database.
- Creating an un-cached route: Implement a route that fetches a post from an external API on each request, without caching.
- Creating a cached route: Develop a route that checks if a post is cached in HarperDB and returns it if found. If not cached, fetch the post from the API, cache it in HarperDB, and return it.
- Testing the routes: Use tools like Postman to test the un-cached and cached routes, observing the difference in response times.
- Improving caching strategy: Discuss the possibility of adding an expiry date to cached data to ensure the cache remains up to date.
- Conclusion: HarperDB's edge caching capabilities can significantly improve application speed and reduce latency by distributing data and application logic closer to the user.