AlgoViz

Microservices or monolith

easy 28 min

A monolith is one program, built and released as a whole.

See it run

One order in both styles: how it runs, how it scales, how it fails, how a change goes live, and how to move from one to the other.

Operations

One order, one program

The monolith on its own: function calls, one database, one transaction.

Step 1 of 4

The request arrives

Our systemorderUsersApplication4 modules insideDatabaseall tables

A shopper places an order. The user, order, payment and stock modules are all in one program.

Step 1: The request arrives

def place(order):    with database.transaction():      # all or nothing        check_user(order.user)        take_stock(order.items)        insert_order(order)    return "confirmed"
Wait for
One commit
You run
1 program
Calls between modules
inside the process

One transaction over several modules is the quiet superpower of a monolith.

In the real world

The same microservices or monolith doing real jobs. Pick one and step through it.

The stock code leaks memory. What do users see in each style?

Step 1 of 3

Monolith: memory grows

Our systemUsersLoad balancerCopy 1all modulesCopy 2all modulesDatabase
Copy 1 memory
88%
Copy 2 memory
91%

Every copy has the leak, because every copy has the stock module.

Step 1: Monolith: memory grows

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • Services that share tables

    critical

    Nobody can change the schema alone, so the services must be released together.

    Wrong
    # in the payment servicedb.query("SELECT * FROM orders WHERE id = ?", id)
    Right
    # in the payment serviceorder_api.get(id)           # or a local copy from events
  • Save, then publish

    critical

    A crash between the two lines leaves an order nobody hears about.

    Wrong
    db.insert(order)broker.publish("order created", order)
    Right
    with db.transaction():    insert(order)    insert_outbox("order created", order)# a relay publishes the outbox
  • A fallback that invents facts

    major

    If stock cannot be asked, answering available sells goods that do not exist.

    Wrong
    except BreakerOpen:    return Stock(available=True)
    Right
    except BreakerOpen:    return Stock(known=False)   # caller decides: wait, or sell later
  • Splitting before knowing the domain

    major

    Wrong boundaries become network contracts, and moving them needs several teams.

    Wrong
    services = [make_service(table) for table in tables]
    Right
    modules = bounded_contexts(business)# extract one when it has a reason

Interview tips

  • How to explain it

    • Monolith: one unit of release, possibly many copies.
    • Microservices: own process, own release, own data.
    • The split trades function calls for network calls.
  • Memory trick: SPLIT

    • Single responsibility, Polyglot, Loose coupling.
    • Independent release.
    • Team ownership.
  • Reach for services when

    • Teams block each other.
    • One part needs different hardware or protection.
    • You can run the platform.
  • Stay together when

    • The team is small.
    • The domain is still moving.
    • One transaction covers what you need.