[ํ’€์Šคํƒ] Zero message queue (ZMQ)

2023. 4. 17. 23:34ใ†ComputerScience/FullStackProgramming

 

 

 

ZeroMQ Patterns

  • Request-reply, which connects a set of clients to a set of services.
    This is a remote procedure call and task distribution pattern (REQ, REP, DEALER, ROUTER).
    • ์š”์ฒญํ•˜๋ฉด ์‘๋‹ต์„ ๋ณด๋‚ด์ค€๋‹ค. 
    • ๋‚ด๊ฐ€ ์š”์ฒญํ•˜๋ฉด ๊ฐ€์šด๋ฐ ์• ๊ฐ€ ๋ฐ›์•„์„œ,
      ์ผ๊พผ์ด 3๋ช… ์žˆ๋Š”๋ฐ ์–ด๋–ค ์• ํ•œํ…Œ ๋ณด๋‚ผ๊นŒ?
      ๋‹ˆ๊ฐ€ ๊ฐ€์ ธ๊ฐ€๋ผ ๋“ฑ๋“ฑ ์ ์ ˆํžˆ ๋ถ„๋ฐฐํ•˜๋Š” ๊ณผ์ •๋„ ์ด๋ฏธ ๊ตฌํ˜„๋จ
      ์ด ์š”์ฒญ์— ๋Œ€ํ•ด์„œ๋Š” ๋‹ˆ๊ฐ€ ์ฒ˜๋ฆฌํ–ˆ์Œ ์ข‹๊ฒ ๋‹ค -> ์„ค์ •ํ•˜๋Š” ๊ฒƒ๋„ ๊ตฌํ˜„๋จ
  •  Pub-sub, which connects a set of publishers to a set of subscribers.
    This is a data distribution pattern (PUB, SUB, XPUB, XSUB).
    • ๊ฐ€์ž…ํ•œ ์• ๋“คํ•œํ…Œ ์ •๋ณด๋ฅผ ๋ฟŒ๋ฆผ
  • Pipeline, which connects nodes in a fan-out/fan-in pattern that can have multiple steps and loops.
    This is a parallel task distribution and collection pattern (PULL, PUSH).
    • ๋Œ€๊ทœ๋ชจ์˜ ๋ณ‘๋ ฌ์ฒ˜๋ฆฌ๋ฅผ ๋™์‹œ๋‹ค๋ฐœ์ ์œผ๋กœ ํ•˜๋Š” ๊ฒƒ -> OS's ํŒŒ์ดํ”„๋ผ์ธ
  • Exclusive pair, which connects two sockets exclusively.
    This is a pattern for connecting two threads in a process, not to be confused with “normal” pairs of sockets.

 

 

 

ZeroMQ Sockets

- ๊ธฐ์กด์˜ ์‚ฌ๊ณ  ๋ฐฉ์‹ - 1:1 ์—ฐ๊ฒฐ์„ ๋งŒ๋“ค ๋•Œ, TCP or UDP -> ์ด์™ธ์˜ ํŒ๋‹จ์€ X
- 1:N, N:M ๊ตฌํ˜„์€ ๊ฐœ๋ฐœ์ž์˜ ๋ชซ
- ์†Œ์ผ“์ด ํ•ด์ฃผ๋Š” ๊ฒƒ์€ ์—†๋‹ค 

- ZMQ๋Š” ์ด๋ฏธ ๋‹ค ๊ตฌํ˜„๋˜์–ด ์žˆ์Œ

 

  • Conventional sockets -> ์ „ํ†ต์ ์ธ ๋ฐฉ์‹์˜ ์†Œ์ผ“
    • Synchronous interface to either connection-oriented reliable byte streams (SOCK_STREAM), or connection- less unreliable datagrams (SOCK_DGRAM)
    • Transfer streams of bytes or discrete datagrams Allow 1:1 (two peers), N:1 (many clients, one server),
    • or in some cases 1:N (multicast) relationships
  • ZeroMQ sockets -> ์ œ๋กœ ๋ฉ”์„ธ์ง€ ํ์˜ ์†Œ์ผ“ 
    • Abstraction of an asynchronous message queue, with the exact queueing semantics depending on the socket type in use
    • Transfer discrete messages
    • Allow many-to-many relationships

 

  •  Asynchronous ZeroMQ sockets
    • Timings of the physical connection setup and tear down, reconnect and effective delivery are transparent to the user and organized by ZeroMQ
    • Further, messages may be queued in the event that a peer is unavailable to receive them
  • ์ œ๋กœ ๋ฉ”์„ธ์ง€ ํ
    ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ž…์žฅ์—์„œ ์ปค๋„ฅ์…˜์ด ๋ฐ‘์—์„œ ์‹ค์ œ๋กœ 
    ์—ด๋ ค์žˆ๋“  ๋ง๋“ , ๋ฉ”์„ธ์ง€ ์ฃผ๊ณ  ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.
    tcp ์—ฐ๊ฒฐ ์„ค์ • -> ๋Š์–ด์ง€๋ฉด ์ •๋ณด๋ฅผ ์ฃผ๊ณ  ๋ฐ›์„ ์ˆ˜ ์—†๋‹ค

    ์—ฐ๊ฒฐ ์š”์ฒญ์ด ์•„์ง ์•ˆ ๋˜์–ด๋„
    application ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๋‚ผ ์ˆ˜ ์žˆ๋‹ค
    ์–ธ์  ๊ฐ€ ์—ฐ๊ฒฐ ์š”์ฒญ์ด ์ด๋ฃจ์–ด์งˆ ๊ฒƒ์ด๋ผ๋Š” ์ „์ œ ํ•˜์—,
    ๋ฉ”์„ธ์ง€ ํ์— ์Œ“์•„๋†“๊ณ , ์–ธ์  ๊ฐ€๋Š” ๋ณด๋‚ธ๋‹ค

    ์—ฐ๊ฒฐ์ด ๋œ ์ƒํƒœ์—์„œ ์ด๋™ํ•˜๊ฑฐ๋‚˜, ๋„คํŠธ์›Œํฌ๊ฐ€ ๋ถˆ์•ˆํ•˜์—ฌ ์—ฐ๊ฒฐ์ด ๋Š์–ด์ง
    -> ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์˜ํ–ฅ์„ ๋ฐ›์ง€ ์•Š๊ณ , ์ •๋ณด๋ฅผ ๋ณด๋‚ผ ์ˆ˜ ์žˆ๋‹ค.
    zmq๊ฐ€ ์—ฐ๊ฒฐ ์žฌ์‹œ๋„ ํ•จ + app์ด ๋ณด๋‚ด๋Š” ์ •๋ณด๋ฅผ ๊ณ„์† ์Œ“์•„๋‘ 

    ์—ฐ๊ฒฐ์ด ๋Š์–ด์กŒ๋Š”์ง€ , ์•ˆ๋Š์–ด์กŒ๋Š”์ง€ ์ƒ๊ด€ ์—†์ด ์‚ฌ์šฉ ๊ฐ€๋Šฅ
    ๋ฌด์„ ์„ ์‚ฌ์šฉํ•˜๋Š” ํ™˜๊ฒฝ์—์„œ zmq ์žฅ์  !

 

  •  ZeroMQ socket lifetime (similar to BSD sockets)
    • Creating and destroying sockets, which go together to form a karmic circle of socket life
    • Configuring sockets by setting options on them and checking them if necessary
    • Plugging sockets into the network topology by creating ZeroMQ connections to and from them.
    • Using the sockets to carry data by writing and receiving messages on them.

 

 

 

ZeroMQ Sockets bind connect

It doesn’t matter who binds and who connects.
In general, the server used Bind while the client used Connect
ZeroMQ creates queues per underlying connection.
If your socket is connected to three peer sockets, then there are three messages queues behind the scenes.

With Bind, you allow peers to connect to you, (์„œ๋ฒ„์˜ ์—ญํ• ์„ ํ•˜๋Š” ๊ฒƒ)
thus you don’t know how many peers there will be in the future and you cannot create the queues in advance.
Instead, queues are created as individual peers connect to the bound socket.

 

With Connect, in general cases, ZeroMQ knows that there’s going to be at least a single peer and thus it can create a single queue immediately.

 

  • bind - server / connect - client
  • When should I use bind:
    • As a general rule use bind from the most stable points in your architecture,
      and use connect from dynamic components with volatile endpoints
    • For request/reply, the service provider might be the point where you bind and the clients are using connect.
      Just like plain old TCP
    • ์›น ์„œ๋ฒ„๋Š” 24์‹œ๊ฐ„ ์‚ด์•„์žˆ์Œ -> stable -> ์ด๋Ÿฐ ์• ๋ฅผ ๋ฐ”์ธ๋“œ ์‹œํ‚จ๋‹ค. 
      ์›น ๋ธŒ๋ผ์šฐ์ € -> (์›น์„œ๋ฒ„)ํ•œํ…Œ connect ํ•˜๋ฉด ๋˜๊ฒ ๋„ค 
  • When should I use connect:
    • If you can’t figure out which parts are more stable (i.e. peer-to-peer),
      consider a stable device in the middle, which all sides can connect to
    • ๊ธฐ์กด tcp/ip์—๋Š” ๋…ธ๋“œ์™€ ๋…ธ๋“œ๋ฅผ tcp ์†Œ์ผ“ ์ค„ ํ•˜๋‚˜๋กœ ์—ฐ๊ฒฐ
      ์–˜๋Š” ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ž…์žฅ์—์„œ ๋‹ค์–‘ํ•œ ์ค„์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๊ณ , 
      ํ•˜๋‚˜๋Š” ๋ฐ”์ธ๋“œ, ํ•˜๋‚˜๋Š” ์ปค๋„ฅํŠธ ์ด๋Ÿฐ ์‹์œผ๋กœ ์จ๋„ ๋œ๋‹ค. 

 

 

 

RabbitMQ (open source message broker)

ZMQ ๋ˆ„๊ตฌ๋Š” ๋ฐ˜๋“œ์‹œ ์„œ๋ฒ„๋‹ค ์ด๋Ÿฐ ๋ฃฐ์ด ์—†๋‹ค
๋ถ„์‚ฐ์ ์œผ๋กœ ๋…ธ๋“œ๊ฐ€ ๋Œ€๋“ฑํ•œ ๊ด€๊ณ„์—์„œ ์ •๋ณด๋ฅผ ์ฃผ๊ณ  ๋ฐ›๋Š” ๊ตฌ์กฐ 

RabbitMQ -> message broker ๋ธŒ๋กœ์ปค๊ฐ€ ์žˆ๋Š” ๊ตฌ์กฐ

 

์ค‘์•™ ์ง‘์ค‘ํ˜• Broker ๊ธฐ๋ฐ˜ ๋ฉ”์„ธ์ง• ๊ฐœ๋…

  • ์ •๋ณด๊ฐ€ ํ์— ์ฐจ๊ณก์ฐจ๊ณก ์Œ“์ด๊ณ , ์–˜๊ฐ€ ๋ฉ”์„ธ์ง€๋ฅผ ๋ฐ€์–ด๋ƒ„
  • ์‚ฌ๋žŒ๋“ค์ด ๋ชฐ๋ฆฌ๋ฉด - ์ฒœ์ฒœํžˆ
    ๋‚ด ์š”์ฒญ๋งŒ ์žˆ์œผ๋ฉด - ๋ฐ”๋กœ ์‹ ์†ํžˆ
  • subscribe๋กœ ๋ณด๋‚ด๊ธฐ๋„ ํ•˜๊ณ , ๋™์‹œ ๋‹ค๋ฐœ์ ์œผ๋กœ (fan out) ์ซ™ ๋ฟŒ๋ฆผ
  • ex) ๋‚ ์”จ ์œ„์ ฏ - ํ”ผ๋“œ๋ฐฑ ์—†์ด ์ผ๋ฐ˜์ ์ธ ์ „์†ก์„ ํ•  ๋•Œ or ์•Œ๋žŒ์„ ๋ณด๋‚ด๊ฑฐ๋‚˜ ๋“ฑ๋“ฑ