> For the complete documentation index, see [llms.txt](https://synaptiq-systems-1.gitbook.io/synaptiq-systems/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://synaptiq-systems-1.gitbook.io/synaptiq-systems/~/changes/GV7eJCpt5kF6deUPqR2y/basics/interactive-blocks.md).

# SwarmConsensus: Collaborative Decision-Making in SynaptiQ Systems

### **SwarmConsensus: Collaborative Decision-Making in SynaptiQ Systems**

**SwarmConsensus** is a core feature of **SynaptiQ Systems** that enables collaborative decision-making among agents. It supports both small-scale and large-scale swarms with the following methods:

* **Basic Redis Methods**: Ideal for small swarms with straightforward task delegation.
* **Advanced Lua and Transactional Methods**: Designed for larger, more complex swarms requiring atomicity and performance optimization.

***

#### **Basic Methods for Small Swarms**

**1. Propose a Task**

Agents can propose tasks using a simple Redis-backed counter.

```python
pythonCopy codeproposal_id = swarm.propose_task("Analyze data trends")
```

**2. Vote for a Task**

Each agent can vote on proposals using Redis hash operations.

```python
pythonCopy codeswarm.vote(proposal_id)
```

**3. Retrieve Consensus**

Consensus is reached when a task receives a threshold number of votes.

```python
pythonCopy codeconsensus = swarm.get_consensus()
print(consensus)
```

***

#### **Advanced Methods for Larger Swarms**

For larger swarms or high-concurrency scenarios, **SwarmConsensus** provides enhanced functionality using Lua scripts and Redis transactions:

**1. Propose a Task with Lua**

Lua ensures atomic task proposal operations, minimizing race conditions.

```python
pythonCopy codeproposal_id = swarm.propose_task_with_lua("Optimize blockchain transaction flow")
```

**2. Vote with Redis Transactions**

Transactions ensure that voting operations are executed atomically, even under high concurrency.

```python
pythonCopy codeswarm.vote_with_transaction(proposal_id)
```

**3. Consensus via Lua**

For large datasets or frequent voting, Lua scripts streamline consensus checks.

```python
pythonCopy codeconsensus = swarm.get_consensus_with_lua(threshold=5)
print(consensus)
```

***

#### **When to Use Each Method**

| **Method**             | **Use Case**                                     |
| ---------------------- | ------------------------------------------------ |
| **Basic Methods**      | Small swarms with simple task delegation.        |
| **Lua Scripts**        | Atomic task proposals and high-frequency voting. |
| **Redis Transactions** | High-concurrency voting in large swarms.         |

***

#### **Example Workflow**

Here’s a step-by-step workflow for using the **SwarmConsensus** module:

**1. Propose a Task**

```python
pythonCopy codeproposal_id = swarm.propose_task_with_lua("Conduct AI-powered simulations")
```

**2. Vote on a Task**

```python
pythonCopy codeswarm.vote_with_transaction(proposal_id)
```

**3. Check Consensus**

```python
pythonCopy codeconsensus = swarm.get_consensus_with_lua(threshold=3)
if consensus:
    print("Consensus reached:", consensus)
else:
    print("No consensus reached yet.")
```

***

#### **Key Notes**

* Use basic methods for lightweight operations and testing.
* For production environments or large-scale swarms:
  * Leverage Lua scripts for atomicity and optimized performance.
  * Use Redis transactions to ensure data consistency under high concurrency.
* Configure the consensus threshold based on your swarm size and requirements.
