SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Plugin Development

Reply
 
Thread Tools Display Modes
Old 10/02/2017, 11:28 PM   #1
[HLF]Southclaw
High-roller
 
[HLF]Southclaw's Avatar
 
Join Date: Apr 2009
Location: England
Posts: 4,710
Reputation: 1255
Default Redis (cache, database and message queue)

Redis for SA:MP

A powerful Redis plugin granting access to a fast in-memory database and asynchronous message channels between other programs.

It's open source and available here: https://github.com/Southclaws/samp-redis

The latest release is available here: https://github.com/Southclaws/samp-r...ses/tag/v0.2.0

It only supports Linux and probably only ever will (unless someone gets Hiredis compiling on Windows!)

What's Redis?

Quote:
Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs and geospatial indexes with radius queries.
(from https://redis.io)

In SA:MP terms: Redis can be the GVar plugin on steroids. It can be threaded MySQL. It can be a place to store large amounts of information and free up heap space (and cut compile times!)

You can also use Redis to delegate tasks, receive events and generally communicate with other programs through the power of message queues!

Development

"Redisamp" is currently in development and is in version 0.1.0 since it's not been fully tested in a production/public server environment.

There are tests in the repository however and everything seems to be running fine so feel free to try it out and give feedback.

My roadmap for development is:
  • Array support (arrays of ints, arrays of floats, arrays of strings/arrays!)
  • Optimisation with c-strings instead of c++ strings, for now I stuck with the safer option and decided to do two levels of conversion (amx (c strings) -> implementation (c++ strings) > redis (c strings))
  • More tests! I need to add more failure states and test corner cases to ensure stability.
API

PHP Code:
Redis:Redis_Connect(host[], port); // Connect to a Redis server
Redis_Disconnect(Redis:context); // Disconnect, do this on OnGameModeExit

Redis_Command(Redis:contextcommand[]); // Send a Redis command
Redis_SetString(Redis:contextkey[], value[]); // Set a string key
Redis_GetString(Redis:contextkey[], value[], len); // Retrieve a string
Redis_SetInt(Redis:contextkey[], value); // Set a integer key
Redis_GetInt(Redis:contextkey[], &value); // Retrieve a integer
Redis_SetFloat(Redis:contextkey[], Float:value); // Set a floating point key
Redis_GetFloat(Redis:contextkey[], &Float:value); // Retrieve a floating point

Redis_BindMessage(Redis:contextchannel[], callback[]); // Bind a message channel to a Pawn callback
Redis_SendMessage(Redis:contextchannel[], data[]); // Send a message to a message channel 
Check out the test script for examples of all these functions.

Examples

You might be still wondering why this is even useful. Here are some possible use cases:
  • Database: much like the threaded MySQL plugin but you can use more modern technologies and nicer languages to handle your data
  • Chat: connecting to Discord will be much easier. I know maddinat0r is working on a Discord plugin in C++ but it would be quicker to just use the existing Discord.py Python library to write the plugin in Python and use the Redis plugin to send chat messages or commands to and from the SA:MP server.
  • Large data store: In my Scavenge and Survive gamemode, there can be tens of thousands of items in the world and a lot of these items have data associated with them such as integers and strings. Redis could be useful in situations like this to store data outside of the SA:MP context, cut down compile times and increase development productivity.
  • Logging: Redis has been used as a log aggregator in the past, if you have a lot of log data and you want to do something clever with it (admin panel, analysis, etc) you can shove it down a Redis queue.
I'll release some code examples at a later date to showcase the power of this plugin.

Last edited by [HLF]Southclaw; 29/07/2017 at 03:37 PM.
[HLF]Southclaw is offline   Reply With Quote
Old 10/02/2017, 11:42 PM   #2
BiosMarcel
High-roller
 
BiosMarcel's Avatar
 
Join Date: Jul 2012
Location: Germany
Posts: 1,231
Reputation: 201
Default Re: Redis (cache, database and message queue)

Awesome, definitely gonna use that sooner or later. 5 Start from me!

Edit: I said 5 stars and didn't rate, i am a genius

Last edited by BiosMarcel; 11/02/2017 at 10:59 AM.
BiosMarcel is offline   Reply With Quote
Old 10/02/2017, 11:56 PM   #3
Pottus
Godfather
 
Pottus's Avatar
 
Join Date: Jun 2012
Posts: 5,304
Reputation: 1164
Default Re: Redis (cache, database and message queue)

That is pretty fucking cool the first thing that came to mind when I saw this was getting SA-MP servers to interact with other SA-MP servers.
Pottus is offline   Reply With Quote
Old 11/02/2017, 10:43 AM   #4
[HLF]Southclaw
High-roller
 
[HLF]Southclaw's Avatar
 
Join Date: Apr 2009
Location: England
Posts: 4,710
Reputation: 1255
Default Re: Redis (cache, database and message queue)

Quote:
Originally Posted by Pottus View Post
That is pretty fucking cool the first thing that came to mind when I saw this was getting SA-MP servers to interact with other SA-MP servers.
Yeah stuff like that will be much easier, I've also got a post in the works about cluster servers which you might find interesting too.

I'll hopefully get some code examples up today or soon.
[HLF]Southclaw is offline   Reply With Quote
Old 11/02/2017, 10:45 AM   #5
SyS
High-roller
 
SyS's Avatar
 
Join Date: Oct 2015
Posts: 1,974
Reputation: 424
Default Re: Redis (cache, database and message queue)

That's pretty useful (I was reading the source code in github and came back to forum ,the first thread was its release thread).
__________________

SyS is offline   Reply With Quote
Old 11/02/2017, 02:55 PM   #6
Pottus
Godfather
 
Pottus's Avatar
 
Join Date: Jun 2012
Posts: 5,304
Reputation: 1164
Default Re: Redis (cache, database and message queue)

Quote:
Originally Posted by [HLF]Southclaw View Post
Yeah stuff like that will be much easier, I've also got a post in the works about cluster servers which you might find interesting too.

I'll hopefully get some code examples up today or soon.
I have not seen the material yet but right away my vision would be the ability to create dynamic servers on the fly then disconnect a player so they reconnect but re-route their connection to the port of the dynamic server.

"It only supports Linux and probably only ever will (unless someone gets Hiredis compiling on Windows!)"

I would highly recommend putting in copious amounts of effort to ensure cross-platform support my view is you will get maximum usage. Given that the majority of people doing any scripting use windows it just makes sense that you should really put in that time.

Last edited by Pottus; 02/07/2017 at 02:52 PM.
Pottus is offline   Reply With Quote
Old 11/02/2017, 04:30 PM   #7
[HLF]Southclaw
High-roller
 
[HLF]Southclaw's Avatar
 
Join Date: Apr 2009
Location: England
Posts: 4,710
Reputation: 1255
Default Re: Redis (cache, database and message queue)

I plan to give it a try on Windows and it could work since I'm only using a minimal portion of Hiredis (not the asynchronous part since I've used threading) so it might be possible. If not, it's likely going to require a completely different version of the plugin for Windows and I don't have much free time to be working on this stuff (linux version only took a few hours from start to finish, I'm mainly developing it for Scavenge and Survive tbh)
[HLF]Southclaw is offline   Reply With Quote
Old 11/02/2017, 05:58 PM   #8
PrO.GameR
Gangsta
 
PrO.GameR's Avatar
 
Join Date: Oct 2012
Posts: 739
Reputation: 121
Default Re: Redis (cache, database and message queue)

I was looking forward to it, but now I'm kinda sad to see it not being supported for windows, my current setup is on windows and I will be having this VPS for a while now, so not really worth it for me to switch to another VPS to use this on the linux setup of the server.

Hope to see a windows version, I can already think of hundred possibilities I'm gonna use it for if I get the chance to.
__________________
Blueberry Prison Roleplay will be back soon!
Follow the forums for more information about opening day.

Forums
PrO.GameR is offline   Reply With Quote
Old 11/02/2017, 07:45 PM   #9
PT
Godfather
 
PT's Avatar
 
Join Date: Nov 2012
Location: Portugal
Posts: 7,078
Reputation: 736
Default Re: Redis (cache, database and message queue)

This is very very good to see.

nice work you did here

thanks for sharing.
__________________
"Quem caiu e se levantou honrado era e honrado ficou..."


http://steamcommunity.com/id/pt_player/
PT is offline   Reply With Quote
Old 12/02/2017, 12:08 PM   #10
isti37
Little Clucker
 
Join Date: May 2015
Posts: 1
Reputation: 0
Default Re: Redis (cache, database and message queue)

I have a friend working on a server and he aims for 1000 players and today I came back on this forum and got a nice suprise.

If we can make this client work with under 3ms 100% latency as in the benchmarks https://redis.io/topics/benchmarks one could theoretically split up a gamemode into multiple servers and sync the callbacks/data through redis to create a multithreaded and highly available system. Basically, you could round robin 4 servers on 4 different ips on different servers and sync all data through redis. If a server crashes the client just reconnects to another server in round robin because there is no state stored in the server.

Even if a single server is capable of only 1000 players, you could just sync in players from other servers with redis and do everything from chat to quests with redis messaging. Splitting the map to different servers to offload processing and maintaining one master to sync only the data from slaves would also be nice.
isti37 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Redis cache and message queue plugin for SA:MP [HLF]Southclaw Discussion 2 06/02/2017 06:47 PM
Using Redis Misiur Discussion 3 11/08/2014 10:17 PM
Moderation Queue Syncro Screenshots and Videos 1 07/05/2014 12:57 PM
Queue iJumbo Discussion 13 06/08/2013 09:29 AM


All times are GMT. The time now is 06:29 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.