message queues

26
Message Queues COMP3017 Advanced Databases Dr Nicholas Gibbins – [email protected] 2012-2013

Upload: awena

Post on 26-Feb-2016

33 views

Category:

Documents


0 download

DESCRIPTION

Message Queues. COMP3017 Advanced Databases. Dr Nicholas Gibbins – [email protected] 2012-2013. Transactions Revisited. Complete units of work – ACID Transactions may be distributed across more than one processor Dedicated link between programs Operations synchronized - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Message Queues

Message QueuesCOMP3017 Advanced Databases

Dr Nicholas Gibbins – [email protected]

Page 2: Message Queues

2

Transactions RevisitedComplete units of work – ACID

Transactions may be distributed across more than one processor

–Dedicated link between programs–Operations synchronized–Distributed two phase commit

Many transactions need not be completed synchronously

–Communication must still be guaranteed–Work can be completed later

Page 3: Message Queues

3

Synchronous versus AsynchronousHTTP is a synchronous protocol

–Request from client to server is followed by reply from server to client in the same TCP connection

SMTP is an asynchronous protocol–Email messages are sent on a store-and-forward basis–Final message recipient need not be available when

message is sent

Page 4: Message Queues

4

Messaging and Queuing (MQ)MQ enables programs to communicate across a network asynchronously

–No private, dedicated link required–Systems can be heterogeneous–Message delivery is guaranteed

Messages are placed on queues by one system and taken off by another

–Similar idea to email, but more sophisticated mechanism

MQ can also be used between programs running on the same system

Page 5: Message Queues

5

Direct Transaction ProcessingConventional (direct) transaction processing has weaknesses:

–Designed for synchronous processing–Has difficulties with long-lived transactions and

communication errors–Difficult to balance loads between several servers carrying

out the same tasks–Difficult to prioritise one request over another

Page 6: Message Queues

6

If server is down, client does not receive an immediate answer

Cannot distinguish between:– request not delivered to server– server failure– reply not delivered to client

Direct TP Problems: Server Failure

Client Server

Page 7: Message Queues

7

Cannot tell if response has been received by client

Direct TP Problems: Client Failure

Client Server

Page 8: Message Queues

8

Cannot ensure that servers carrying out a task have an even load

Direct TP Problems: Unbalanced Load

Server 1 Server 2 Server 3

requests

Page 9: Message Queues

9

Tasks handled on a first-come, first-served basis; cannot process high-priority requests early

Direct TP Problems: No Prioritisation

Server

!requests

urgent request

Page 10: Message Queues

10

Client adds request to queue (enqueues)

Server removes request from queue (dequeues)

If server is unavailable (busy, down, disconnected), the request is stored in the queue until the server is able to process it

Message Queues

Client Servermessage queue

enqueuerequest

dequeuerequest

Page 11: Message Queues

11

Bidirectional communication between client and server requires a separate queue for communications in each direction

Bidirectional Queues

Client Server

request queueenqueuerequest

dequeuerequest

reply queue

enqueuereplydequeue

reply

Page 12: Message Queues

12

Communication may be one-to-many

Communication may be many-to-one

Application Structure

Client

Server

message queueServer

Server

Clientmessage queue

Server

Client

Client

Page 13: Message Queues

13

When a server finishes processing a request, it takes the next from the queue

Load Balancing

Client Server

message queueenqueue dequeue

Client Server

Page 14: Message Queues

14

Queued Transaction Processing

transactionstart

dequeue(request queue)

process requestenqueue(reply

queue)commit

requestqueue

replyqueue

Page 15: Message Queues

15

Queued Transaction ProcessingIf transaction aborts:

– request returned to input queue– changes made by transaction are rolled back– if necessary, reply removed from output queue

Repeated aborts (due to a poisoned message) may be prevented with a maximum limit on retries

Page 16: Message Queues

16

Queued Transaction Processing

transaction 1: submit requeststart

construct requestenqueue(request

queue)commit

transaction 3: process replystart

dequeue(reply queue)

process outputcommit

transaction 2: execute requeststart

dequeue(request queue)

process requestenqueue(reply

queue)commit

client

server

requestqueue

reply queue

Page 17: Message Queues

17

Message OrderingDescription of message queues so far does not consider how messages are ordered in a queue

–First-come, first-served–Highest-priority-first

Aborted transactions may lead to out-of-order processing:

– T1 dequeues M1– T2 dequeues M2– T2 commits– T1 aborts, returns M1 to queue

Page 18: Message Queues

18

Benefits• Simple APIs hide communications complexity

• Fewer constraints on inter-program operation– Programs communicate indirectly – asynchronous–Client and server do not need to be running at the same

time

• Fewer network sessions needed, and programs are less vulnerable to network failures

• Business change easier to handle

• Assured message delivery

• Asynchronous transaction processing

Page 19: Message Queues

19

Implementations and StandardsMessage Queuing is not a new idea!

–Used for, example, within IBM's Information Management System (IMS) 30 years ago

Both proprietary and open source APIs and platforms– IBM Websphere MQ, Microsoft Message Queuing, Oracle

Advanced Queuing–Apache ActiveMQ, Rabbit MQ

Some products inter-operate via common standards–AMQP, MQTT

Page 20: Message Queues

20

Example 1: Client-Server ApplicationsInsurance agents throughout the country request insurance quotations using an online system.

This system is implemented as a traditional client-server application, with client programs (the insurance agents) sending requests for quotations to a central server program.

The server does some calculations using data from a central insurance database, then sends a quotation to the requesting agent.

Page 21: Message Queues

21

Client programs put request messages on a single queue, from which the server program takes them.

Responses may be sent back to clients via extra message queues (one per client)

Example 1: Client-Server Applications

Insurance Agent

message queue

InsuranceQuotation

InsuranceAgent

InsuranceAgent

insurancedata

Page 22: Message Queues

22

Example 2: Output-only DevicesA device providing a service is output-only, and does not send messages back to the requester. Only one-way message flows are required.

Output-only devices:– Printing devices–Displays:- Stock exchange, Flight arrivals and departures–Factory floor robotics

Page 23: Message Queues

23

Robotics Controller is in control of an automated manufacturing process. It puts messages on:

–Queue 1 for the Robotics A program, which directs some welding machinery, and on

–Queue 2 for the Robotics B program, which controls a paint sprayer.

Example 2: Output-only Devices

RoboticsController

RoboticsA

Robotics B

queue 1

queue 2

Page 24: Message Queues

24

Example 3: The Batch WindowA department store writes its sales figures to a file throughout the day’s trading. Overnight, a report of the day’s sales is produced using this file of data as input. The report must be on the Sales Manager’s desk before the next day’s trading begins.

– The amount of time available for producing the report is limited to the “window” of time between the end of business on one day and the start of business on the next

– The start and finish times for the activity are fixed

Page 25: Message Queues

25

Instead of operating in sequence and communicating via a file, the two programs could run independently of each other and communicate using a message queue

Example 3: The Batch Window

Sales Recording

SalesReporting

message queue

Page 26: Message Queues

26

SummaryMessage queuing allows an alternative way to distribute applications

Many applications involve databases, so the technologies intersect

Conceptually and practically, MQ is often a more straightforward approach than direct connections between systems, and synchronous transaction processing