仅用8台虚拟机处理每日数十亿笔交易?PayPal咋做到的?
“PayPal如何仅用8台虚拟机扩展至每日处理数十亿交易?”看到这篇文章的时候,被这一惊人壮举给震撼,现在一起看看具体的实践历程。
虽然他们解决了可扩展性,但是也带来了一些新的问题:
爆炸性增长带来的问题
采用Actor模型
package mainimport ("fmt""sync")// Actor represents an actor with its own state and a channel for receiving messages.type Actor struct {state intmailbox chan int}// NewActor creates a new actor with an initial state.func NewActor(initialState int) *Actor {return &Actor{state: initialState,mailbox: make(chan int),}}// ProcessMessage processes a message by updating the actor's state.func (a *Actor) ProcessMessage(message int) {fmt.Printf("Actor %d processing message: %d\n", a.state, message)a.state += message}// Run simulates the actor's runtime by continuously processing messages from the mailbox.func (a *Actor) Run(wg *sync.WaitGroup) {defer wg.Done()for {message := <-a.mailboxa.ProcessMessage(message)}}// System represents the actor system managing multiple actors.type System struct {actors []*Actor}// NewSystem creates a new actor system with a given number of actors.func NewSystem(numActors int) *System {system := &System{}for i := 1; i <= numActors; i++ {actor := NewActor(i)system.actors = append(system.actors, actor)go actor.Run(nil)}return system}// SendMessage sends a message to a randomly selected actor in the system.func (s *System) SendMessage(message int) {actorIndex := message % len(s.actors)s.actors[actorIndex].mailbox <- message}func main() {// Create an actor system with 3 actors.actorSystem := NewSystem(3)// Send messages to the actors concurrently.var wg sync.WaitGroupfor i := 1; i <= 5; i++ {wg.Add(1)go func(message int) {defer wg.Done()actorSystem.SendMessage(message)}(i)}// Wait for all messages to be processed.wg.Wait()}
Actor 3 processing message: 5Actor 8 processing message: 2Actor 1 processing message: 3Actor 2 processing message: 1Actor 3 processing message: 4
结语