(858) 586 7777 | About | Testimonials | Contact
vteams vteams vteams vteams
  • How does it work?
    • Startup Process
    • Your First Day
  • Technologies
    • Hire PHP Developer
    • Hire App Developer
    • Hire JavaScript Developer
    • Hire ROR Developer
    • Hire IOS Developer
    • Hire .NET Developer
    • Hire AI Developer
    • Hire Robotics Engineer
  • Sample Budgets
  • Meet The Team
  • Experiments
  • Captain’s Log
  • Blog
vteams vteams
  • How does it work?
    • Startup Process
    • Your First Day
  • Technologies
    • Hire PHP Developer
    • Hire App Developer
    • Hire JavaScript Developer
    • Hire ROR Developer
    • Hire IOS Developer
    • Hire .NET Developer
    • Hire AI Developer
    • Hire Robotics Engineer
  • Sample Budgets
  • Meet The Team
  • Experiments
  • Captain’s Log
  • Blog
Blog
  1. vteams
  2. Blog
  3. Using Redis to Share Real Time Data among Servers
Jun 22
using-redis-to-share-real-time-data-among-servers

Using Redis to Share Real Time Data among Servers

  • June 22, 2017

vteam #678 has been working on a telephony web application. To increase the capacity (concurrent users) and reliability of the application, the client required load balancing so that the agents (users) can login from any of the three servers. Additionally, the application’s admin panel was required to display and control the status of all the active agents on any of the three servers.

As the status of agents was changing continuously, some real time features were required. In addition to it, the real time data (like 100 changes per second) must be stored somewhere so that it could be accessed and manipulated later quickly. The following could be the possible solutions to store real time data but it is recommended not to:

  • Make dozens of database queries per second
  • Share the sessions of the three servers
  • Share the files of the three servers
    .

Solution

After doing some R&D, it was proposed and later decided to use Redis because it supports many data structures including:

  • Strings
  • Sets
  • Lists
  • Hashes, etc.
    .

vteams engineer stored the real time data of the client’s application in Redis Hashes. To hold the database, the application already had a dedicated server. The same server was used for Redis. It was then configured to accept external connections.

To implement the above mentioned solution, setup redis as per the instructions given in the laravel docs. This setup included the “predis/predis” package for Laravel. Since the application had two user guards i.e. “user” and “admin”, it was decided to define a trait which would be responsible for all the communication with Redis.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
namespace App\Traits;
 
/**
* Class HasStatusInCache
* @package App\Traits
*/
trait HasStatusInCache
{
   /**
    * @var string
    * Default key used to identify user records
    */
   public $cacheKey = 'id';
 
   /**
    * @return \Illuminate\Redis\Database
    * In case we decide to replace Redis with some other cache driver
    */
   protected function cacheClient()
   {
       return app('redis');
   }
 
   /**
    * @return string
    * Used to group objects in the Cache
    * e.g. You can set cacheTag for Users as "users" and for Admins as "admins"
    * Helpful when if need all the users or admins at once:
    * $keys = app("redis")->keys("agents*");
    * foreach ( $keys as $key ) {
    *      $users[] = app("redis")->get($key);
    */
   protected function cacheTag()
   {
       return get_called_class();
   }
 
   /**
    * @return string
    * Actual cache key stored, as a combination of cacheKey and cacheTag
    */
   protected function cacheField()
   {
       $key = $this->{$this->cacheKey};
       return "{$this->cacheTag()}:{$key}";
   }
 
   /**
    * @return mixed
    * Get all the key value pairs of the hashed data
    */
   public function getCachedStatus()
   {
       return $this->cacheClient()->hgetall($this->cacheField());
   }
 
   /**
    * @param $data
    * @return mixed
    * Sets the key value pairs provided in $data
    * By default Redis hashes work in a way that $data is set in such a way that only provided keys are over-written.
    * So you only need to provide the fields that you need to set / update
    */
   public function setCachedStatus($data)
   {
       return $this->cacheClient()->hmset($this->cacheField(), $data);
   }
 
   /**
    * @param $key
    * @return mixed
    * Get the value for a specific key
    */
   public function getCachedStatusValue($key)
   {
       return $this->cacheClient()->hget($this->cacheField(), $key);
   }
 
   /**
    * @param $key
    * @param $value
    * @return mixed
    * Set the value for a specific key
    */
   public function setCachedStatusValue($key, $value)
   {
       return $this->cacheClient()->hset($this->cacheField(), $key, $value);
   }
 
}
 
Now the class (User / Admin) that has status in cache:
 
class User extends Authenticatable
{
   use HasStatusInCache;
 
   public function cacheTag()
   {
       return "agents";
   }
Start Using it:
example: when logging in
private function logIn($user)
{
    Auth::login($user);
    …
    // set status to cache
    $user->setCachedStatus($user->toArray());
    …
}
 
example: when something changes
$user->setCachedStatusValue('station', $station->title);
 
or
// save the current status to cache
$agentStatusData = [
    'auxCode' => $status->name,
    'auxCodeId' => $status->id
];
$agent->setCachedStatus($agentStatusData);

Conclusion

As a result, the three servers were then able to share their data in an extremely fast data store.

  • Facebook
  • Twitter
  • Tumblr
  • Pinterest
  • Google+
  • LinkedIn
  • E-Mail

Comments are closed.

SEARCH BLOG

Categories

  • Blog (490)
  • Captain's Log (1)
  • Closure Reports (45)
  • Experiments (7)
  • How-To (56)
  • Implementation Notes (148)
  • Learn More (156)
  • LMS (8)
  • Look Inside (10)
  • Operations Log (12)
  • Programmer Notes (20)
  • R&D (14)
  • Rescue Log (4)
  • Testimonials (25)
  • Uncategorized (4)

RECENT STORIES

  • GitHub Actions- Automate your software workflows with excellence
  • Yii Framework – Accomplish Repetitive & Iterative Projects with Ease
  • A Recipe for CRM Software Development
  • Are Agile and DevOps the same?
  • The Data Scientist’s Toolset

ARCHIVES

In Short

With the vteams model, you bypass the middleman and hire your own offshore engineers - they work exclusively for you. You pay a reasonable monthly wage and get the job done without hassles, re-negotiations, feature counts or budget overruns.

Goals for 2020

  • Open development center in Australia
  • Complete and Launch the Robot
  • Structural changes to better address Clients' needs

Contact Us

Address: NEXTWERK INC.
6790 Embarcadero Ln, Ste 100,
Carlsbad, CA 92011, USA

Tel: (858) 586 7777
Email: fahad@nextwerk.com
Web: www.vteams.com

© 2020 vteams. All Rights Reserved.

Content Protection by DMCA.com