May 12, 2018

Infrastructure: API that needs to serve fast anywhere in the world. What's your set up?

Right now I have a server in France. It's too slow for American and Asian customers.

The overkill solution is to set up a server in each continent, and have some sort of server that redirects customers to their area server. This is not simple to set up, expensive and also hard to maintain, since I'd need to deploy changes in multiple servers, etc.

My question is, is there a solution (AMZ?) where you have a main server where you deploy the API and it takes care to replicate it to servers located in different areas?

I ask here because I believe answers won't be strictly technical, it should be a rather simple to set up/maintain solution for a small biz ;f


  1. 3

    I've been working on this with a client at work recently. Unfortunately there's no easy answer. Main options I've found are

    • use a CDN such as cloudflare, but this is difficult when the responses can change regularly

    • have different servers in different geographic locations (you can use route53 to choose the nearest server for a user https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/routing-policy.html#routing-policy-geoproximity)

    • design your app to be less dependent on the API. For example have syncing data to the server done in the background while the UI updates straight away. If this is a web application you can use service workers to do this.

    We've decided to move some of the API to AWS lambda which means we don't need to have servers running all over the world and we just pay for when the API is being used.

    Hope that helps.

    1. 1

      I have a similar setup:

      AWS Lambda + API Gateway + AWS Cloudfront.

      My static files are served by the Cloudfront CDN, via S3 buckets, and Cloudfront also points to my API Gateway.

      It's not "really" a CDN for your API, but you do speed up requests since once you hit the CDN (which should be geographically close to you), it goes through Amazon's network to your API.

    2. 1

      A CDN wouldn't help here since since it's for an API.

  2. 1

    For an API, you'd need to have global access to the underlying (globally distributed) db. That means you need a "global" db. Azure Cosmos db and Google Cloud Datastore and Spanner offer something like this.

    With Google App Engine you can have distributed front-ends to the db very easy.

    A similar setup is possible with firebase.

    However you'd probably have some problems doing this GDPR compliant.

  3. 1

    Firebase Auth + Firebase Functions + Firebase Storage = Serverless 0.2second response time

  4. 1

    I'm not aware of a solution that replicates servers in the way you're describing. One solution though is to use AWS Lambdas with an API Gateway. Building a caching layer may be a good idea. Depends on your speed requirements.

    I'd be happy to help if you need high-end, custom software development. You can contact me through https://bilimoria.io/contact

  5. 1

    Which database do you use? You can deploy a serverless app in 15 locations without any cost but (afaik) no DB

  6. 1

    So normally you want to clarify what anywhere in the world truly means for you and when it truly starts to matter. Ideally you would want your MVP to focus around one market (pilot) then your scaling strategy on tech should follow your “internatiolization”.

    I would advise relying on established cloud offering leader (Aws, Azure, Google cloud) to host your server apps/api as all of them provide regional strategies to help you get lowest latency for your customers.

    But to give optimal “recipe” for your setup / architecture you need to understand case specifics.

  7. 1

    What about AWS Lambda ?

  8. 1

    You should check Amazon Elastic Beanstalk. It might be a little expensive than normal EC2 instance but it comes with everything you need for your API.

  9. 1

    It depends on the application use case. Hard to give you recommendation without understand your requirements. But this might help: https://docs.microsoft.com/en-us/azure/architecture/reference-architectures/app-service-web-app/multi-region

  10. 1

    Also, are you setting caches correctly? May be you can cache the data so you don't have to hit your database or external resources unnecessarily.

  11. 1

    How about putting cloudflare in front on your server so cloudflare redirects user to nearest data center. You may set the Cache Expiry so CloudFlare can respond without request reaching your server.

    1. 1

      Cloudflare only works for frontend assets.

    2. 0

      James is right... CDN's only work for static assets. Only benefit of putting an API behind cloudflare is using their ASN to connect to whatever server is hosting your API and make it so that the end user only needs to connect to a cloudflare edge.