I have built Zero: Fast and high performance Python RPC framework to build microservices
Tags: python, microservices, showdev
Published: 2021-09-28 10:18:18

Zero is actually a RPC like framework that uses ZeroMQ under the hood for communication over tcp. That's why it's fast and super lightweight.

Without going into details let's just get started, cause one of the philosophy behind Zero is **Zero Learning Curve** 😃

## Example

_Ensure Python 3.8+_

- Install Zero
```
pip install zeroapi
```
_Sadly the `zero` package is already there_ 🙁

- Create a `server.py`

```python
from zero import ZeroServer

def echo(msg: str):
    return msg

async def hello_world():
    return "hello world"


if __name__ == "__main__":
    app = ZeroServer(port=5559)
    app.register_rpc(echo)
    app.register_rpc(hello_world)
    app.run()

```

Please note that server **RPC methods' args are type hinted**. Type hint is **must** in Zero server.

_See the method type async or sync, doesn't matter._ 😃

- Run it

```
python -m server
```

- Call the rpc methods

```python
from zero import ZeroClient

zero_client = ZeroClient("localhost", 5559)

def echo():
    resp = zero_client.call("echo", "Hi there!")
    print(resp)

def hello():
    resp = zero_client.call("hello_world", None)
    print(resp)


if __name__ == "__main__":
    echo()
    hello()

```

This is it! Tell me about the learning curve 😉

## A bit of background

After working on large systems (with 50M+ users) over the years, one of the realization is - we waste a good amount of time writing boilerplate code and clients when working on microservices. And also we mostly use HTTP to communicate among them. This is really fine and usual but http has some overheads (headers, separate tcp connection every request etc). In case of microservice communications we can avoid these to get better performance. 
Another thing is usual web frameworks are bulky and has a learning curve. So I was looking for something to reduce these costs and overheads.

Zero is still an idea, though the package is there already 😜 Zero solves the boilerplate code problem by using RPC like structure, you can just focus on the business logic. And use ZeroMQ rather HTTP to solve the overhead and reconnection problem. Though not both of them are solved explicitly, Zero is still a baby to learn more.

## Pros and Cons of Zero over Flask or FastAPI

### Pros

- Inter-service communication is faster as using ZeroMQ and raw TCP under the hood.

- Simple and easy to learn and use.

- Extremely lightweight.

- Super flexible zero can be used only for network communication and you can structure your codebase independently.

### Cons

- Not an HTTP framework, not so much traditional like Flask of FastAPI. People need to understand the actual usage. Like if you have fairly large microservice architecture and there are independent services that communicate among them, zero is a good substitute to reduce network overhead and other framework complexity. But if you only have a few services that communicate directly with the client/frontend, zero is not that much of a use.

- Only Python based framework. Zero server can only be connected with Python zero client for now. (I have plan to introduce Go, in distant future btw)

- You always need another HTTP framework as a gateway if your frontend communicates over HTTP, which is the usual and extremely common way.


## I will be back!

There are more, I will write more about Zero in coming weeks. For now I will be looking towards comments, opinions and suggestions 🙏


GitHub: https://github.com/Ananto30/zero