I am working on a web application as a hobby and trying to learn some concepts related to cloud development and distributed applications. I am currently targeting an AWS EC2 instance as a deployment environment, and while I don't currently have plans to deploy the same instance of my API application to many servers, I would like to design my application so that is possible in the future.

I have a search operation that I currently have implemented using a Trie. I am thinking that it would be slow to rebuild the trie every time I need to perform the search operation, so I would like to keep it in memory and insert into it as the search domain grows. I know that if I only wanted to have one server, I could just implement the trie structure as a singleton and dependency inject it. If I do this in a potentially distributed application, though, I would be opening myself up to data consistency issues. My thought was to implement the trie in another service and deploy it separately and make requests to it (this sounds like micro service concepts, but I have no experience with those). Is this common practice? Is there a better solution for maintaining persistent data structures in this way?


1 Answer 1


Implement the trie in another service, deploy it separately & make requests to it.

This is indeed the right way to do this & is a very common practice in distributed systems.

As you correctly understood, keeping an in-memory cache on a single node of a distributed system is not a good idea. Not only do you need to replicate & synchronize the in-memory data strcuture across all nodes at all times, you also have to plan for node failures, etc.

It makes perfect sense to separate out the in-memory cache from the nodes that run the API. This way:

  • It can scale independently as search queries grow in volume.
  • It can have its own dedicated compute & memory resources & not have to compete with the API for system resources.
  • It being a single central cache for the entire distributed system, it can be:
    • backed up independently & periodically
    • persisted to disk/S3
    • hydrated from disk/S3 when rebooted
    • systematically put through scheduled maintenance
    • using a dedicated software specializing in in-memory caching

2 of the most common in-memory caching systems are Memcached & Redis, & AWS has a service called ElastiCache that lets you deploy a Memcached and/or a Redis cluster, keep your in-memory data structures in it & make calls to it from your APIs in EC2. ElastiCache will fully manage the cluster for you.

Related Questions:

P.S. — Consider using an Elasticsearch cluster instead of a trie. AWS provides a managed service for it too — Amazon Elasticsearch Service. Here's a comparison between the 2 — Elasticsearch or Trie for search/autocomplete?


Not the answer you're looking for? Browse other questions tagged or ask your own question.