DEV Community

cycy
cycy

Posted on

Understanding gRPC for a Restaurant App - Super Simple Explanation

Setting the Scene: Our Restaurant Management System

Imagine we're building a complete restaurant management system with different parts that need to talk to each other:

  • A Menu Service that manages all the food items, prices, and availability
  • An Order Service that handles customer orders
  • A Business Service that manages restaurant information like hours and locations
  • A Delivery Service that coordinates food delivery

Each service is like a different department in a restaurant, and they all need to work together seamlessly. This is what's called a "microservices architecture" - breaking a big system into smaller, specialized pieces.

The big question is: How do these services communicate with each other? That's where gRPC comes in!

Project Structure πŸ“

To understand how everything fits together, let's take a look at our project structure:

menu-swift-menu-service/
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ grpc/
β”‚   β”‚   β”œβ”€β”€ protos/             # Protocol definitions
β”‚   β”‚   β”‚   └── menu.proto      # Our contract file
β”‚   β”‚   β”œβ”€β”€ generated/          # Auto-generated code
β”‚   β”‚   β”‚   β”œβ”€β”€ menu_pb2.py
β”‚   β”‚   β”‚   └── menu_pb2_grpc.py
β”‚   β”‚   β”œβ”€β”€ services/           # Service implementations
β”‚   β”‚   β”‚   └── menu_service.py
β”‚   β”‚   └── server.py           # gRPC server class
β”‚   β”œβ”€β”€ models/                 # Database models
β”‚   β”‚   └── menu.py
β”‚   β”œβ”€β”€ scripts/                # Entry point scripts
β”‚   β”‚   β”œβ”€β”€ grpc_client.py      # Test client
β”‚   β”‚   └── grpc_server.py      # Standalone server
β”‚   β”œβ”€β”€ services/               # Business logic services
β”‚   β”‚   └── image_service.py
β”‚   β”œβ”€β”€ tests/                  # Test utilities
β”‚   β”‚   └── grpc/
β”‚   β”‚       └── create_test_menu.py
β”‚   β”œβ”€β”€ grpc_server.py          # FastAPI + gRPC integration
β”‚   └── rest_server.py          # REST API server
└── requirements.txt
Enter fullscreen mode Exit fullscreen mode

This structure follows best practices for organizing a Python gRPC project, with clear separation of concerns and proper file organization.

1. menu.proto - The Rule Book πŸ“œ

This is like writing down all the rules for a game before you start playing.

message Menu {
    string id = 1;
    string name = 2;
    string description = 3;
    double price = 4;
    // ...
}

service MenuService {
    // GetMenu retrieves a specific menu item by its ID
    rpc GetMenu(MenuRequest) returns (Menu);

    // ListMenus retrieves all available menu items
    rpc ListMenus(google.protobuf.Empty) returns (MenuList);
    // ...
}
Enter fullscreen mode Exit fullscreen mode

What it does:

  • Defines what a "menu item" looks like (it has an id, name, price, etc.)
  • Lists what actions we can do (get one menu, list all menus, etc.)

Think of it as: The blueprint for your LEGO set. It shows all the pieces and how they fit together.

2. menu_pb2.py and menu_pb2_grpc.py - The Auto-Generated Code πŸ€–

These files are made by a computer program that reads the rule book.

class MenuServiceStub(object):
    """MenuService provides operations for managing restaurant menu items"""

    def __init__(self, channel):
        self.GetMenu = channel.unary_unary(...)
        self.ListMenus = channel.unary_unary(...)
Enter fullscreen mode Exit fullscreen mode

What they do:

  • Turn the rules into actual code that computers understand
  • Create helper tools for sending and receiving messages

Think of them as: Translators that help your programs talk to each other in the same language.

3. menu_service.py - The Restaurant Kitchen πŸ‘¨β€πŸ³

This is where all the real work happens.

def ListMenus(self, request, context):
    try:
        result = self.crud.read(model_name=Menu)
        menus = result["items"]
        return menu_pb2.MenuList(items=[self._to_proto_menu(menu) for menu in menus])
    except HTTPException as e:
        context.set_details(f"{e.status_code}: {e.detail}")
        return menu_pb2.MenuList()
Enter fullscreen mode Exit fullscreen mode

What it does:

  • Gets menu items from the database when asked
  • Creates new menu items when told to
  • Updates or deletes menu items

Think of it as: The chef who actually cooks the food when someone orders it.

4. grpc_server.py - The Restaurant Building 🏒

This file starts up the restaurant and gets it ready for customers.

def serve():
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    session = Session(engine)

    menu_pb2_grpc.add_MenuServiceServicer_to_server(
        MenuServiceServicer(session), server)

    server.add_insecure_port('[::]:50051')
    server.start()
    print("gRPC Server started on port 50051")
Enter fullscreen mode Exit fullscreen mode

What it does:

  • Opens the restaurant doors (starts a server)
  • Tells the chef to get ready (connects to the menu service)
  • Waits for customers (listens on port 50051)

Think of it as: Opening the restaurant for business each morning.

5. grpc_client.py - The Test Customer πŸ§‘β€πŸ¦±

This file acts like a customer walking into the restaurant to test if everything works.

def run():
    channel = grpc.insecure_channel("localhost:50051")
    stub = menu_pb2_grpc.MenuServiceStub(channel)
    response = stub.ListMenus(Empty())
    print("Menus received:")
Enter fullscreen mode Exit fullscreen mode

What it does:

  • Connects to the restaurant (server)
  • Asks for the menu (calls ListMenus)
  • Prints out what it gets back

Think of it as: A food critic visiting to check if the restaurant is good.

How It All Works Together πŸ”„

  1. First, you write down the rules (menu.proto)
  2. Then you use a special tool to create helper code:
   python -m grpc_tools.protoc -I./app/grpc/protos --python_out=./app/grpc/generated --grpc_python_out=./app/grpc/generated ./app/grpc/protos/menu.proto
Enter fullscreen mode Exit fullscreen mode
  1. You write the actual kitchen code (menu_service.py)
  2. You open the restaurant (grpc_server.py)
  3. A test customer visits (grpc_client.py)

Real-World Example: Taking an Order

Here's what happens when a customer places an order in our restaurant app:

  1. Customer taps "Order" for a Spicy Chicken Burger on their phone
  2. Order Service needs to check if this menu item is available
  3. Order Service calls Menu Service using gRPC:
Order Service: "Hey Menu Service, is the Spicy Chicken Burger available?"
Menu Service: "Yes, we have 15 portions left!"
Order Service: "Great, I'll place an order for 2 burgers."
Enter fullscreen mode Exit fullscreen mode
  1. Menu Service updates the database: Reduces available portions from 15 to 13
  2. Order Service creates the order: Sends it to the kitchen for preparation

Why Is This Important for Our Restaurant App? πŸ”

In our complete restaurant management system:

  • The Menu Service knows all about food items
  • The Order Service needs to know what food is available
  • The Business Service manages restaurant info
  • The Delivery Service needs to know order details

Using gRPC, they can all talk to each other super fast and without confusion, which means:

  • Customers always see accurate menu information
  • The kitchen never gets orders for items that are sold out
  • Delivery drivers get precise order details

It's like having perfect communication between all staff in a busy restaurant - that's the power of gRPC in our microservices system!

Running Our Restaurant for Real

To see this in action, we:

Create some test menu items:

   python -m app.tests.grpc.create_test_menu
Enter fullscreen mode Exit fullscreen mode

Start our Menu Service:

   python -m app.scripts.grpc_server
Enter fullscreen mode Exit fullscreen mode

Test it with a client:

   python -m app.scripts.grpc_client
Enter fullscreen mode Exit fullscreen mode

And just like that, we have a high-performance communication system that helps our entire restaurant management system work together smoothly!

For larger systems, we can also integrate gRPC with our REST API:

python -m app.grpc_server
Enter fullscreen mode Exit fullscreen mode

This starts both the REST API and gRPC server together, giving us the best of both worlds!

Top comments (0)