Skip to content
Go back

Data Source Routing

Terms

When we Choose Data Source Routing

When implementing Repository methods, you want to preserve original behaviour: read methods are slave-preferred, while write methods use master host.

When you want to revert all operations, you use Transaction. Or you are doing read-after-write, when Transaction is not applicable: write some data from request, do some remote calls or I/O operations, then read back some data (think about why this is not a good idea to do some work like this inside a Transaction). In these cases you need all your queries performed on one host, and since you are doing some writes — this is a Master host.

Transaction

Safely force all routing to Master inside a Transaction operation — you do not utilise Transactions when not doing some write work. If you do, just do not.

Outside Transaction

Outside Transaction, you need to force routing with some additional calls.

Why not just Master it

Simple solution is to Master all Slave Preferred operations. This will shift load from your Read Replicas onto Master node and increase I/O and CPU utilisation. You would usually want to avoid it. If you need to backup your Data Source, it is probably a good idea to use other mechanisms.

Who Controls Routes

When performing operation in response to input (HTTP request, CLI call), you as developer already know that there will be a Write. When doing a Write inside operation, route all Data Source choices to Master.

Two ways to do it when implementing a Repository is to:

  1. Add WithMaster method on repository
  2. Add Context-Based routing to Data Source wrapper

WithMaster

Pros: explicit routing; fine-grained control Cons: boilerplate

type Repository struct {
    db db.Router
}
    
func (r *Repository) WithMaster() *Repository {
    r.db.SlavePreferred = r.db.Master
    return r
}
    
func (r *Repository) WithoutMaster() *Repository {
    r.db.SlavePreferred = r.db.Slave
    return r
}
    
func (s *Service) Operation(ctx context.Context, req *http.Request) {
    s.repository = s.repository.WithMaster()
    payload = fromRequest(req)
    
    written := s.repository.Write(ctx, payload)
    s.client.SendBeacon(written)
    
    read := s.otherRepository.Read(ctx, written)
    
    // ...
    return read
}

func (s *Service) OtherOperation(ctx context.Context, req *http.Request) {
    s.repository = s.repository.WithMaster()
    payload = fromRequest(req)
    
    written := s.repository.Write(ctx, payload)
    s.client.SendBeacon(written)
    
    read := s.otherRepositoryepository.Read(ctx, written)
    
    // ...
    return read
}

####Context-Based Routing Pros: less boilerplate Cons: might be hard to test; reverting Master force must be done at top-level

type forceMasterKey struct {}

func ForceMasterContext(ctx) context.Context {
    return context.WithValue(ctx, forceMasterKey{}, true)
}

func RevertForceMasterContext(ctx) context.Context {
    return context.WithValue(ctx, forceMasterKey{}, nil)
}

func (s *Service) Operation(ctx context.Context, req *http.Request) {
    ctx = ForceMasterContext(ctx)
    payload = fromRequest(req)
    
    written := s.repository.Write(ctx, payload)
    s.client.SendBeacon(written)
    
    read := s.repository.Read(ctx, written)
    
    // ...
    return read
}

func (s *Service) OtherOperation(ctx context.Context, req *http.Request) {
    ctx = ForceMasterContext(ctx)
    payload = fromRequest(req)
    
    written := s.repository.Write(ctx, payload)
    s.client.SendBeacon(written)
    
    ctx = RevertForceMasterContext(ctx)
    read := s.otherRepository.Read(ctx, written)
    ctx = ForceMasterContext(ctx)
    
    // ...
    return read
}

Share this post on:

Next article
How to not Lose Money On Feature Flags