What is the best architecture for a web-app communicating with a gRPC service? - web

I have built a website with chess.js and java chess libraries that communicates with a custom c++ chess engine via gRPC with python. I am new to web dev and especially gRPC, so I am not sure on the architecture I should be going for when it comes to hosting.
My questions are below:
Do the website and gRPC service need to be hosted on separate server instances and connected via API?
Everything right now is hosted locally and I use two ports as it is right now (5000 for the website and 8080 for the server). If the site and server aren't separate, is this how they will communicate to each other on a single server (one local port)?
I am using this website just for a showcase of my portfolio for job searching, so I am looking for free/cheap hosting that also provides a decent RAM availability since the c++ chess engine is fairly computationally intense. Does anyone have any suggestions for what hosting service I should use for this?
I was considering a free hosting for the website and then a cheap dedicated server for the service (if the two should be separate). Is this a bad idea?
Taking all tips and tricks that anyone has to offer. Again, totally novice to web dev, hosting, servers, etc.

NOTE This is an architecture rather than a programming question and discouraged on stack overflow.
The website and gRPC service may be hosted on the same server (as you're doing locally). You have the flexibility in running both processes (website and gRPC service) on a single more powerful host or separately on two hosts.
NOTE Although most often gRPC communicates over TCP sockets, it is possible to use UNIX sockets and even buffered memory too.
If you run both processes on a single host, you will want to consider connecting the website to the gRPC service via localhost (127.0.0.1 or the loopback device). Using localhost, network traffic doesn't leave the host.
If you run both processes on different hosts, traffic must travel across a network. This is slower and will likely incur charges when hosted.
You will want to decide whether the gRPC service should be exposed to any network traffic other than your website. In many cases, a gRPC service is used to provide an API to facilitate integration by 3rd-parties. If you definitely don't want the gRPC service accessed by other things, then you'll want to ensure either that it's bound to localhost (see above; and thereby inaccessible to anything other than other processes e.g. your website on the host) or firewalled such that only the website is permitted to send traffic to it.
You can find cheap hosting of virtual machines (VMs) and you'll likely want to consider hosting both processes on a single VM, ensure that you constrain the resources that you pay for and that you secure traffic (as above).
You may wish to consider containerizing the application. In this case, while it's possible to run both processes in a single container, this is considered not good practice. You should thus consider 2 containers (website and gRPC server). Many hosting|cloud platforms provide container hosting and this is generally easier than managing VMs (since you don't need to patch|update the OS and any dependencies). If you can find a platform that accepts a Docker Compose describing or a Kubernetes Deployment in which you describe both your services and how they interact such that the gRPC service is only accessible to the website, that could be ideal.

Related

NodeJS TLS/TCP server in need of an external firewall

Problem:
I have an AWS EC2 instance running FreeBSD. In there, I'm running a NodeJS TLS/TCP server. I'd like to create a set of rules (in my NodeJS application) to be able to individually block IP addresses programmatically based on a few logical conditions.
I'd like to run an external (not on the same machine/instance) firewall or load-balancer, that I can control from NodeJS programmatically, such that when certain conditions are given, I can block a specific remote-address(IP) before it reaches the NodeJS instance.
Things I've tried:
I have initially looked into nginx as an option, running it on a second instance, and placing my NodeJS server behind it, but after skimming through the NGINX
Cookbook
Advanced Recipes for High Performance
Load Balancing I've learned that only the NGINX Plus (the paid version) allows for remote/API control & customization. While I believe that paying $3500/license is not too much (considering all NGINX Plus' features), I simply can not afford to buy it at this point in time; in addition the only feature I'd be using (at this point) would be the remote API control and the IP address blocking.
My second thought was to go with the AWS/ELB (elastic-load-balancer) by integrating AWS' SDK into my project. That sounded feasible, unfortunately, after reading a few forum threads and part of their documentation (unless I'm mistaken) it seems these two features I need are not available on the AWS/ELB. AWS seems to offer an entire different service called WAF that I honestly don't understand very well (both as a service and from a feature-stand-point).
I have also (briefly) looked into CloudFlare, as it was recommended in one of the posts, here on Sackoverflow, though I can't really tell if their firewall would allow this level of (remote) control.
Question:
What are my options? What would you guys recommend I did?
I think Nginx provide such kind of functionality please refer to link
If you want to block an IP with Node TCP you can just edit a nginx config file and deny IP address.
Frankly speaking, If I were you, I would use AWS WAF but if you don’t want to use it, you can simply use Node JS
In Node JS You should have a global array variable where you will store all blocked IP addresses and upon connection, you will check whether connected host IP is in blocked IP variable. However there occurs a problem when machine or application is restarted, you will lose all information about blocked IP-s. So as a solution to that you can just setup Redis (It is key-value database but there are also other datatypes) DB and store blocked IP-s there. Inasmuch as Redis DB is in RAM all interaction with DB will be instantly and as long as machine or node is restarted, Redis makes a backup on hard drive and it syncs from it and continue to work in RAM with old databases.

How to host Node.Js server and PostgreSQL database from my computer?

I want to host my own server and database on my computer, I don't want to pay monthly for services.
I developed a node.js app and it's using a postgresql database. I have a domain with an angular app and the app needs to use data from the server.
Can someone tell me how I can do this and which OS would be the best?
Thanks!
You have to do few things for that to work.
First, your Angular app needs to be able to connect with your home server so it either needs a static IP address accessible from the outside, a dynamic IP with dynamic DNS, or a VPN.
Your server needs to properly support CORS so that your Angular app would be able to connect with it. It will send OPTIONS requests that your server needs to handle properly.
Make sure that your server is always on, the internet connection is reliable, the power is reliable and that your services are properly restarted on reboot.
Make sure that your server is always up to date with security patches, is configured properly and doesn't use any unneeded software and services.
For (1) you have a lot of options and it all depends on whether you have a static or dynamic IP address, whether it is accessible from the internet etc. which you didn't include in your answer.
For (2) it depends on what Node framework do you use for you server-side application which you didn't include in your question. You need to use a way to set up CORS that is specific for the framework that you use.
The (3) is hard in home environment but it's important because on any downtime your users will not be able to use your application.
The (4) is critical in home environment because if anyone breaks into your server, he'll have access to your home network which may have a different kinds of consequences that breaking into a data center.
Another option would be to use a cheap VPS provider like Digital Ocean where you can get a server for $5 a month (or 2 months for free with this link) which may be less hassle that setting up your own server - for which you have to pay for electricity, manage the hardware, monitor the connectivity etc.
If you choose a VPS then (1) us taken care for you - you get your own static IP address accessible from the world, (3) is taken care for you completely, (4) is relatively easy to do and the biggest issue is making sure that CORS works as it should - but here you can host your API on the same domain as your frontend and then you don't need to worry about CORS at all.
If you get a VPS then you can host your frontend Angular app from the same server so that it doesn't even have to cost you more.

Redis deployment configuration - master slave replication

Currently I have two servers which I have deployed node.js/Express.JS based web services API. I am using Redis for caching the JSON strings.
What will be the best option deploying this setup in to production? I see here it advices to go with a dedicated server redis. OK. I take it and use a dedicated server for running redis master. Can I use existing app servers as slave nodes? Note : these app servers are running an Node/Express application.
What other other options do I have?
You can.
It all depends on the load that those other servers have, it's a problem of resource sharing. To be honest my main issue with your architecture is not the dedicated vs the non-dedicated servers, it's the fact that you are placing a Redis server (master or not) on a host that most likely will be facing the internet (expressJS app), meaning, it's quite exposed.
If you can simulate HTTP load into your Node/Express JS servers, see the difference between running some benchmark tests on your dedicated server vs the non dedicated ones:
On a running redis server type in:
redis-benchmark -q -n 100000
If the app servers are being hammered and using all cores frequently you should see a substantial difference in the benchmarks.
My suggestion is, go ahead with your first setup and add monitoring for the redis response times, and only act when you have to, which might be now if the benchmarks show very poor results.
As a side note, consider the option of not sharing hosts for services that you expose to the internet with services that perform internal functions to your application.

Why node.js can't run on shared hosting? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
First thing: I searched all of the known web hosting companies for shared hosting of nodejs but I didn't find anyone. Then I came to know that nodejs cannot run on a shared host system. I want to know why?
Second thing: I am a normal guy with a normal budget. Choosing a vps or deicated server or cloud hosting makes the node run, but its out of my pocket money range as compared to the PHP shared hosting services, so should I learn node.js?
Theoretically it can, but practically it depends on hosting provider to have such infrastructure in place.
Node comparing to classic web platforms is self-sustainable platform. In case with PHP (for example), it runs on with of apache or nginx (or any other), and PHP it self is just script language with some libraries that does not do much apart of logic implementation, and requires web server solution. Web server creates socket to listen specific ports for traffic, will do its own magic and will execute PHP to process requests.
In meantime node.js creates own socket, and binds it to own port. That gives it much more low-level access, so it is web server it self. You can't bind to one port two applications, so it already unsharable.
There are services (web servers) that allow you to create proxy to route traffic to your node.js process but that is not as efficient in some cases, and shared hosting does not provide such functionality.
As node.js is still fairly young as well as is well, different, it still did not hit majority of shared hosting services. There are some available services online to host your node.js applications in a 'shared' manner.
Additionally you can rent EC2 Micro instance on AWS for free (Free Tier) for one year, which gives you plenty possibilities and time to try and test different stuff. You'll get semi-dedicated system, where you can do pretty much anything (install software, modify OS configurations, and much more), where shared hosting would not allow you to do so.
Look into Heroku. For simple low traffic apps, they are free and can easily be scaled for more traffic (for an added cost). Additionally, you use Git to deploy, so is really simple to get stuff updated...
There are other ways to deploy node.js apps.
You can use PaaS services, like Openshift, Heroku, AppFog, Paastor, dotCloud etc.
Other great node app hosting options include Joyent's SmartOS and Microsoft Azure. Both have a free trial period.
Azure can be a great learning platform for node.js as you can host your node app in Windows Server, Ubuntu Linux, or Azure's special "web site" shared deployment scheme.
http://www.windowsazure.com/en-us/develop/nodejs/tutorials/create-a-website-(mac)/
Another cost efective solution for node app hosting is Azure's "Web Site" approach - about $10 per month. The down side is that you have to use their shared environment that hosts your node app via IIS. In practice, this worked for well for me, but you are limited in that you can't use certain Linux functions from Node when it's running on Windows, and you won't learn how to configure the node service yourself, which may or may not be important to you. (Note: Azure's GIT deployment process works great if you want to deploy your app from a local GIT repository. Also note that NodeIIS will stop your node app when it's not in use for a certain period of time - and it auto-starts again when a request for your app comes in.)
Joyent's SmartOS platform is a Linux OS optimized for hosting your node.js app. They have impressive reliability and performance as well as great diagnostic tools.
http://wiki.joyent.com/wiki/display/jpc2/Developing+a+Node.js+Application
The most cost effective solution I have found so far is DigitalOcean, a great new hosting solution where you can host a full Linux VM for only $5/month! I have had great luck hosting Node apps there so far: https://www.digitalocean.com/pricing
a2hosting allows Node.js in shared hosting.But don't have experience there.Found from a web search
Update : Use DigitalOcean. Private VPS
Node doesn't work like most servers. With IIS and Apache, there is one server running multiple sites, which lends itself to shared environments. With Node, you're running your own server so instead you tend to share resources on a machine.
I can't tell you whether it's worth learning node because I don't know your motivation, but it can expand your career opportunities if you choose to go there, and to expand your skillset.
Here are a couple of hosting options in the low price range.
http://nodester.com/
https://www.nodejitsu.com/

Separate service on NodeJS server

I want to know how to structure my NodeJS server.
I want to separate services proposed on my website to mount cluster in the future and to have many servers (each allowed to one special task).
Example :
The 'main' server which have one project : ExpressJS and Database
The 'communication server' which have one project : Chat + Forum
Others projects : For complex computing (generating chart / stats / emailing)
Could you explain me different approach for this type of complex website ?
Like Benjamin Gruenbaym said, the architecture belongs somewhere else.
If you are wondering about how to setup the applications on an individual server, there are a few things to keep in mind.
NodeJS runs in a single process, so it should ideally take up 1 core of the CPU. If you run a database on the same server, that is another core. So it may be fine to host all node applications on the same server, if it has a sufficient number of cores.
To run two different Node processes on the same machine, you simply start them one after another, but make sure that they listen on different ports.
To make sure that you can scale out your application later, it is important that you use domain names, instead of IP adresses when you identify your services to each other. So the nodeJS app should know about the database as mydatabase.mycompany.com, not as 192.168.1.10 or any other ip address. This will allow you to later move the database to another network address or to use a load balancer.

Resources