Menu
Theme
Bachelor of Science in Computer Science
Course Content

Microservices

Distributed Systems

Habari! Cracking the Code on Microservices

Welcome, future tech leader! In your journey through Distributed Systems, we've talked about how to make computers work together. Now, we're going to look at one of the most powerful and modern ways to build software that powers the world, from Jumia to Netflix to M-PESA.

Ever been stuck in Nairobi traffic in one massive bus? If that bus breaks down, everyone is late. Now, imagine if instead of one bus, you had a fleet of agile Boda Bodas. If one gets a puncture, the rest of the fleet continues, and your passengers still get to their destination. That, in a nutshell, is the core idea behind Microservices! We are breaking down the big, slow bus into a team of fast, independent Boda Bodas. Let's ride!

First, Let's Understand the "Big Bus": The Monolith

Before microservices, the most common way to build an application was as a single, large unit called a Monolith. Think of an entire application like a single program. The user interface, business logic, and data access layer are all combined into one big, inseparable block.

Imagine an e-commerce website like Jumia built as a monolith. The code for user accounts, product catalogues, the shopping cart, and payments are all in one giant codebase.


+-----------------------------------------------------+
|                  JUMIA (Monolith App)               |
| +-------------------------------------------------+ |
| |                  User Interface (Web/App)       | |
| +-------------------------------------------------+ |
| |                                                 | |
| |    +-----------------+   +-------------------+  | |
| |    |  User Service   |   |  Product Service  |  | |
| |    +-----------------+   +-------------------+  | |
| |                                                 | |
| |    +-----------------+   +-------------------+  | |
| |    |  Cart Service   |   |  Payment Service  |  | |
| |    +-----------------+   +-------------------+  | |
| |                                                 | |
| +-------------------------------------------------+ |
| |                  Single Large Database          | |
| +-------------------------------------------------+ |
+-----------------------------------------------------+
The Monolith Problem: It's Black Friday on Jumia! The payment processing part of the app is under massive load. To handle the traffic, you must deploy more copies of the ENTIRE application. You are forced to scale the user service and product catalogue, even though they are not the bottleneck. It's like sending the whole bus to pick up one extra person – very inefficient! Worse, if a small bug in the product catalogue code crashes, it could bring down the entire website, including payments.

Enter the "Boda Boda Fleet": The Microservice Architecture

A microservice architecture breaks down a large application into a collection of smaller, independent services. Each service is self-contained, runs its own process, and communicates with other services, usually through a lightweight mechanism like an API (Application Programming Interface).

Each service is built around a specific business capability. For our Jumia example, we would have:

  • A User Service to handle sign-ups and logins.
  • A Product Service to manage the catalogue.
  • A Cart Service to manage shopping carts.
  • A Payment Service to process M-PESA, card payments, etc.

      +----------------+      +----------------+
      |  User Service  | <--> | Product Service|
      | (Own Database) |      | (Own Database) |
      +----------------+      +----------------+
              ^                       ^
              |        API Calls      |
              v                       v
      +----------------+      +----------------+
      |  Cart Service  | <--> | Payment Service|
      | (Own Database) |      | (Own Database) |
      +----------------+      +----------------+

Image Suggestion: A vibrant, bustling digital marketplace in Kenya. In the center is a customer on their phone. Arrows point from the phone to different, colorful market stalls. One stall is labeled "User Accounts" with a person icon. Another is labeled "Product Catalogue" with images of items. A third, very busy stall is labeled "M-PESA Payments" with the M-PESA logo, showing several servers behind it to indicate high traffic. The style should be modern, tech-focused, but with a distinct Kenyan feel.

Key Characteristics of Microservices

  • Decentralized & Independent: Each service can be developed, deployed, and scaled independently. The team working on the Payment Service can release updates without needing to coordinate with the Product Service team. This is like different county governments managing their own projects without waiting for the national government.
  • Single Responsibility: Each service does one thing and does it well. The Payment Service only knows about payments; it doesn't care how the user logs in.
  • Technology Freedom (Polyglot): You can use the best tool for the job! The Product Service might be written in Python for its data science libraries, while the Payment Service might be in Java for its robust, enterprise-grade features. You are not locked into a single technology stack.
  • Resilience (Fault Isolation): If the Product Service goes down (maybe due to a database error), users might not be able to browse new items, but they can still log in, view their cart, and even complete a purchase if the item is already in their cart! The failure of one "boda boda" doesn't stop the whole fleet.

The Math of Scaling: Why Microservices Win

Let's quantify the scaling problem from our Jumia Black Friday example. Assume a monolith server instance costs 100 KES/hour to run and a microservice instance costs 30 KES/hour (it's smaller).

The application has 3 main parts: User (U), Products (P), and Payments (Pay). During the sale, Payment traffic is 5x normal, while U and P are at 2x normal.


// SCENARIO 1: MONOLITH
// The bottleneck is the Payment service, which needs a 5x scale-up.
// Because it's a monolith, you must scale the ENTIRE application 5x.

Total Cost = (Number of Instances) * (Cost per Monolith Instance)
Total Cost = 5 * 100 KES/hour
Total Cost = 500 KES/hour

// SCENARIO 2: MICROSERVICES
// You can scale each service independently based on its actual load.

Cost_User     = (Instances for User) * (Cost per Microservice)
              = 2 * 30 KES/hour = 60 KES/hour

Cost_Products = (Instances for Products) * (Cost per Microservice)
              = 2 * 30 KES/hour = 60 KES/hour
              
Cost_Payments = (Instances for Payments) * (Cost per Microservice)
              = 5 * 30 KES/hour = 150 KES/hour

Total Cost = 60 + 60 + 150
Total Cost = 270 KES/hour

// RESULT:
// Microservices are almost 50% cheaper to scale in this scenario!
// (270 KES vs 500 KES)

A Real-World Kenyan Giant: The M-PESA Ecosystem

Think about the complexity of Safaricom's M-PESA. It's not just one big program. It's an ecosystem of services working together.

  • When you "Send Money", you're likely hitting a Peer-to-Peer Transaction Service.
  • When you "Lipa na M-PESA", you're using a Merchant Payment Service.
  • When you connect your M-PESA to your KCB account, that's an External API Integration Service.
  • When you request a statement, a separate Reporting Service compiles your data.

If the service that connects to banks is slow, it doesn't stop you from sending money to your friend. This separation is what makes the system so robust and allows Safaricom to innovate and add new features (like Fuliza, an overdraft service) without rebuilding the entire M-PESA platform from scratch.

Challenges: It's Not a Free Ride!

While powerful, managing a fleet of Boda Bodas is more complex than managing one bus. You need to track all of them! Microservices introduce their own set of challenges:

  • Operational Complexity: You need to deploy, monitor, and manage dozens or even hundreds of services. This requires strong DevOps skills and tools like Docker and Kubernetes.
  • Network Latency: Services communicate over a network. This is always slower than in-process calls within a monolith. Your design must account for this.
  • Data Consistency: Each service often has its own database. Keeping data consistent across services (e.g., when an order is placed, inventory must be updated) is a significant challenge.
  • Distributed Debugging: If a request fails, it might have passed through 5 different services. Finding the root cause is much harder than debugging a single application stack.

Image Suggestion: An intricate, futuristic digital flowchart resembling a Nairobi roundabout (like the Uthiru interchange) at night with glowing lines of light representing data. Each exit of the roundabout leads to a different building block labeled "Service A," "Service B," etc. The image should convey controlled, complex, but efficient traffic flow.

Conclusion: Are You Ready to Build the Future?

You don't always need microservices. For your final year project, a monolith is probably the right choice! But for large, complex, and scalable applications that need to evolve quickly, the microservice architecture is the industry standard.

By breaking down large problems into smaller, manageable pieces, microservices allow teams to move faster, build more resilient systems, and scale more efficiently. You've just learned the architectural pattern that powers the digital Kenya you use every single day.

Next up, we will explore the tools that make this possible: API Gateways for managing requests, and Docker & Kubernetes for deploying and managing our Boda Boda fleet at scale. Hongera, you're on your way!

Pro Tip

Take your own short notes while going through the topics.

Previous Cloud Computing
KenyaEdu
Add KenyaEdu to Home Screen
For offline access and faster experience